• MySQL中的六个约束


    主键约束 primary key:一个表中只能有一个主键,主键值不能为空,不能重复

                                       设置主键约束:1 在创建表时就设置好主键约束

                                                                   create table person(id int primary key);

                                                             或  create table person(id int ,name char(10),primary key(id));

                                                                2 通过修改表中列的属性来设置主键约束

                                                                  alter table person add primary key(id);

                                                            或  alter table person modify id int primary key;

                                       设置联合主键:1:在创建时就设置为联合主键约束

                                                                       create table person(id int,name char(10),primary key(id,name));

                                                                2:修改列的属性设置联合主键约束 

                                                                       alter table person add primary key(id,name);

                                       不同记录的联合主键的列值不能同时相同,可以分别相同,不能同时为空

                                       删除主键约束:alter table person drop primary key;

    递增约束 auto_increment:设置为自动递增的列,自己没给他添加值,系统会自动给他赋值,一般把主键设为递增约束

                                                create table student(id int primary key auto_increment);

    唯一约束 unique:设置为唯一约束的列值不能重复

                               

                                     设置唯一约束:  1 在创建表时就设置好唯一约束

                                                                   create table person(id int unique);

                                                             或  create table person(id int ,name char(10),unique(id));

                                                                2 通过修改表中列的属性来设置唯一约束

                                                                  alter table person add unique(id);

                                                            或  alter table person modify id int unique;

                                       设置联合唯一约束:1:在创建时就设置为联合唯一约束

                                                                       create table person(id int,name char(10),primary key(id,name));

                                                                       2:修改列的属性设置联合唯一约束 

                                                                       alter table person add unique(id,name);

                                       不同记录的联合唯一约束的列值不能同时相同,可以分别相同,不能同时为空

                                       删除主键约束:alter table person drop unique;

                              

    默认约束 default:设置某个列在没有记录时使用默认值

    create table person (id int,name char(10) default 'zhangsan');

    非空约束 not null:非空约束的列值不能为空

     create table person(id int,name char(10) not null);

    外键约束 foreige key   references:副表被约束的列值从主表约束列值中选择,主表约束的列值被副表列值使用时,这个主表的列值不能被删除。主表的列必须是主键;

     create table person(id int primary key);

     create table student(id int,foreign key(id) references person(id));

  • 相关阅读:
    Ext checkbox
    Ext4.1 grid 多选(可无checkbox)
    System.Web.HttpContext.Current.Session获取值出错
    Ext4.1 tree grid的右键菜单
    javascript innerHTML、outerHTML、innerText、outerText的区别
    EXT ajax简单实例
    C# 各版本更新简介
    C#与.Net Framework的各种版本和联系
    Ext4 简单的treepanel
    Ext4.1 Grid 分页查询
  • 原文地址:https://www.cnblogs.com/zhangyang4674/p/11600362.html
Copyright © 2020-2023  润新知