L.06 Flashcards

SQL DDL

1
Q

What is SQL?

A

A standardized language for interacting with relational databases.
Can be pronounced as “sequel” or “S-Q-L.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two main parts of SQL?

A

DDL (Data Definition Language): Defines the database schema.
DML (Data Manipulation Language): Manages and queries data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are some common relational database systems?

A

Open-source: MySQL, PostgreSQL
Commercial: Oracle, Microsoft SQL Server

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the command to create a table?

A

CREATE TABLE table_name (
column_name data_type constraints
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between CREATE TABLE and CREATE VIEW?

A

CREATE TABLE: Creates a physical table stored in the database.
CREATE VIEW: Creates a virtual table based on a query.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a primary key?

A

A column (or set of columns) that uniquely identifies each row in a table.
Defined using PRIMARY KEY(column_name).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you define a foreign key?

A

FOREIGN KEY (column_name) REFERENCES referenced_table(primary_key)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the different types of relationships in databases?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are some key constraints in SQL?

A

PRIMARY KEY – Ensures uniqueness.
UNIQUE – Ensures unique values in a column.
NOT NULL – Ensures a column cannot be empty.
CHECK – Enforces a specific condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Example of a CHECK constraint:

A

Dnumber INT NOT NULL CHECK (Dnumber > 0 AND Dnumber < 21);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you alter a table?

A

ALTER TABLE table_name
ADD column_name data_type;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you drop a table?

A

DROP TABLE table_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens if a referenced key is deleted?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Common data types in SQL?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you define a domain type?

A

CREATE DOMAIN SSN_TYPE AS CHAR(8);

Useful for consistency when using the same type across multiple tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you auto-generate a primary key?

A

CREATE TABLE table_name (
id SERIAL PRIMARY KEY
);

SERIAL automatically increments the ID value.

17
Q

How do you define a multi-attribute primary key?

A

PRIMARY KEY (column1, column2);

18
Q

How do you explicitly name constraints?

A

CONSTRAINT constraint_name PRIMARY KEY (column_name);

19
Q

What are referential integrity triggers?

A

Ensure that foreign keys reference valid values in another table.
Options include CASCADE, SET NULL, RESTRICT, SET DEFAULT.

20
Q

How do you enforce total participation in relationships?

A

Use NOT NULL in the foreign key definition.

CREATE TABLE ENROLLMENT (
student CHAR(10) NOT NULL,
FOREIGN KEY (student) REFERENCES STUDENT(personnr)
);

21
Q

How do you change a column’s default value?

A

ALTER TABLE table_name
ALTER COLUMN column_name SET DEFAULT value;

22
Q

How do you drop a column?

A

ALTER TABLE table_name
DROP COLUMN column_name;

23
Q

Why should you be careful when executing SQL commands?

A

Most DBMSs do not ask for confirmation before executing commands.
Always back up data before running delete or update statements.

24
Q

How do you check if a table was successfully created?

A

\dt
SELECT * FROM table_name;