Constraints of SQL Sever database

Database constraints


First, create, modify and delete constraints

  • Constraint is a way to automatically enforce data integrity provided by SQL Server, the rules defined by the value of the column, is mandatory integrity standard mechanism. Using constraints in preference to triggers, rules, and defaults.

1, non-empty constraint

  • Determining whether the row empty columns in the table for that column may contain null values. Null (NULL) is different from the string 0, blank or zero-length, NULL means that no input, NULL value occurs is generally unknown or undefined values.

1.1, create a non-null constraints

  • When using the CREATE TABLE can create a table, use the NOT NULL keyword specifies a non-null constraint syntax is as follows:

    [CONSTRAIN <约束名>]  NOT NULL
    

1.2, non-empty constraint modification

ALTER TABLE table_name
alter column_name column_type null | not null
table_name:要修改非空约束的表名
column_name:要修改非空约束的列名
column_type:要修改非空约束的列类型
null | not null:修改为空或非空
例:
USE TEST1
ALTER TABLE Agent
alter column Age int not null

2, the primary key constraint

  • To create a primary key constraint by defining PRIMARY KEY

2.1, create a primary key constraint

(1) creating primary key constraint when creating the table
USE TEST1
CREATE TABLE [dbo].[Tb1](
[ID][int]CONSTRAINT PK_ID PRIMARY KEY,
[Name][char](50),
[Sex][char](2),
[Age][int]
)
-- CONSTRAINT PK_ID PRIMARY KEY为创建一个主键约束,PK_ID为用户自定义的主键约束名称,该名称需是合法的标识符。
(2) create a primary key constraint existing table
ALTER TABLE table_name
ADD 
CONSTRAINT constraint_name
PRIMARY KEY[CLUSTERED | NONCLUSTERED]
{(Columns[,...n])}
CONSTRAINT:创建约束的关键字
constraint_name:创建约束的名称
PRIMARY KEY:表示所创建的约束的类型为主键约束
CLUSTERED | NONCLUSTERED:是表示为 PRIMARY KEY或 UNIQUE约束创建集聚或非集聚索引的关键字。
例:
USE TEST1
ALTER TABLE Agent
ADD CONSTRAINT ZJ_ID PRIMARY KEY(ID)

2.2, modify the primary key constraint

  • To modify the PRIMARY KEY constraint, you must delete the existing PRIMARY KEY constraint, and then re-create a new definition of the constraint.

2.3, delete the primary key constraint

  • The syntax is as follows:

    ALTER TABLE table_name
    DROP CONSTRAINT constraint_name[,...n]
    constraint_name:主键名
    例:
    USE TEST1
    ALTER TABLE Tb1
    DROP CONSTRAINT PK_ID
    

3, the only constraint

  • The only constraint enforcement UNIQUE used to uniquely set the value of the column. The UNIQUE constraint, any two rows of the table columns not have the same value. Further, the primary key uniqueness is forcibly embodiment, the primary key is not allowed as a unique value NULL.

3.1, create a unique constraint when creating tables

  • example:

    USE TEST1
    CREATE TABLE[dbo].[Employee]( --创建表
    [ID][int]CONSTRAINT UQE_ID UNIQUE,  --设置唯一约束
    [Name][char](50),
    [Sex][char](2),
    [Age][int]
    )
    

3.2, create a unique constraint on an existing table

  • The syntax is as follows:

    ALTER TABLE table_name
    ADD CONSTRAINT constraint_name
    UNIQUE[CLUSTERED | NONCLUSTERED]
    {{column[,...n]}}
    
    参数说明
    table_name:要创建唯一约束的表名称
    constraint_name:唯一约束名称
    column:要创建唯一约束的列名称。
    例子:
    USE TEST1
    ALTER TABLE Agent
    ADD CONSTRAINT Unique1_ID  --设置唯一约束
    UNIQUE(ID)
    

3.3, modify unique constraints

  • To modify a unique constraint, you must first delete the existing UNIQUE constraint, and then re-created with the new definition.

3.4 Drop the unique constraints

  • The syntax is as follows:

    ALTER TABLE table_name
    DROP CONSTRAINT constraint_name[,..n]
    
    --例子
    USE TEST1
    ALTER TABLE Tb1
    DROP CONSTRAINT Unique_ID
    

4, check constraints

  • CHECK integrity check constraints may force domain; CHECK constraints similar FOREIGN KEY constraints can be controlled into the value in the column; however, they differ in the way of determining the rms: RMS FOREIGN KEY constraints obtained from another table list, and CHECK constraint expression is determined by the effective value not eager logical data in the other columns.

4.1, create a check constraint

(1) create a check constraint when creating tables
  • example:

    USE TEST
    CREATE TABLE [dbo].[Employee1](
    [ID][int]CONSTRAINT Unique2_ID UNIQUE,
    [Name][char](50),
    [Sex][char](2)CONSTRAINT CK_Sex Check(sex in('男','女')),
    [Age][int]
    )
    
(2) create a check constraint existing table
  • The syntax is as follows:

    ALTER TABLE table_name
    ADD CONSTRAINT constraint_name
    CHECK(logical_expression)
    
    参数说明
    table_name:要创建检查约束的表名称
    constraint_name:检查约束名称
    logical_expression:要检查约束的条件表达式
    
    例子:
    USE TEST
    ALTER TABLE [Tb_student]
    --ADD CONSTRAINT Check_Sex Check(性别 in('男','女'))
    ADD CONSTRAINT CK_tz Check(统招否 in(1,0))
    
3251331-a90aedd96279952b.PNG
Check constraints .PNG

4.2, modify check constraints

  • Modify the table a column CHECK constraint expressions used, you must first delete the existing CHECK constraints, and then re-created using the new definition, in order to modify a CHECK constraint.

4.3, delete the check constraint

  • Delete the check constraint syntax is as follows:

    ALTER TABLE table_name
    DROP CONSTRAINT constraint_name[,...n]
    
    --例子
    USE TEST
    ALTER TABLE Employee1
    DROP CONSTRAINT CK_Sex
    

5, default constraints

  • When you create or modify a table can create a default by default constraint DEFAULT. The default value may be any value calculation constants are provided for each row will be assigned the default value is a default value where the column.

5.1, create a default constraint

(1) to create a default constraint when creating tables
  • The syntax is as follows:

    USE TEST
    CREATE TABLE[dbo].[temp](   --创建表
    [ID][int],
    [Name][char](50),
    [Sex][char](2)CONSTRAINT Default_Sex Default'男', --设置默认约束
    [Age][int]
    )
    USE TEST
    INSERT INTO Temp(ID,Name,Age)VALUES(1, '彭友聪', 21)
    /****** Script for SelectTopNRows command from SSMS  ******/
    SELECT TOP (1000) [ID]
          ,[Name]
          ,[Sex]
          ,[Age]
      FROM [TEST].[dbo].[Temp]
    
3251331-24a8f3a953ae2431.PNG
Default constraint .PNG
(2) to create a default constraint in the existing table
  • The syntax is as follows

    ALTER TABLE table_name
    ADD CONSTRAINT constraint_name
    DEFAULT constraint_expression[FOR column_name]
    --参数说明
    --table_name:要创建默认约束的表名称
    --constraint_name:默认约束名称
    --constraint_expression:默认值
    --例子
    USE TEST
    ALTER TABLE [Tb_student]
    ADD CONSTRAINT Default_age --设置默认约束
    DEFAULT 22 FOR 年龄
    

5.2, modify the default constraint

  • Modify a table column expressions using the Default constraint, you must first remove the existing Default constraint, and then re-created using the new definition, in order to modify Default constraints.

5.3, delete the default constraint

  • Remove the default constraint syntax is as follows:

    ALTER TABLE table_name
    DROP CONSTRAINT constraint_name[,...n]
    
    --例子
    USE TEST
    ALTER TABLE Temp
    DROP CONSTRAINT Default_Sex
    

6, the foreign key constraint

6.1, create foreign key constraints

(1) is created when you create a table
CREATE TABLE Pyc(
    [ID][int],
    [Wage][money],
    CONSTRAINT FKEY_ID
    FOREIGN KEY(ID)
    REFERENCES Temp(ID)   --外键约束
    )
(2) created in an existing table
  • The syntax

    ALTER TABLE table_name
    ADD CONSTRAINT constraint_name
    [FOREIGN KEY]{(column_name[,..n])}
    REFERENCES ref_table[(ref_column_name[,...n])]
    
  • Parameters in the following table

    parameter description
    table_name To create the name of the foreign key table
    constraint_name The name of the foreign key constraint
    FOREIGN KEY REFERENCES Data column provide a referential integrity constraints. ROFEIGN KEY constraints require that each value in the column of the corresponding table is referenced in the referenced column exists; FOREIGN KEY constraint can reference the referenced table row is UNIQUE or PRIMARY KEY constraints or referenced in Table UNIQUE INDEX within the referenced column.
    ref_table FOREIGN KEY constraint referenced table name
    (ref_column_name[,...n]) One or more columns in Table FOREIGN KEY constraint referenced
  • example:

    USE TEST1
    ALTER TABLE Tb1
    ADD CONSTRAINT Fkey_ID
    FOREIGN KEY(ID)
    REFERENCES Agent(ID)
    

6.2, modify the foreign key constraint

  • Modify the table foreign key constraint, you must first remove the existing foreign key constraint, and then create a new definition of foreign key constraints.

6.3, delete the foreign key constraint

  • The syntax is as follows:

    ALTER TABLE table_name
    DROP CONSTRAINT constraint_name[,..n]
    
    --例子
    USE TEST
    ALTER TABLE Pyc
    DROP CONSTRAINT FKEY_ID
    

Second, the creation and maintenance of relationships

  • SQL Server 2017 is a relational database management system, when the database contains multiple tables, you need to build relationships between tables by primary key, the coordination between the various tables work.

  • Relationship works by matching key data column, while the key columns are typically between two tables have the same column name, create a relationship between the data in the table may display a connection to another column of the table columns in a table.

  • There is a relationship between the table and the table

    1) one relationship

    2) many relationship

    3) many relationship

1, one to one relationship

  • Refers to a one to one relationship between records in Table A does have only one match and are recorded in Table B, respectively.
  • This relationship has the following characteristics
  • 1) dividing a table containing a number of columns.
  • 2) The isolation of a part of the table for security reasons.
  • 3) storage can easily delete temporary data, just delete the data table to delete data.
  • 4) applies only to store information master subset.
  • 5) If the two are related to the primary key column and having a unique constraint.

1.1, create a one to one relationship

  • 1) Start SQL Server Management Studio, and connect to the database in 2017 SQL Server
  • 2) selected need to create a database table relationships
  • 3) the need to establish the relationship between the selected data table, right click and choose "Design" command, open the table design window, right-click the ID field, the shortcut menu, select "relationship" command, open the "foreign key relationships" dialog
  • 4) Foreign Key Relationships dialog box, click the Browse button below conventional "table and column norms" text box, open the "Tables and Columns" dialog box, set the table in the dialog box and the main foreign key relationship between the name of
  • 5) In the "Tables and Columns" dialog box, click the OK button; foreign key relationship dialog box, click Close.
3251331-abf23781011b01bf.PNG
Tabulated relationship .PNG
  • Many, similar to the above method to create a relationship of many to many.

Reproduced in: https: //www.jianshu.com/p/58b1b875c2ca

Guess you like

Origin blog.csdn.net/weixin_33797791/article/details/91331686