CIS275 - Chapter 4: Structured Query Language Flashcards
_____ is a high-level computer language for storing, manipulating, and retrieving data in a relational database.
Structured Query Language (SQL)
- A database designer uses SQL to create a database and the database tables.
- A database user uses SQL to insert, retrieve, update, and delete data from the tables.
An _____ is a complete command composed of one or more clauses.
SQL statement
A clause groups SQL keywords like SELECT, FROM, and WHERE with table names like City, column names like Name, and conditions like Population > 100000. An SQL statement may be written on a single line, but good practice is to write each clause on a separate line.
- The SELECT clause starts the statement. Name is a column name.
- The FROM clause must follow the SELECT clause. City is a table name.
- The WHERE clause is optional. When included, the WHERE clause must follow the FROM clause. Population > 100000 is a condition.
- The three clauses ending in a semicolon is a statement. The statement retrieves the names of all cities that have a population greater than 100,000 people.
The SQL language is divided into five sublanguages:
_____ defines the structure of the database.
Data Definition Language (DDL)
The SQL language is divided into five sublanguages:
_____ retrieves data from the database.
Data Query Language (DQL)
The SQL language is divided into five sublanguages:
_____ manipulates data stored in a database.
Data Manipulation Language (DML)
The SQL language is divided into five sublanguages:
______ controls database user access.
Data Control Language (DCL)
The SQL language is divided into five sublanguages:
_____ manages database transactions.
Data Transaction Language (DTL)
The _____ statement creates a new database.
CREATE DATABASE
Once a database is created, tables can be added to the database.
The _____ statement deletes the database, including all tables in the database.
DROP DATABASE
The _____ statement provides database users and administrators with information about databases, the database contents (tables, columns, etc.), and server status information.
SHOW
Commonly used SHOW statements include:
_____ lists databases available in the database system.
SHOW DATABASES
Commonly used SHOW statements include:
_____ lists tables available in the currently selected database.
SHOW TABLES
Commonly used SHOW statements include:
_____ lists columns available in a specific table named by a FROM clause.
SHOW COLUMNS
Commonly used SHOW statements include:
_____ shows the CREATE TABLE statement for a given table.
SHOW CREATE TABLE
The ___ statement selects a database and is required to show information about tables within a specific database.
USE
A _____ is a number that may be negative.
signed number
An _____ is a number that cannot be negative.
unsigned number
Integer
1 byte
Signed range: -128 to 128
Unsigned range: 0 to 255
TINYINT
Integer
2 bytes
Signed range: -32,768 to 32,767
Unsigned range: 0 to 65,535
SMALLINT
Integer
3 bytes
Signed range: -8,388,608 to 8,388,607
Unsigned range: 0 to 16,777,215
MEDIUMINT
Integer
4 bytes
Signed range: -2,147,483,648 to 2,147,483,647
Unsigned range: 0 to 4,294,967,295
INTEGER or INT
8 bytes
Signed range: -2^63 to 2^63 -1
Unsigned range: 0 to 2^64 -1
BIGINT
Decimal
Varies depending on M and D
Exact decimal number where M = number of significant digits,
D = number of digits after decimal point
DECIMAL(M,D)
Decimal
4 bytes
Approximate decimal numbers with range: -3.4E+38 to 3.4E+38
FLOAT
Decimal
8 bytes
Approximate decimal numbers with range: -1.8E+308 to 1.8E+308
DOUBLE
Date and time
3 bytes
Format: YYYY-MM-DD. Range: ‘1000-01-01’ to ‘9999-12-31’
DATE
Date and time
3 bytes
Format: hh:mm:ss
TIME
Date and time
5 bytes
Format: YYYY-MM-DD hh:mm:ss.
Range: ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’.
DATETIME
Character
N bytes
Fixed-length string of length N; 0 ≤ N ≤ 255
CHAR(N)
Character
Length of characters + 1 bytes
Variable-length string with maximum N characters; 0 ≤ N ≤ 65,535
VARCHAR(N)
The _____ statement creates a new table by specifying the table name, column names, and column data types.
CREATE TABLE
The _____ statement deletes a table, along with all the table’s rows, from a database.
DROP TABLE
Ex: DROP TABLE Employee; deletes the Employee table.
The _____ statement adds, deletes, or modifies columns on an existing table.
ALTER TABLE
The ALTER TABLE statement specifies the table name followed by a clause that indicates what should be altered. The table below summarizes the three ALTER TABLE clauses.
- The ALTER TABLE statement adds a Salary column to Employee that holds 7 significant digits and 2 decimal places.
- The ALTER TABLE statement changes the Salary column’s name to AnnualSalary. The data type is changed to INT.
- The ALTER TABLE statement deletes the AnnualSalary column.
CREATE TABLE Country (
ISOCode3 CHAR(3),
Population INTEGER UNSIGNED
);
DROP TABLE Country;
CREATE TABLE Country creates table Country.
Since the code is always three letters long, the best data type is CHAR(3). Population should be stored as a whole number, and since no country’s population exceeds 1,500,000,000 and a population can’t be negative, INTEGER UNSIGNED or INT UNSIGNED is the best data type.
DROP TABLE Country deletes the Country table.
Expected:
ALTER TABLE Country
DROP Population;
The ALTER TABLE statement adds, deletes, or modifies columns on an existing table. DROP Population; deletes the column Population.
Expected:
ALTER TABLE Country
ADD IndepDate DATE;
The ALTER TABLE statement adds, deletes, or modifies columns on an existing table. ADD IndepDate DATE; adds a column called IndepDate. Since IndepDate will hold a date, the best data type is DATE.
Expected:
ALTER TABLE Country
DROP IndepDate;
The ALTER TABLE statement adds, deletes, or modifies columns on an existing table.
DROP IndepDate; deletes the column IndepDate.
Expected:
ALTER TABLE Country
CHANGE Population OfficialPopulation INTEGER UNSIGNED;
CHANGE Population OfficialPopulation INTEGER UNSIGNED; renames Population to OfficialPopulation and changes OfficialPopulation from accepting numbers up to 8,388,607 to accepting numbers up to 2,147,483,647.
A _____ is a rule that applies to table data.
constraint
Constraints are specified in a CREATE TABLE statement or may be added to a preexisting table with an ALTER TABLE statement.
The _____ in a CREATE TABLE statement names the table’s primary key, the column(s) that uniquely identify each row.
PRIMARY KEY constraint
An auto-increment column is a column that is assigned an automatically incrementing value.
auto-increment column
A primary key on an ID column is commonly implemented as an auto-increment column.
Ex: A database system may assign an auto-incrementing column values 1, 2, 3, etc. for each row that is inserted into the table.
The AUTO_INCREMENT keyword defines an auto-increment column in MySQL.
A _____ is added to a CREATE TABLE statement with the FOREIGN KEY and REFERENCES keywords.
foreign key constraint
- The Employee table has primary key ID and several rows.
- The Department table is created with a FOREIGN KEY constraint that REFERENCES the Employee ID column.
- When rows are added to Department, the ManagerID value must exist in Employee ID. ManagerID 9999 is rejected because 9999 does not exist in Employee ID.
_____ responds to an invalid primary key deletion. Ex: Deleting a primary key 1234 that is used in a foreign key.
ON DELETE
ON DELETE and ON UPDATE must be followed by a response:
RESTRICT
SET NULL
SET DEFAULT
CASCADE
_____ responds to an invalid primary key update. Ex: Updating a primary key 1234 to 5555 when 1234 is used in a foreign key.
ON UPDATE
ON DELETE and ON UPDATE must be followed by a response:
RESTRICT
SET NULL
SET DEFAULT
CASCADE
Expected:
PRIMARY KEY (ISOCode2)
PRIMARY KEY (ISOCode2) sets ISOCode2 as the primary key of the table.
Expected: PRIMARY KEY (ISOCode2, ISOCode3)
PRIMARY KEY (ISOCode2, ISOCode3) sets ISOCode2 and ISOCode3 as the composite primary key of the table.
Expected: PRIMARY KEY (TLD, ISOCode3)
PRIMARY KEY (TLD, ISOCode3) sets TLD and ISOCode3 as the composite primary key of the table.
Expected:
FOREIGN KEY (Code2) REFERENCES Country (ISOCode2)
FOREIGN KEY (Code2) REFERENCES Country (ISOCode2) indicates that the foreign key Code2 refers to the primary key in the Country table.
Expected:
FOREIGN KEY (Domain) REFERENCES Country (TLD)
FOREIGN KEY (Domain) REFERENCES Country (TLD) indicates that the foreign key Domain refers to the primary key in the Country table.
Expected:
Nothing happens to Country table. Domain .ly is deleted from Statistics table. TLD of LY is updated to .xy in Country table. Domain of .ly is updated to .xy in Statistics table.
Row containing LY is deleted in Country table. Row containing .ly is deleted in Statistics table.
Deleting the foreign key ‘.ly’ in Statistics has no effect on the primary key ‘.ly’ in Country.
ON UPDATE CASCADE updates the foreign key Domain in the Statistics table to the same value used to update the primary key TLD in the Country table.
ON DELETE CASCADE deletes rows with foreign key Domain in Statistics table when the matching primary key TLD in Country table is deleted.
Expected:
Nothing happens to Country table. CntyNm Italy is deleted from Statistics table.
Row containing .it is deleted in Country table. Row containing Italy is deleted in Statistics table.
Name of .it is updated to XYZ in Country table. CntyNm of Italy is updated to XYZ in Statistics table.
Deleting the foreign key ‘Italy’ in Statistics has no effect on the primary key ‘Italy’ in Country.
ON DELETE CASCADE deletes rows with foreign key CntyNm in Statistics table when the matching primary key Name in Country table is deleted.
ON UPDATE CASCADE updates the foreign key CntyNm in the Statistics table to the same value used to update the primary key Name in the Country table.
Expected:
Row containing Uganda is deleted in Country table. Row containing UG is deleted in Statistics table.
ISOCode2 of Uganda is updated to XY in Country table. Code2 of UG is updated to XY in Statistics table.
Nothing happens to Country table. Code2 UG is deleted from Statistics table.
ON DELETE CASCADE deletes rows with foreign key Code2 in Statistics table when the matching primary key ISOCode2 in Country table is deleted.
ON UPDATE CASCADE updates the foreign key Code2 in the Statistics table to the same value used to update the primary key ISOCode2 in the Country table.
Deleting the foreign key ‘UG’ in Statistics has no effect on the primary key ‘UG’ in Country.