zoukankan      html  css  js  c++  java
  • mysql约束

    1.主键约束

       主键是表中一列或者多列的组合,不为空,要求数据唯一
    
        create table tb_emp3( id int, name varchar(25), tept varchar(25) ,primary key(id,name));
    
      (分为单组件约束和双组件约束,单组件约束一般为ID ,多主键约束一般为部分+姓名 确定唯一值)

    2.外键约束

    外键就是保证数据的完整性,一个表可以有多个外键,外键可以为空值,if not 每一个外键(可以不是本表的主键)必须对应关联表的主键,例如 部门表的主键是  id,在员工表中有一个键(deptid)与这个id相关联
    格式为:
    constraint 外键名称 foreign key 字段 123.。。
    reference 主表(字段 123,,n)
    constraint:约束
    foreign  :国外
    reference :参考
    
    mysql> create table dept01
        -> (
        -> id int(11) primary key,
        -> name varchar(22) not null,
        -> location varchar(50)
        -> );
    主表
    
    下表为从表
    mysql> create table test02
        -> (
        -> deptid int(11) primary key,
        -> name varchar(25),
        -> deptIDD int(11),
        -> salary float,
        -> constraint outkey_name foreign key(deptIDD) references dept01(id)
        -> );

    3.非空约束

    mysql> create table tb_emp6
        -> ( id int(11) primary key,
        -> name varchar(25) not null,
        -> deptId int(11),
        -> salary float
        -> );
    Query OK, 0 rows affected (0.11 sec)

    4.唯一性约束

    mysql> create table tb_dept2
        -> (
        -> id int(11) primary key,
        -> name varchar(22) unique,
        -> location varchar(50)
        -> );
    Query OK, 0 rows affected (0.18 sec)

    5.默认约束

    mysql> create table tb_emp7
        -> (
        -> id int(11) primary key,
        -> name varchar(25) not null,
        -> deptid int(11) default 1111,
        -> salary float
        -> );
    Query OK, 0 rows affected (0.09 sec)
    
    mysql> insert into tb_emp7(id,name) values(1,"fd");
    Query OK, 1 row affected (0.02 sec)
    
    mysql> select * from tb_emp7;
    +----+------+--------+--------+
    | id | name | deptid | salary |
    +----+------+--------+--------+
    |  1 | fd   |   1111 |   NULL |
    +----+------+--------+--------+
    1 row in set (0.00 sec)
    
    mysql> 

    6.主键自动增加

    mysql> create table tb_emp8
        -> (
        -> id int(11) primary key auto_increment,
        -> name varchar(25) not null,
        -> deptid int(11),
        -> salary float
        -> );
    Query OK, 0 rows affected (0.30 sec)
    
    mysql> insert into tb_emp8 (name,deptid) values ('ds',2),('fr',7),("ds",99);
    Query OK, 3 rows affected (0.02 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> select * from tb_emp8;
    +----+------+--------+--------+
    | id | name | deptid | salary |
    +----+------+--------+--------+
    |  1 | ds   |      2 |   NULL |
    |  2 | fr   |      7 |   NULL |
    |  3 | ds   |     99 |   NULL |
    +----+------+--------+--------+
    3 rows in set (0.00 sec)
    RUSH B
  • 相关阅读:
    IIS无法加载字体文件(*.woff,*.svg)的解决办法
    windows server 2012 r2 安装IIS失败
    ASP.NET Core文件上传、下载与删除
    VS2015打开特定项目就崩溃
    sql server优化思路
    Asp.net Core中使用Session
    ABP之动态WebAPI
    web.xml和@WebServlet
    同一个页面 andriod和ios设备上的按钮颜色不一致
    地址中如果含有"+",发给服务器时"+"变成了空格问题解析
  • 原文地址:https://www.cnblogs.com/tangsonghuai/p/11002035.html
Copyright © 2011-2022 走看看