constraint

1 Data Integrity Feelings

(1) Entity integrity

    Entity integrity dictates that each row of a table is unique within the table. The indexes, UNIQUE constraints, PRIMARY KEY constraints and IDENTITY attributes defined in the entity table are the embodiment of entity integrity.

(2) Domain Integrity

    Domain integrity means that the columns in the database table must satisfy a certain data type or constraint, and the constraint includes the value range and precision. CHECK constraints, FOREIGN KEY constraints, DEFAULT definitions, NOT NULLs, etc. in the table all belong to the category of domain integrity.

(3) Referential integrity

    Referential integrity means that the data of the primary key and foreign key of the two tables should be consistent. It ensures the existence of rows corresponding to foreign keys of other tables in tables with primary keys, ensures data consistency between tables, and prevents data loss or meaningless data from spreading in the database. Referential integrity is created on the relationship between the foreign key and the primary key or between the foreign key and the unique key. With referential integrity, SQL Server will prevent users from doing the following;

<1> When there is no associated record in the main table, add or change the record to the related table;

<2> Change the value in the main table, which will cause orphan records in the related table;

<3> Delete the record from the main table, but there are still related records matching the record;

FOREIGN KEY and CHECK constraints can achieve referential integrity.

(4). User-defined integrity

    User-defined integrity refers to a set of rules specified by the user that does not belong to entity integrity, domain integrity, or referential integrity. All column-level and table-level constraints, stored procedures, and triggers in CREATE TABLE belong to user integrity.

2. Column constraints and table constraints

    For the database, constraints are divided into column constraints and table constraints. Among them, the column constraint only acts on the column itself as part of the column definition, and the table constraint can act on multiple columns as part of the table definition.

CREATE TABLE Goods(
   g_ID char(6),
   g_Name varchar(50),
   t_ID char(2) REFERENCES Types(t_ID), --column constraint
   g_Price float,
   g_Discount float,
   g_Number smallint,
   g_ProduceDate datetime DEFAULT '2007-07-01', --column constraints
   g_Image varchar(100),
   g_Status varchar(10),
   g_Description varchar(1000)
CONSTRAINT pk_gID PRIMARY KEY(g_ID) --table constraint

【hint】

  • Adding constraints can be created when the table is created using CREATE TABLE, or can be added when the table is modified using ALTER TABLE.
  • When adding constraints to ALTER TABLE, use the WITH NOCHECK option to not enforce constraints (CHECK and FOREIGN KEY) on existing data in the table.
  • When adding constraints to ALTER TABLE, use the NOCHECK option to prohibit the application of constraints (CHECK and FOREIGN KEY) when modifying and adding data.
  • Removing constraints can only be done using the ALTER TABLE statement.

3. Allow Null Constraints

    NOT NULL constraints can be created both when creating a table with CREATE TABLE and when modifying a table with ALTER TABLE.

CREATE TABLE Goods(
   g_ID char(6) NOT NULL,
   g_Name varchar(50)
)
ALTER TABLE Goods
ALTER COLUMN g_Name varchar(50) NOT NULL

4. DEFAULT definition

The format is as follows:

DEFAULT default value

    This can be achieved by using the NOT NULL keyword in the CREATE TABLE statement or ALTER TABLE statement.

CREATE TABLE Goods(
   g_ProduceDate datetime DEFAULT '2007-07-01',

【hint】

  • To modify a DEFAULT definition, you must first delete the existing DEFAULT definition and then recreate it.
  • The default value must match the data type of the column to which the DEFAULT definition is to be applied.
  • You can use the getdate() function to replace "2007-07-01", the , means that the system current date is used by default.

5. CHECK constraints

The format is as follows:

CONSTRAINT constraint name CHECK (expression)

    CHECK constraints can be defined using the T-SQL statements CREATE TABLE and ALTER TABLE.

ALTER TABLE Goods WITH NOCHECK
ADD CONSTRAINT ck_Discount
CHECK(g_Discount>=0.5 AND g_Discount<=1.0
)
【hint】

  • Constraints can also be specified at table creation time using the CREATE TABLE statement.
  • A CHECK constraint named ck_Discount can be dropped using "ALTER TABLE Goods DROP CONSTRAINT ck_Discount".
  • Multiple CHECK constraints can be applied to a single column.
  • A CHECK constraint can be applied to multiple columns by creating a CHECK constraint at the table level.
  • CHECK constraints do not accept values ​​that evaluate to False.
  • CHECK constraints are validated when executing add and modify record statements, and are not validated when executing delete record statements.

6. PRIMARY KEY constraints

The format is as follows:

CONSTRAINT constraint name PRIMARY KEY (column or combination of columns)

    A table usually has one or more columns that contain a value that uniquely identifies each row in the table. Such one or more columns are called the table's primary key (PK) and are used to enforce the entity integrity of the table. The PRIMARY KEY constraint is guaranteed by creating a unique index Specifies the entity integrity of the column. When using a PRIMARY KEY constraint, the column's null attribute must be defined as NOT NULL. PRIMAARY KEY constraints can be applied to one or more columns in a table.

CREATE TABLE Goods(
   g_ID char(6) PRIMARY KEY,
   g_Name varchar(50),
   t_ID char(2),
   g_Price float,
   g_Discount float,
   g_Number smallint,
   g_Image varchar(100),
)

    composite primary key

CREATE TABLE OrdderDetails(
   o_ID char(14),
   g_ID char(6),
   d_Price float,
   d_Number smallint
   CONSTRAINT pk_d_o_ID PRIMARY KEY(o_ID,g_ID)
)

【hint】

  • A table can have only one PRIMARY KEY constraint, and columns in the PRIMARY KEY constraint cannot accept null values.
  • If a PRIMARY KEY constraint is specified for a table, the SQL Server 2008 database engine enforces the uniqueness of the data by creating a unique index on the primary key column. This index can also be used for fast access to data when the primary key is used in a query.
  • If a PRIMARY KEY constraint is defined on a composite column, the value of one of the columns in the composite column may be duplicated, but any combination of values ​​from all columns in the PRIMARY KEY constraint definition must be unique.

7. FOREIGN KEY constraints

The format is as follows:

CONSTRAINT constraint name FOREIGN KEY (column) REFERENCES referenced table (column)

    FOREIGN KEY constraints provide referential integrity for one or more columns of data in a table, which restricts the value inserted into the constrained column in the table must already exist in the referenced table.

CREATE TABLE Goods(
   g_ID char(6) PRIMARY KEY,
   g_Name varchar(50),
   t_ID char(2) REFERENCES Types(t_ID),
   g_Price float,
   g_Discount float,
   g_Number smallint,
   g_ProduceDate datetime DEFAULT '2007-07-01',
   g_Image varchar(100),
   g_Status varchar(10),
   g_Description varchar(1000)

【hint】

  • The Types table must be created first.
  • The primary key based on the t_ID column in the Types table must be created.
  • When creating a FOREIGN KEY, the corresponding primary key constraint or UNIQUE constraint must be established.
  • A FOREIGN KEY constraint can not only be linked to another table's PRIMARY KEY constraint, it can also be defined as a UNIQUE constraint that references another table.
  • FOREIGN KEY constraints can also refer to columns in tables in the same database or columns in the same table.
  • The table contains no more than 253 FOREIGN KEY constraints, and no more than 253 FOREIGN KEY constraints refer to the table.
  • ON UPDATE CASCADE: Update
  • ON DELETE CASCADE: delete

8. UNIQUE constraint

The format is as follows:

CONSTRAINT constraint name UNIQUE (column or combination of columns)

CREATE TABLE Goods(
   g_ID char(6) PRIMARY KEY,
   g_Name varchar(50) UNIQUE,
   t_ID char(2) REFERENCES Types(t_ID),
   g_Price float,
   g_Discount float,
   g_Number smallint,
   g_ProduceDate datetime DEFAULT '2007-07-01',
   g_Image varchar(100),
   g_Status varchar(10),
   g_Description varchar(1000)

【hint】

  • Both UNIQUE constraints and PRIMARY KEY constraints are used to enforce uniqueness. PRIMARY KEY constraints automatically use UNIQUE constraints, but only on primary key columns. Instead, use UNIQUE constraints to implement unique constraints on non-primary key columns or columns that allow nulls.
  • A table can define multiple UNIQUE constraints, but only one PRIMARY KEY constraint.
  • UNIQUE constraints can be defined on columns that allow null values, but PRIMARY KEY constraints cannot be defined.
  • FOREIGN KEY constraints can also reference UNIQUE constraints.

    To add column constraints with the ALTER TABLE statement, use the ADD keyword.

ALTER TABLE Goods ADD UNIQUE(g_Name)
    To drop a UNIQUE constraint with the ALTER TABLE statement, use the DROP keyword.

ALTER TABLE Goods DROP UNIQUE(g_Name)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326449520&siteId=291194637