zoukankan      html  css  js  c++  java
  • msq_table's methods2

    
    
    -- 删除数据 自增长id被占用
    -- 清楚所有数据并重置id 1
    
    truncate table name;

    --
    主键(唯一) id int primary key; -- 主键内容不能重复,不能为空 create table test( id int primary key auto_increment, -- auto_increment 自增长 name varchar(10) not null, age char(5) );

    -- 删除 主键 有自增长需要先删除自增长 通过change改变该字段的自增长属性

    alter table student change id id int not null;

    alter table test drop primary key;

    -- 复合主键
    
    create table test(
        id int;
        name varchar(10) not null,
        age char(5) null,
        primary key(id,name,age)
    );
    
    -- 只要是张表 都有id字段 通常只要存在id字段 ,那么基本上是主键
    
    
    -- 自增 配合主键id使用 auto_increment
    create table test(
        id int primary key auto_increment;
        name varchar(10) not null,
        age char(5) null,
    );
    
    -- 唯一键 unique key
        -- 表里没有主键时,设置唯一键会显示主键的标志 PRI,但他本质还是唯一键
      
    -- 删除唯一键
    alter table test drop index `name`;  -- name为字段名字
    
    
    create table test(
        name varchar(10) unique key ,   -- name 不能重复 要求唯一了
        age char(5)
    );
    
    create table test(
        name varchar(10),   -- name 不能重复 要求唯一了
        age char(5),
        unique key 'uni_name'(name)
    );
    -- 外键 foreign key 关联两张表的

    -- 外键删除
    alter table foreign_key drop foreign key `外键名`;  -- 外键名通过查看建表过程

    
    

    外键的几种模式
    * `restrict` 默认
    * `cascade` 级联
    * 父表更新 子表更新 父表删除 子表删除
    * `set null` 置空
    * 父表更新 子表置空 父表删除 子表置空 (前提 支持为空)

     
    create database test;
    use test;
    
    create table student(
        id int primary key auto_increment,
        name varchar(20) not null,
        gender char(15) not null
        );
    
    create table course(
        id int primary key auto_increment,
        name varchar(20)
        );
    
    insert into student(name,gender) values('Which','male');
    insert into student(name,gender) values('Tuple','male');
    insert into student(name,gender) values('Rose','female');
    
    insert into course values(1,'Python');
    insert into course values(2,'Web');
    insert into course values(3,'Java');
    
    create table mark(
        id int,
        stu_id int,
        course_id int,
        score int not null,
        primary key(id,stu_id,course_id),
        constraint FK_ha foreign key(id) references course(id),  -- 设置外键的名字
        foreign key(course_id) references student(id)
        );
                                    -- 外键名字通过 show create table mark; 查看
    
    insert into mark values(1,1,1,99);
    insert into mark values(2,1,2,98);
    insert into mark values(3,2,1,60);
    insert into mark values(3,3,2,99);
    mysql> select * from student;
    +----+-------+--------+
    | id | name  | gender |
    +----+-------+--------+
    |  1 | Which | male   |
    |  2 | Tuple | male   |
    |  3 | Rose  | female |
    +----+-------+--------+
    3 rows in set (0.00 sec)
    
    mysql> select * from course;
    +----+--------+
    | id | name   |
    +----+--------+
    |  1 | Python |
    |  2 | Web    |
    |  3 | Java   |
    +----+--------+
    3 rows in set (0.00 sec)
    
    mysql> select * from mark;
    +----+--------+-----------+-------+
    | id | stu_id | course_id | score |
    +----+--------+-----------+-------+
    |  1 |      1 |         1 |    99 |
    |  2 |      1 |         2 |    98 |
    |  3 |      2 |         1 |    60 |
    |  3 |      3 |         2 |    99 |
    +----+--------+-----------+-------+
    4 rows in set (0.00 sec)
    
    
    
     
  • 相关阅读:
    为什么整个互联网行业都缺前端工程师?
    css3做的圆特效
    又一个前端的小渣渣诞生了
    返回顶部代码!
    网页动画的十二原则
    JQuery缓冲加载图片插件lazyload.js的使用方法
    关于写手机页面demo的准备工作
    HTML5加载动画
    HTML5加载动画
    正则表达式语法
  • 原文地址:https://www.cnblogs.com/tangpg/p/8094328.html
Copyright © 2011-2022 走看看