Database Design/Construction Commands Flashcards

(32 cards)

1
Q

What is the goal of SQL design-construction commands?

A

To define and modify the database schema.

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

What SQL command creates a new database?

A

CREATE DATABASE database_name;

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

What SQL command deletes a database?

A

DROP DATABASE database_name;

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

What is the basic syntax to create a table?

A

CREATE TABLE table_name (column1 datatype constraint, …);

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

What is the purpose of constraints in table creation?

A

To enforce data validity rules like uniqueness, non-null values, or references.

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

How do you define a primary key in CREATE TABLE?

A

CONSTRAINT pk_name 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 in CREATE TABLE?

A

CONSTRAINT fk_name FOREIGN KEY (column_name) REFERENCES parent_table(parent_column)

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

List four major categories of SQL data types.

A

Numerical, String, Date-Time, Boolean.

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

Give examples of SQL string data types.

A

VARCHAR(n), TEXT

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

What is the BOOLEAN type used for?

A

To store TRUE or FALSE values.

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

What does the PRIMARY KEY constraint enforce?

A

That column values are unique and not null.

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

What does the FOREIGN KEY constraint do?

A

It ensures values in the column exist in the referenced table’s primary key.

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

What does NOT NULL constraint enforce?

A

That a column cannot have NULL values.

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

What does the UNIQUE constraint enforce?

A

That no duplicate values exist in the column.

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

What does the DEFAULT constraint do?

A

Assigns a default value if no value is provided.

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

What does the CHECK constraint do?

A

Ensures a column value satisfies a given condition.

17
Q

Give a syntax example using DEFAULT.

A

column_name datatype DEFAULT value

18
Q

Give a syntax example using CHECK.

A

column_name datatype CHECK (condition)

19
Q

What command shows the structure of a table?

A

DESC table_name;

20
Q

What command adds a column to a table?

A

ALTER TABLE table_name ADD column_name datatype constraint;

21
Q

How do you add a column at a specific position?

A

Use AFTER keyword in ADD COLUMN: … ADD COLUMN name AFTER existing_column;

22
Q

What command deletes a column?

A

ALTER TABLE table_name DROP column_name;

23
Q

What command modifies a column’s type or constraint?

A

ALTER TABLE table_name MODIFY COLUMN column_name new_type [constraint];

24
Q

What command renames a column?

A

ALTER TABLE table_name RENAME COLUMN old_name TO new_name;

25
Can multiple ALTER operations be combined?
Yes, they can be chained in one ALTER TABLE statement.
26
Give an example of multiple ALTER operations.
ALTER TABLE Employees DROP COLUMN MiddleName, ADD Email VARCHAR(100), MODIFY Department VARCHAR(50);
27
Spot the mistake: 'CREATE TABLE employees (id INT PRIMARY, name VARCHAR(100));'
PRIMARY should be PRIMARY KEY.
28
Spot the mistake: 'ALTER TABLE employees MODIFY name VARCHAR(100);'
Use MODIFY COLUMN instead: ALTER TABLE employees MODIFY COLUMN name VARCHAR(100);
29
If a column must never be empty, what constraint is needed?
NOT NULL
30
If a column must be unique but also optional, what combination of constraints works?
UNIQUE without NOT NULL
31
Explain why constraints are vital in database design.
They prevent bad data from being entered and protect integrity.
32
If you forget the primary key when designing a table, what problems may arise?
Duplicates, difficulty linking records, poor integrity.