Primary Key : A primary key is a column or a group of columns that uniquely identifies each row in a table. You create a primary key for a table by using the PRIMARY KEY
constraint
If the primary key consists of only one column, you can define use PRIMARY KEY
constraint as a column constraint:
create table sales (sale_id int primary key identity,sale_date datetime,sale_amount decimal(5,2),quantity int)
In case the primary key has two or more columns, you must use the PRIMARY KEY
constraint as a table constraint:
create table participant(sale_id int,customer_id int,primary key(sale_id,customer_id))
Each table can contain only one primary key. All columns that participate in the primary key must be defined as NOT NULL
. SQL Server automatically sets the NOT NULL
constraint for all the primary key columns if the NOT NULL
constraint is not specified for these columns.
How to Add Primary key on existing table:
create table event(event_id int not null,event_name varchar(500),event_date date,duration decimal(5,2))
alter table event add primary key(event_id)
If you update one or more rows in the parent table, you can set one of the following actions:
ON UPDATE NO ACTION
: SQL Server raises an error and rolls back the update action on the row in the parent table.ON UPDATE CASCADE
: SQL Server updates the corresponding rows in the child table when the rows in the parent table are updated.ON UPDATE SET NULL
: SQL Server sets the rows in the child table toNULL
when the corresponding row in the parent table is updated. Note that the foreign key columns must be nullable for this action to execute.ON UPDATE SET DEFAULT
: SQL Server sets the default values for the rows in the child table that have the corresponding rows in the parent table updated.
If you delete one or more rows in the parent table, you can set one of the following actions:
ON DELETE NO ACTION
: SQL Server raises an error and rolls back the delete action on the row in the parent table.ON DELETE CASCADE
: SQL Server deletes the rows in the child table that is corresponding to the row deleted from the parent table.ON DELETE SET NULL
: SQL Server sets the rows in the child table toNULL
if the corresponding rows in the parent table are deleted. To execute this action, the foreign key columns must be nullable.ON DELETE SET DEFAULT
SQL Server sets the rows in the child table to their default values if the corresponding rows in the parent table are deleted. To execute this action, the foreign key columns must have default definitions. Note that a nullable column has a default value ofNULL
if no default value specified.
By default, SQL Server appliesON DELETE NO ACTION
if you don’t explicitly specify any action.
NOT NULL
constraints simply specify that a column must not assume the NULL
UNIQUE
constraints allow you to ensure that the data stored in a column, or a group of columns, is unique among the rows in a table.UNIQUE
index to enforce the uniqueness of data stored in the columns that participate in the UNIQUE
constraint. Therefore, if you attempt to insert a duplicate row, SQL Server rejects the change and returns an error message stating that the UNIQUE
constraint has been violated.UNIQUE
constraint, SQL Server will automatically generate a name for it.UNIQUE
constraint, you use the CONSTRAINT
keyword as follows:The CHECK
constraints reject values that cause the Boolean expression evaluates to FALSE
.
Because NULL
evaluates to UNKNOWN
, it can be used in the expression to bypass a constraint.
Check constraint on multiple column: