zoukankan      html  css  js  c++  java
  • 【mySQL】

    什么是主键?

    对于表中的每一行数据,都会有一个字段或一组字段,用于标识自己的唯一性,这样的一个或一组字段,就叫主键

    如果没有这个主键,那么对于表中的每一行的管理,会陷入混乱,我要更新某一特定行的数值,该怎么选择呢?

    主键需要满足什么条件?

    a. 主键要满足唯一性:任何两行数据,其主键必定不相同。

    b. 主键要满足非空性:主键如果为空,则无法起到标识此行的作用

    c. 主键不做修改和更新

    建表

    CREATE TABLE person_test(
        id bigint not null,
        name varchar(20) not null default '王自健',
        sex varchar(10) 
    );

    创建带有主键的表

    create table person_test(
        id      bigint      not null primary key,
        name    varchar(20) not null,
        sex     varchar(10) 
    )

    插入数据

    insert into person_test(id, name, sex) values(01, '海涛', 'boy')

    复合主键

    复合主键就是一组字段标识一行

    create table person_test(
        id      bigint      not null,
        name    varchar(20) not null,
        sex     varchar(10) ,
        primary key(id, name)
    )

    创建完后再决定主键 

    create table person_test(
        id      bigint      not null ,
        name    varchar(20) not null,
        time    timestamp   default current_timestamp
    );
    alter table person_test add primary key (id);

     

  • 相关阅读:
    Spring学习之旅(二)--容器
    Spring学习之旅(一)--初始Spring
    Logback的使用
    DES加解密工具类
    Lombok插件的使用
    from 表单用 GET 方法进行 URL 传值时后台无法获取问题
    组播
    linux头文件路径
    IANA
    6号板获取或放文件
  • 原文地址:https://www.cnblogs.com/Sinkinghost/p/9302666.html
Copyright © 2011-2022 走看看