• [mysql]约束


    #约束分类

    关键字:CONSTRAINT、REFERENCES

    #primary key: 主键:用于约束表中的一行,作为这一行的唯一标识,通过主键就能定位到一行。主键不能重复且不能为空
    create table employee(id int(10) primary key);
    create table employee(id int(10), constraint dpt_pk primary key(id));//主键自定义
    create table employee(id int(10), uname char(20), constraint pro_pk primary key(id,char));//复合主键

    #default: 默认值: 当有default约束的列,插入数据为空时,将使用默认值
    create table employee(id int(10) primary key, age int(10) default'10');//在使用insert语句时可以体现。如果default位没有值。那么这个位置的值被default的值填充。
    insert into employee(id) value(1);//id=1时,age=10
    insert into employee(id, age) value(2, 15);//id=2时,age=15

    #unique: 唯一:规定表中指定一列的值必须不能有重复。即这一列中每个值都是唯一的。
    create table employee(id int(10) primary key, unmae char(20), age int(10) default'10',
    phone int(20), unique(phone));

    #foreign key: 外键:一个表可以有多个外键,每个外键必须references另一个表的主键,被外键约束的列,取值必须在它references的列有对应值。
    constraint emp_fk foreign key(in_dpt) references department(dpt_name)
    # 外键名(自定义) 外键 department表dpt_name列
    #当前表中的in_dpt列定义为外键,自定义名称为emp_fk,它是department表中的dpt_name列。在对in_dpt赋值时,要参考department表中的主键。即department的in_dpt的值对应该的行的primary key的值不能为空。


    #not null: 非空:被非空约束的列,在插入值时必段为非空
    create table employee(id int(10) primary key, uname char(20) not null);
    insert into table employee value(1, 'an');

    快乐生活每一天
  • 相关阅读:
    我来说说博客评论的事
    SWFUpload+Javascript仿163邮件上传文件
    如何暂停和终止线程
    分享我的数据处理类库,欢迎拍砖
    求数列两两之差,再求和
    poj 1006 中国剩余定理
    Poj算法做题顺序
    poj 1328
    ZOJ 3279
    poj 2352 树状数组
  • 原文地址:https://www.cnblogs.com/sunner/p/8659576.html
Copyright © 2020-2023  润新知