Data integrity constraints in SQL
PRIMARY KEY is a set of fields (1 or more), the values of which form a unique combination and are used to uniquely identify a record in the table. Only one such constraint can be created for a table. This constraint is used to enforce the integrity of the entity that is described by the table.
CHECK is used to restrict the set of values that can be placed in a given column. This constraint is used to ensure the integrity of the domain, which is described by the tables in the database.
UNIQUE ensures that there are no duplicates in a column or set of columns.
FOREIGN KEY protects against actions that could break relationships between tables. FOREIGN KEY in one table points to PRIMARY KEY in another. Therefore, this restriction is intended to ensure that there are no FOREIGN KEY entries that do not correspond to PRIMARY KEY entries.
Differences between PRIMARY and UNIQUE constraints
By default, the PRIMARY constraint creates a clustered index on a column, and UNIQUE creates a nonclustered index. Another difference is that PRIMARY does not allow NULL records, while UNIQUE allows one (and in some DBMSs several) NULL records.
Can a value in a column that has a FOREIGN KEY constraint be NULL?
Maybe, if the given column does not have a NOT NULL constraint.
Create an index
The index can be created either with the CREATE INDEX statement:
CREATE INDEX index_name ON table_name (column_name)
or by specifying an integrity constraint as a unique UNIQUE or primary PRIMARY key in the CREATE TABLE statement.
MERGE statement
MERGE allows you to merge data from one table with data from another table. When tables are merged, the condition is checked, and if it is true, then an UPDATE is executed, and if not, an INSERT. At the same time, it is impossible to change the table fields in the UPDATE section, which are used to link two tables.
Difference between DELETE and TRUNCATE statements
DELETE - DML statement, deletes records from the table that meet the WHERE criterion, while triggers, constraints, etc. are involved.
TRUNCATE - DDL statement, deletes a table and creates it anew. Moreover, if there are FOREGIN KEY references to this table or the table is used in replication, then such a table cannot be recreated.
Read also:
Comments
Post a Comment