zoukankan      html  css  js  c++  java
  • 数据库约束学习

    #主键

    • primary key
    create table l1(id int(10) primary key,name char(10));
    
    alter table table_test drop primary key;
    #删除主键
    

    #多字段主键

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

    #唯一

    • unique
    create table l2(id int(10) primary key,name char(10) unique);
    #创建唯一约束
    alter table t drop index name;
    #删除非空约束
    alter table t modify id int unique;
    添加非空约束
    
    

    #联合唯一

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

    #非空

    • not null
    create table l3(id int(10) primary key,name char(10) unique not null);
    #创建非空约束
    alter table t modify name char(10) null;
    #去掉null约束
    alter table t modify name char(10) not null;
    #添加null约束
    

    #自增

    • auto_increment
    create table l4(id int(10) primary key auto_increment,name char(10) unique not null);
    

    ​ (unique auto_increment 自动设为主键)

    create table l5(id int(10) unique auto_increment,name char(10) unique not null);
    #unique auto_increment自动设为主键
    

    #设置默认值

    • default
    create table l6(id int(10) primary key auto_increment,name char(10) unique not null,age int(8) default "18");
    

    #外键

    • foreign key
    create table k1(id int(10) primary key,name char(14) not null);
    
    create table k3(k1_id int(10) primary key,age int(10) default "18",foreign key(k1_id) references k1(id) on update cascade);
    ## on update cascade  级连更新    ##on delete cascade 级连删除
    ##创建外键
    
    alter table book add constraint fk_id foreign key(press_id) references press(id);
    ##添加外键
    
    alter table book drop foreign key fk_id;
    ##删除外键
    

  • 相关阅读:
    利用锚点制作简单索引效果
    BOM之location对象
    引入CSS
    对象继承
    indexOf、instanceOf、typeOf、valueOf详解
    JSON详解
    浏览器兼容性-JS篇
    gcc堆栈排列的建议(译文)
    VLAN 学习
    DPDK KNI 接口2
  • 原文地址:https://www.cnblogs.com/Nayears/p/12133091.html
Copyright © 2011-2022 走看看