L.06 Flashcards
SQL DDL
What is SQL?
A standardized language for interacting with relational databases.
Can be pronounced as “sequel” or “S-Q-L.”
What are the two main parts of SQL?
DDL (Data Definition Language): Defines the database schema.
DML (Data Manipulation Language): Manages and queries data.
What are some common relational database systems?
Open-source: MySQL, PostgreSQL
Commercial: Oracle, Microsoft SQL Server
What is the command to create a table?
CREATE TABLE table_name (
column_name data_type constraints
);
What is the difference between CREATE TABLE and CREATE VIEW?
CREATE TABLE: Creates a physical table stored in the database.
CREATE VIEW: Creates a virtual table based on a query.
What is a primary key?
A column (or set of columns) that uniquely identifies each row in a table.
Defined using PRIMARY KEY(column_name).
How do you define a foreign key?
FOREIGN KEY (column_name) REFERENCES referenced_table(primary_key)
What are the different types of relationships in databases?
1:N (One-to-Many): Foreign key in the “N” table.
1:1 (One-to-One): Can be modeled as a 1:N or merged into one table.
N:M (Many-to-Many): Requires an association table.
What are some key constraints in SQL?
PRIMARY KEY – Ensures uniqueness.
UNIQUE – Ensures unique values in a column.
NOT NULL – Ensures a column cannot be empty.
CHECK – Enforces a specific condition.
Example of a CHECK constraint:
Dnumber INT NOT NULL CHECK (Dnumber > 0 AND Dnumber < 21);
How do you alter a table?
ALTER TABLE table_name
ADD column_name data_type;
How do you drop a table?
DROP TABLE table_name;
What happens if a referenced key is deleted?
NO ACTION / RESTRICT: Prevents deletion.
CASCADE: Deletes dependent rows.
SET NULL: Sets the foreign key column to NULL.
SET DEFAULT: Sets the foreign key column to a default value.
Common data types in SQL?
Numeric: INTEGER, SMALLINT, FLOAT, DOUBLE PRECISION
Character: CHAR(n), VARCHAR(n)
Boolean: TRUE, FALSE, NULL
Date: DATE (format: YYYY-MM-DD)
Binary: BLOB, CLOB (for storing files)
How do you define a domain type?
CREATE DOMAIN SSN_TYPE AS CHAR(8);
Useful for consistency when using the same type across multiple tables.
How do you auto-generate a primary key?
CREATE TABLE table_name (
id SERIAL PRIMARY KEY
);
SERIAL automatically increments the ID value.
How do you define a multi-attribute primary key?
PRIMARY KEY (column1, column2);
How do you explicitly name constraints?
CONSTRAINT constraint_name PRIMARY KEY (column_name);
What are referential integrity triggers?
Ensure that foreign keys reference valid values in another table.
Options include CASCADE, SET NULL, RESTRICT, SET DEFAULT.
How do you enforce total participation in relationships?
Use NOT NULL in the foreign key definition.
CREATE TABLE ENROLLMENT (
student CHAR(10) NOT NULL,
FOREIGN KEY (student) REFERENCES STUDENT(personnr)
);
How do you change a column’s default value?
ALTER TABLE table_name
ALTER COLUMN column_name SET DEFAULT value;
How do you drop a column?
ALTER TABLE table_name
DROP COLUMN column_name;
Why should you be careful when executing SQL commands?
Most DBMSs do not ask for confirmation before executing commands.
Always back up data before running delete or update statements.
How do you check if a table was successfully created?
\dt
SELECT * FROM table_name;