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

  • 相关阅读:
    golang与vscode的安装与配置
    numpy学习之前的必要数学知识:线性代数
    分布式系统设计系列 -- 基本原理及高可用策略
    微服务的4个设计原则和19个解决方案
    JAVA8 十大新特性详解
    ConcurrentHashMap总结
    Java NIO理解与使用
    深入了解 Java-Netty高性能高并发理解
    java内存泄漏的定位与分析
    Netty高性能编程备忘录(下)
  • 原文地址:https://www.cnblogs.com/Nayears/p/12133091.html
Copyright © 2011-2022 走看看