Add constraints when creating a tableSummary

Table of contents

1. Non-empty constraints

Second, the only constraint

3. Primary key constraints

4. Self-incrementing column

5. Default Constraints

6. Check constraints

Seven, foreign key constraints


For the syntax of this article, please refer to the data integrity article on the homepage

1. Non-empty constraints

-- Add a non-null constraint when creating a table (ensure that the attribute of the current constraint is not a null value)

create table n(
s_id char(20) not null
);
-- 创建表时添加唯一约束unique
create table g(
s_name char(20) unique
);

create table g(
s_name char(20),
unique(s_name)
);

Second, the only constraint

-- Add a composite unique constraint unique when creating a table (add a unique constraint for two or more attributes so that their values ​​cannot be repeated at the same time, and there can be partial repetitions)

create table staff(
staff_id int,
staff_name varchar(20), 
staff_age int,
unique(staff_id,staff_name)
);

3. Primary key constraints


-- Add a primary key constraint primary key when creating a table (primary key constraint is a non-null constraint plus a unique constraint, which can only be used to constrain the primary key, so that the primary key can neither be empty nor unique. If there is no primary key in the data table, then the first A non-null and unique attribute
-- will automatically become the primary key

 

create table staff(
staff_id int primary key,
staff_name char(20)
);

4. Self-incrementing column

-- Add an auto-increment column when creating a table (an auto-increment column is when inserting data, if the value of the attribute is not specified, its value will be automatically increased by one according to the value of the previous attribute, when adding an auto-increment column to an attribute, This attribute is needed
-- there are already unique constraints, and generally only auto-increment is added to the primary key constraint, so you must first give a primary key constraint to an attribute, and then add an auto-increment column for it)

create table h(
h_id int primary key auto_increment
);
desc h;


5. Default Constraints

-- Add a default value constraint default when creating a table (if the value of the attribute is not given when inserting data, the attribute with the default value constraint will automatically use the given default value)
 

create table l(
h_id int default "3"
);
desc l;

6. Check constraints

-- Add a check constraint check when creating a table: (Check constraint is to check the value range of the attribute in the column to determine whether the value of the field is the specified value, if not, the insertion or modification of data is not allowed)
 

create table o(
s_id int check(s_id >"1" and s_id<"2")
);
desc o;
insert into  o(s_id) value("6");

Seven, foreign key constraints

-- Add foreign key constraints when creating a table (foreign key constraints refer to the attribute values ​​in one table refer to the main attribute values ​​in another table, that is, the foreign key values ​​refer to the main attribute values, because the foreign key constraints are table-level constraints, So before creating the reference table, create the referenced table
-- the foreign key of the reference table and the primary key of the referenced table create constraints)

constraint for_sdid foreign key(staff_did) references department_tb(d_id) -- 外键约束
-- for_sdid :我们创建的外键名
-- foreign key (staff_did) : 参考表的外键
-- references 参考

-- constraint: 约束


 

Guess you like

Origin blog.csdn.net/m0_65334415/article/details/130273707