0% found this document useful (0 votes)
7 views3 pages

SQL Database Management for Bands and Albums

Uploaded by

crousodan64
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

SQL Database Management for Bands and Albums

Uploaded by

crousodan64
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

CREATE DATABASE test;

DROP DATABASE test;


CREATE TABLE test (
test_table INT
);

ALTER TABLE test


ADD another_column VARCHAR(255);
----------------x-----------------
CREATE DATABASE record_company;
USE record_company;

CREATE TABLE bands (


id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE albums (


id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
release_year INT,
band_id INT NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY (band_id) REFERENCES band(id)
);

INSERT INTO bands (name)


VALUES ('Iron Maiden');

INSERT INTO bands (name)


VALUES ('Deuce'), ('Avenged Sevenfold'), ('Ankor');

SELECT * FROM bands; // will full table

SELECT * FROM bands LIMIT 2; // will give first 2 entries of table

SELECT name FROM bands;

SELECT id AS 'ID', name AS 'Band Name' FROM bands;

SELECT * FROM bands ORDER BY name DESC;

INSERT INTO alumbs (name,release_year, band_id)


VALUES ('The Number of the Beasts', 1985, 1),
('Power Slave', 2018, 2),
('Nightmare', 2018, 2),
('Nightmare', 2010, 3),
('Test Album', NULL, 3);

SELECT * FROM albums;

SELECT DISTINCT name FROM albums;


UPDATE albums
SET release_year = 1982
WHERE id = 1;

SELECT * FROM albums


WHERE release_year < 2000;

SELECT * FROM albums


WHERE name LIKE '%er%';

SELECT * FROM albums


WHERE release_year = 1984 AND(/OR) band_id = 1;

SELECT * FROM albums


WHERE release_year BETWEEN 2000 AND 2018;

SELECT * FROM albums


WHERE release_year IS NULL;

DELETE FROM albums; / will delete data in table


DELETE FROM albums WHERE id = 5;

// JoINING TABLES

//INNER JOIN --> returns only those values whose match it is able to find.
(Matlab ki left and right dono tables me value honi chaiye unke conidition wale
column me)
SELECT * FROM bands
INNER JOIN albums ON [Link] = albums.band_id;

//Everything on left table will be on master table


SELECT * FROM bands
LEFT JOIN albums ON [Link] = albums.band_id;

//Everything on right table will on master table


SELECT * FROM bands
RIGHT JOIN albums ON [Link] = albums.band_id;

//Aggregate Functions

SELECT AVG(release_year) FROM albums;


SELECT SUM(release_year) FROM albums;

SELECT band_id, COUNT(band_id) FROM albums


GROUP BY band_id;

Common questions

Powered by AI

The SQL command SELECT DISTINCT is used to remove duplicate rows from a result set, returning only unique entries for the selected columns . This operation is particularly useful when duplicate data can obscure analysis and clarity, such as when counting unique occurrences or omitting redundant data in datasets. Using DISTINCT can impact performance slightly as it requires additional processing to identify and remove duplicates.

The command SELECT * FROM bands retrieves all columns and rows from the bands table . In contrast, SELECT name FROM bands selects only the values from the 'name' column of all entries, omitting all other columns . Both commands query data from the same table, but with different scopes of information retrieval, allowing for flexibility based on data retrieval needs.

Foreign key constraints enhance data integrity by ensuring that every value in a column of a table exists in the referenced primary key column of another table. This ensures that the relationships between tables remain consistent, preventing invalid entries and maintaining referential integrity . For instance, in the given document, a foreign key is used to link band_id in the albums table with id in the bands table.

Using DROP DATABASE in SQL deletes an entire database, including all tables and data within it, without the possibility of recovery unless backups exist. This operation is irreversible and should be used with caution to avoid unintended data loss . Proper permissions and double-checking the target database are critical to mitigate accidental database deletions.

In SQL queries, logical operators such as AND, OR, and BETWEEN can be used to filter results based on multiple conditions. The AND operator requires all conditions to be true for a record to be selected . The OR operator requires at least one condition to be true . The BETWEEN operator selects values within a specified range and is inclusive . Using these operators can refine queries for precise data retrieval.

A LEFT JOIN includes all records from the left table and matched records from the right table, with NULLs in the result set where there is no match in the right table . An INNER JOIN, however, includes only those records that have matching values in both tables, excluding any records without a match . This fundamental difference affects the resulting dataset size and inclusion scope, making LEFT JOINs useful for retaining unmatched records for comprehensive analysis.

Aggregate functions in SQL, such as AVG, SUM, and COUNT, are used to extract summary statistics from datasets . AVG calculates the average of a specified column, SUM computes the total value, and COUNT finds the number of entries matching a criteria. These functions are often combined with GROUP BY clauses to generate summaries and insights from grouped data . Aggregate functions provide powerful tools for analyzing large datasets efficiently.

The UPDATE SQL statement is used to modify existing records in a table. It requires a specified column to update and new values, often accompanied by a WHERE clause to limit the operation to specific records . Precautions include ensuring the WHERE clause is correct to avoid unintentionally updating all records. Additionally, it is advised to backup data before performing updates and test the operation in a development environment to mitigate the risk of data corruption.

SQL can remove specific datasets using the DELETE operation, which removes records from a table based on specified conditions in a WHERE clause . If improperly used, such as omitting the WHERE clause or applying incorrect conditions, DELETE can erase more data than intended, leading to significant data loss. Therefore, it is crucial to ensure accuracy in condition statements and perform operations in a test environment or with backups in place.

SQL table join operations determine how data from different tables are combined based on a related column. An INNER JOIN returns only rows with matching values in both tables . A LEFT JOIN returns all rows from the left table and the matched rows from the right table; if no match is found, NULLs are returned for columns from the right table . Conversely, a RIGHT JOIN returns all rows from the right table and matched rows from the left table with NULLs for non-matches .

You might also like