SQL six kinds of constraints

Database constraints


1.not null-null constraint

  • ① mandatory column does not accept null values
  • ② Example: When you create a table, name varchar (6) not null,

2.unique unique constraint

  • ① constraint uniquely identifies each record in the database table
  • ②unique and provides a unique primary key constraint data
  • ③primary key constraint defined automatically have Unique
  • ④ Note: Each table can have only one primary key constraint, but can have multiple constraints Unique
  • ⑤ Syntax:
    1.name int UNIQUE
    2.unique (column_name)
    3.CONSTRAINT uc_PersonID UNIQUE (P_Id, the LastName) add multiple constraints
    4.alter table table_name add unique (column_name) adding constraints table
    5.ALTER TABLE table_name DROP CONSTRAINT delete the primary key constraint name

3.primary key constraint

  • ① constraint uniquely identifies each record in the database table
  • ② primary key must contain unique values
  • ③ primary key column can not be empty
  • ④ Each table should have a primary key, but can have only one primary key
  • ⑤ syntax:
    1.StudentID int not null Primary Key to create student number as the primary key
    2.primary key (Students) student ID to create a primary key
    3.primary key (StudentID, Email) to create student ID and Email is the primary key
  • ⑥ create a primary key for the existing columns
    1.alter table table_name add primary key (column_name )
  • ⑦ delete the primary key constraint
    1.alter table table_name drop primary key
  • ⑧ delete the primary key constraint
    1.alter table table_name drop constraint primary key constraint officiating key constraint name query can use sp_help

4.foreign key constraint

  • ① a foreign key in the table points to another table primary key
  • ②foreign key constraint for preventing destruction of the connection between the operation table
  • ③foreign key constraints can prevent illegal data into the foreign key column, because it must be one of the values ​​of the table points
  • ④ syntax:
    1.foreign Key (column_name) the References main table name (the primary key column names) to create the main table name column_name foreign key
    2.column_name int foreign key references the primary table name (the primary key column names) to create the main table name column_name foreign key
    3.alter the table table_name
    the Add foreign key (column name) references the main table name (the primary key column names) to create a foreign key column that already exists
    4.alter table table_name drop constraint foreign key constraint name delete the foreign key constraint (SQL Server oracle )
    5.alter the Table table_name drop a foreign key constraint name foreign key To delete a foreign key constraint (Mysql)

5.check constraints

  • ①check constraint for limiting the range of values ​​in a column
  • ② if done a check constraint on a single column, the column can only enter a particular numerical value
  • ③ If a table definition check constraint, then the constraint will limit the value of a particular column
  • ④ syntax:
    1.StudentID int not null the Check (StudentID> 0) limit StudentID input is greater than 0 (the Oracle SQL Server)
    2.StudentID int not null, the limit value StudentID input is greater than 0 (Mysql)
    the Check (StudentID> 0)
    3.sex VARCHAR (2) not null the Check (sex = 'male' or sex = 'female') limit sex only sex is male or female
    4.alter table table_name add check (column name> 0) to have some columns adding check constraints
    5.alter table table_name drop constraint check constraint name deleted constraint constraint name can sp_help table_name View

6.default constraints

  • ①default constraints for inserting a default value to the column
  • ② if no other value is specified, then the default value will be added to all new records
  • ③ Syntax:
    1.name VARCHAR (10) default 'John Doe' name default name insertion seating
    2.systime date default gatedate () The default value inserted into the Time getetime () function is the default value of the time of
    3.alter table table_name add column name set default 'value' column is inserted the default value to an existing name
    4.alter table table_name drop constraint default constraint remove constraint name
发布了104 篇原创文章 · 获赞 60 · 访问量 5842

Guess you like

Origin blog.csdn.net/Q_1849805767/article/details/103731914