Table Definition and Modification Flashcards

1
Q

What is the syntax for creating a table in SQL?

A

CREATE TABLE [table name] (
[attribute definition],
…,
[attribute definition],
[primary key definition],
[candidate key definition],
…,
[candidate key definition],
[foreign key definition],
…,
[foreign key definition]
);

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

What is included in the [attribute definition] section?

A

The [attribute definition] section includes the definition of each attribute of the table, specifying the attribute name and its data type.

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

What is the purpose of the [primary key definition] section?

A

The [primary key definition] section is used to define the primary key for the table, ensuring that each row in the table is uniquely identified by the values in the specified primary key columns.

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

What is the [candidate key definition] section used for?

A

The [candidate key definition] section is used to define candidate keys for the table. Candidate keys are sets of attributes that can uniquely identify each row, similar to the primary key.

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

How is the [foreign key definition] section used in table creation?

A

The [foreign key definition] section is used to define foreign keys, establishing relationships between the current table and another table. It specifies which columns in the current table reference the primary key or unique key in another table.

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

What is the syntax for defining an attribute in SQL?

A

[attribute name] [attribute type]

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

What are the possible types for an attribute in the provided information?

A

The possible types for an attribute in the provided information are:

integer: A 32-bit integer ranging from -2147483648 to 2147483647.

double: A double-precision number, for example, 3.14159.

char(n): A string with at most n characters, where n is an integer of your choice.

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

What is the range of values for the integer attribute type?

A

The integer attribute type has a range of values from -2147483648 to 2147483647.

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

How is the char(n) attribute type defined, and what does it represent?

A

The char(n) attribute type is defined with a specified length n, where n is an integer of your choice. It represents a fixed-length string with at most n characters.

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

Are there other attribute types mentioned in the provided information?

A

The provided information mentions that many other types depend on the concrete database system. It indicates that the attribute types mentioned (integer, double, char(n)) are examples, and there could be additional types depending on the specific database system.

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

What is the SQL command used to insert a tuple into a table?

A

INSERT INTO [table name] VALUES (value1, value2, …, valueN);

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

What does each value in the VALUES clause represent?

A

Each value in the VALUES clause represents the corresponding value for an attribute in the tuple being inserted. The values are listed in the order of the attributes defined in the table.

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

Can the order of values in the VALUES clause be different from the order of attributes in the table?

A

No, the order of values in the VALUES clause must match the order of attributes in the table. Each value is assigned to the corresponding attribute based on their positions.

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

Is it mandatory to specify values for all attributes when inserting a tuple?

A

Yes, when using the VALUES clause to insert a tuple, values must be specified for all attributes of the table, and the number of values provided must match the number of attributes.

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

Are there alternative ways to insert tuples into a table in SQL?

A

Yes, besides using the VALUES clause, you can also use the INSERT INTO … SELECT statement to insert tuples by selecting data from another table or result set.

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

What is the SQL command used to delete tuples from a table?

A

DELETE FROM [table name] WHERE [condition];

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

What does the WHERE clause in the DELETE statement specify?

A

The WHERE clause in the DELETE statement specifies the condition that determines which tuples to delete. Tuples that satisfy the condition will be removed from the table.

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

Is the WHERE clause optional when using the DELETE statement?

A

No, the WHERE clause is not strictly optional. If you omit the WHERE clause, it will delete all rows in the table, which is often not desired. It’s a good practice to include a condition to specify which rows to delete.

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

Are there alternative ways to delete tuples from a table in SQL?

A

Yes, another way to delete tuples is to use the TRUNCATE TABLE statement, which removes all rows from a table, effectively deleting all tuples. However, TRUNCATE TABLE is more commonly used for bulk deletion without specifying conditions.

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

What is the SQL command used to update tuples in a table?

A

UPDATE [table name] SET [column1 = value1, column2 = value2, …] WHERE [condition];

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

What does the SET clause in the UPDATE statement specify?

A

The SET clause in the UPDATE statement specifies the columns to be updated and their new values. Each assignment is in the form of column = value.

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

Is the WHERE clause optional when using the UPDATE statement?

A

While the WHERE clause is technically optional, it’s a good practice to include it. Without the WHERE clause, all rows in the table will be updated with the specified values, which might not be the intended behavior.

23
Q

Are there alternative ways to update tuples in a table in SQL?

A

Yes, another way to update tuples is to use the INSERT INTO … ON DUPLICATE KEY UPDATE statement or the MERGE statement, depending on the specific database system. These statements allow you to insert new tuples or update existing ones based on certain conditions.

24
Q

What is the purpose of the Entity Integrity Constraint in a relational database?

A

The Entity Integrity Constraint, often enforced through a Primary Key, ensures that each row in a table is uniquely identified. It guarantees that the primary key attribute (or combination of attributes) has a non-null value and is unique across all rows in the table.

25
Q

How is a Primary Key defined in a table?

A

A Primary Key is defined in a table by specifying the attribute (or combination of attributes) that uniquely identifies each row. It is declared using the PRIMARY KEY constraint.

26
Q

What does the Referential Integrity Constraint (Foreign Key) ensure in a relational database?

A

The Referential Integrity Constraint, implemented through a Foreign Key, ensures the consistency of relationships between tables. It guarantees that values in a foreign key column match values in the corresponding primary key column of the referenced table or are null.

27
Q

How is a Foreign Key defined in a table?

A

A Foreign Key is defined in a table by specifying the attribute (or combination of attributes) that refers to the primary key in another table. It is declared using the FOREIGN KEY constraint.

28
Q

What is the role of validation in a database?

A

Validation in a database ensures that data entered into the database meets certain criteria or rules. It involves checking the integrity and accuracy of data to maintain consistency and adhere to predefined standards.

29
Q

Can you provide examples of validation rules in a database?

A

Certainly! Examples of validation rules include checking that a date falls within a specific range, ensuring that a numeric value is positive, or validating that an email address follows a proper format.

30
Q

What does the Entity Integrity Constraint specify in a relational database?

A

The Entity Integrity Constraint specifies that no component of the primary key of a base relation is allowed to accept null values.

31
Q

How is NULL defined in the context of a database?

A

In the context of a database, NULL represents missing or undefined information for some reasons. It is used when a particular data value is not known or not applicable.

32
Q

What is a base relation in a relational database?

A

A base relation in a relational database is one that permanently exists in the database. It represents a table with well-defined attributes and contains persistent data.

33
Q

What is a transient relation in the context of a relational database?

A

A transient relation in the context of a relational database refers to temporary results, often in the form of a table, generated as a result of a query. Unlike base relations, transient relations are not permanently stored in the database.

34
Q

Why is it important for every relation entity in the database to be uniquely identifiable?

A

It is important for every relation entity in the database to be uniquely identifiable to ensure adherence to the Entity Integrity Constraint. Uniquely identifiable entities help maintain the integrity of the data and support efficient querying and retrieval operations.

35
Q

What is a candidate key in the context of a relational database?

A

A candidate key in a relational database is an attribute or set of attributes (composite key) K of a relation R that satisfies two time-independent properties: Uniqueness and Minimality.

36
Q

What does the Uniqueness property of a candidate key ensure?

A

The Uniqueness property ensures that, at any given time, no two rows of relation R have the same value for the candidate key K.

37
Q

What does the Minimality property of a candidate key ensure?

A

The Minimality property ensures that if the candidate key K is composite (consisting of multiple attributes), then no component of K can be eliminated without destroying the uniqueness property. In other words, it ensures that the candidate key is minimal, and removing any part of it would make it a superkey.

38
Q

How is the primary key determined in a relation?

A

The primary key is chosen from the candidate keys of a relation. It is selected based on criteria such as simplicity, stability, or application requirements.

39
Q

What is the term used for candidate keys that are not chosen as the primary key?

A

Candidate keys that are not chosen as the primary key are known as alternate keys.

40
Q

How is a primary key for a table defined in SQL?

A

In SQL, a primary key for each table is defined through a constraint. This constraint is typically specified during table creation or alteration using the PRIMARY KEY keyword.

41
Q

What additional constraints does the PRIMARY KEY automatically add to the relevant column definition?

A

The PRIMARY KEY constraint automatically adds the UNIQUE constraint and the NOT NULL constraint to the relevant column definition. This ensures that the values in the primary key column (or columns) are both unique and not null.

42
Q

What is the significance of having a PRIMARY KEY in a table?

A

Having a PRIMARY KEY in a table is a big hint to the Database Management System (DBMS) to optimize for searches by the set of attributes defined as the primary key. This optimization can significantly improve the performance of queries involving searches, joins, and other operations.

43
Q

Can a table have more than one PRIMARY KEY?

A

In most SQL database systems, a table can have only one primary key. However, a composite primary key (primary key consisting of multiple columns) is possible.

44
Q

What happens if an attempt is made to insert a NULL or duplicate value into a PRIMARY KEY column?

A

The NOT NULL constraint ensures that a NULL value cannot be inserted into the primary key column. The UNIQUE constraint prevents the insertion of duplicate values into the primary key column.

45
Q

What is a candidate key in the context of a relational database?

A

A candidate key in a relational database is a set of attributes (or a single attribute) within a relation that uniquely identifies each tuple or row in that relation. It satisfies two key properties: Uniqueness and Minimality. The uniqueness property ensures that no two rows have the same values for the candidate key, and the minimality property ensures that no proper subset of the candidate key possesses the uniqueness property.

46
Q

What is a foreign key in the context of a relational database?

A

A foreign key in a relational database is an attribute or a set of attributes in one table that refers to the primary key in another table. It establishes a relationship between the two tables, indicating that values in the foreign key column(s) of one table must correspond to the values in the primary key column(s) of the referenced table. Foreign keys are used to enforce referential integrity between related tables.

47
Q

What is Self-Referential Integrity in the context of a relational database?

A

Self-Referential Integrity in a relational database refers to the scenario where a table contains a foreign key that references the same table. In the context of employees and managers, it means that the manager is also an employee, creating a hierarchical relationship within the same table.

48
Q

Is there any restriction mentioned in the example regarding employees and managers?

A

: Yes, the example mentions that an employee may not have a manager, and the manager attribute can be null. This implies that not every employee needs to have a manager, and those who do not have a manager can be indicated by a null value in the manager attribute.

49
Q

What are Reaction Policies in the context of updating or deleting tuples in a relational database?

A

Reaction Policies refer to the actions taken when updating or deleting a tuple in a table that is referenced by foreign keys in other tables. Three common reaction policies are often specified:

Restrict: The update or delete operation is restricted to cases where there are no matching entities in the referencing table. If matching entities exist, the operation is not carried out (default behavior).

Cascades: The update or delete operation cascades to affect those matching entities in the referencing table. Changes in the referenced table propagate to the referencing table, updating or deleting corresponding tuples.

Nullifies: For delete or update, the foreign key is set to null in all matching entities in the referencing table, and then the item is deleted or updated. This policy should not apply if the foreign key cannot accept null values.

50
Q

What is the default behavior if no reaction policy is specified?

A

The default behavior, in the absence of a specified reaction policy, is often set to “Restrict.” This means that the update or delete operation is restricted unless there are no matching entities in the referencing table.

51
Q

What is the purpose of validation constraints in a relational database?

A

Validation constraints in a relational database ensure that the data entered into the database meets specific criteria or rules, maintaining data integrity and consistency

52
Q

Can you provide examples of validation constraints mentioned for certain attributes?

A

Supplier numbers must be of the form Snnnn (where nnnn stands for up to four decimal digits).
Part numbers must be of the form Pnnnnn (5 digits).
Supplier status values must be in the range 1-100.
Supplier and part cities must be drawn from a certain list.
Part colors must be drawn from a certain list.
Part weights must be greater than zero.
Shipment quantities must be a multiple of 100.
If the supplier city is London, then the status must be 20.

53
Q

How do validation constraints contribute to maintaining data integrity?

A

Validation constraints help ensure that the data entered into the database adheres to predefined standards, preventing the insertion of incorrect or inconsistent values. This, in turn, contributes to maintaining the integrity and reliability of the database.