Database Design/Construction Commands Flashcards

1
Q

What is the purpose of SQL commands in database design and construction?

A

They are used to set up and modify the database structure.

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

What command is used to create a database?

A

CREATE DATABASE database_name;

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

What 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

Why would you use ‘IF NOT EXISTS’ in CREATE TABLE?

A

To avoid errors if the table already exists.

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

What is the basic syntax to create a table in SQL?

A

CREATE TABLE [IF NOT EXISTS] table_name (column1 datatype constraint, column2 datatype constraint, …, PRIMARY KEY (column1), FOREIGN KEY (column) REFERENCES parent_table(parent_column));

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

Why do we specify data types when creating columns?

A

To define the kind of data the column will hold and enforce data integrity.

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

What is the purpose of constraints in table creation?

A

To enforce rules on the data, such as uniqueness, non-null values, or referential integrity.

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

List four 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 numerical data types.

A

Int, Float, Double.

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

Give examples of string data types.

A

Varchar(n), Text.

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

Give examples of date-time data types.

A

Date, Time, Datetime, Timestamp.

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

What is the boolean data 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
13
Q

What does the PRIMARY KEY constraint do?

A

Ensures the column holds unique, non-null values.

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

What does the FOREIGN KEY constraint do?

A

Links tables by ensuring a column’s value exists in another table’s primary key.

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

What does the NOT NULL constraint do?

A

Ensures a column cannot contain null values.

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

What does the UNIQUE constraint do?

A

Ensures all values in the column are unique.

17
Q

What does the DEFAULT constraint do?

A

Sets a default value for a column if no value is provided.

18
Q

What does the CHECK constraint do?

A

Enforces a rule for acceptable values in a column.

19
Q

Give an example of using the DEFAULT constraint.

A

column1 datatype DEFAULT value.

20
Q

Give an example of using the CHECK constraint.

A

column1 datatype CHECK (condition).

21
Q

What command shows the structure of a table?

A

DESC table_name;

22
Q

What command adds a column to a table?

A

ALTER TABLE table_name ADD column_name datatype constraints;

23
Q

What command deletes a column from a table?

A

ALTER TABLE table_name DROP column_name;

24
Q

What command modifies a column in a table?

A

ALTER TABLE table_name MODIFY COLUMN column_name new_data_type [constraint];

25
Q

What command renames a column in a table?

A

ALTER TABLE table_name RENAME COLUMN old_name TO new_name;

26
Q

Can multiple ALTER operations be combined in one command?

A

Yes, multiple altering operations can be combined.

27
Q

If you want to ensure a column never contains null values, which constraint should you use?

A

NOT NULL constraint.

28
Q

If you want to prevent duplicate values in a column, what constraint should you apply?

A

UNIQUE constraint.

29
Q

If you want to link two tables, what constraint should you use?

A

FOREIGN KEY constraint.

30
Q

If you want to provide a default value for a column, what should you do?

A

Use the DEFAULT constraint.

31
Q

If you want to enforce a specific rule on the values of a column, what should you use?

A

CHECK constraint.

32
Q

Spot the mistake: ‘CREATE TABLE employees (id INT PRIMARY, name VARCHAR(100));’

A

Error: PRIMARY should be PRIMARY KEY.

33
Q

Spot the mistake: ‘ALTER TABLE employees MODIFY name VARCHAR(100);’

A

Correct syntax is: ALTER TABLE employees MODIFY COLUMN name VARCHAR(100);

34
Q

Explain in your own words the role of a foreign key.

A

A foreign key links two tables and ensures data consistency between related records.

35
Q

Describe why constraints are important when designing a database.

A

Constraints enforce data validity, ensure accuracy, and maintain integrity.

36
Q

If you forget to use PRIMARY KEY in table design, what problem could arise?

A

Duplicate or null records could enter, compromising data integrity.

37
Q

What happens if you skip data types when creating columns?

A

The database will not know how to store or validate the data properly.