约束

    科技2024-05-07  97

    约束是指对表中数据进行限定,保证数据完整性

    分类

    1.主键约束:primary key

       非空唯一

       一张表只能有一个主键

    删除主键:alter table stu drop primary key

        

    2.非空约束:not null

        一列的值不能为空 

       创建表时约束:

       创建后添加非空约束;

    3.唯一约束:unique 

        删除唯一约束:alter table stu drop index 键

       添加唯一约束:alter table 表名 modify 键 属性 unique

    4.外键约束:foreign key

       constraint 外键名称  foreign key 外键列名称 references 主表名称(主表列名称)

      alter table 表名 drop foreign key 外键名称

      alter table 表名 add    constraint 外键名称  foreign key 外键列名称 references 主表名称(主表列名称)

        级联操作: 

        添加外键时设置级联更新  on update cascade

        添加外键时设置级联删除   on delete cascade

    5.自动增长;

         auto_increment

    Processed: 0.011, SQL: 8