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;
    ##删除外键
    

  • 相关阅读:
    python的dict和set
    python基础之dict和set
    python基础之条件判断和循环
    mongodb安装和配置,遇到问题和解决方法
    mybatis12--一级缓存
    mybatis11--多对多关联查询
    mybatis10--自连接多对一查询
    mybatis09--自连接一对多查询
    mybatis08--关联查询多对一
    mybatis07--关联查询一对多
  • 原文地址:https://www.cnblogs.com/Nayears/p/12133091.html
Copyright © 2011-2022 走看看