MySQL03_数据管理

文章目录

1 外键

  • 方式一:在创建表的时候增加外键约束

    create table if not exists `grade`
    (
        `gradeId`   int(10)     not null auto_increment comment '年级id',
        `gradeName` varchar(30) not null comment '年级名称',
        primary key (`gradeId`)
    ) engine = InnoDB
      default charset = utf8;
    
    -- constraint [FK_ID] foreign key(外键字段名) references 外表表名(主键字段名)
    create table if not exists `student`
    (
        `id`       int(4)      not null auto_increment comment '学号',
        `name`     varchar(3)  not null default '匿名' comment '姓名',
        `pwd`      varchar(20) not null default '123456' comment '密码',
        `sex`      varchar(2)  not null default '男' comment '性别',
        `birthday` datetime             default null comment '出生日期',
        `gradeId`  int(10)     not null comment '学生年级',
        `address`  varchar(100)         default null comment '家庭住址',
        `email`    varchar(50)          default null comment '邮箱',
        primary key (`id`),
        key `FK_gradeId` (`gradeId`),
        constraint `FK_gradeId` foreign key (`gradeId`) references `grade` (`gradeId`)
    ) engine = InnoDB
      default charset = utf8;
    

    在删除有外键关系的表时,必须先删除引用别的表的表(从表),再删除被引用的表(主表)

  • 方式二:创建表示不添加约束,之后添加

    create table if not exists `grade`
    (
        `gradeId`   int(10)     not null auto_increment comment '年级id',
        `gradeName` varchar(30) not null comment '年级名称',
        primary key (`gradeId`)
    ) engine = InnoDB
      default charset = utf8;
    
    create table if not exists `student`
    (
        `id`       int(4)      not null auto_increment comment '学号',
        `name`     varchar(3)  not null default '匿名' comment '姓名',
        `pwd`      varchar(20) not null default '123456' comment '密码',
        `sex`      varchar(2)  not null default '男' comment '性别',
        `birthday` datetime             default null comment '出生日期',
        `gradeId`  int(10)     not null comment '学生年级',
        `address`  varchar(100)         default null comment '家庭住址',
        `email`    varchar(50)          default null comment '邮箱',
        primary key (`id`)
    ) engine = InnoDB
      default charset = utf8;
    
    -- alert table 表名 add constraint FK_ID foreign key(外键字段名) references 外表表名(主键字段名)
    alter table `student`
        add constraint `FK_gradeId` foreign key (`gradeId`) references `grade` (gradeId);
    

以上的操作都是物理外键,属于数据库级别的外键,不建议使用!

2 DML语言

  • 插入语句(添加)

    -- insert into 表名([字段名1, 字段名2, 字段名3, ...]) values('值1', '值2', '值3', ...)
    insert into `grade`(`gradeId`, `gradeName`) values ('1', '初一');
    
    -- 由于主键自增,可以省略主键的插入
    insert into `grade`(`gradeName`) values ('高三');
    
    -- 如果不写表的字段,MySQL会一一匹配
    insert into `grade` values ('3', '大四');
    
    -- 插入多个字段
    insert into `grade`(`gradeName`) values ('大一'), ('大二');
    

    插入时字段可以省略,但后面赋值时必须要一一对应,不能省略任何值

  • 修改语句

    insert into `student`(`name`, `pwd`, `sex`)
    values ('张三', '111111', '男'),
           ('小红', 'abc', '女'),
           ('Bob', 'password', '男'),
           ('乌蝇哥', '114514', '男');
           
    -- 修改指定条件学生的名字
    update `student` set `name`='李四' where `id` = 1;
    
    -- 不指定条件的情况下,会改动所有列
    update `student` set `name`='匿名';
    
    -- 修改多个字段,其中value可以是定值,也可以是像current_time这样的变量
    update `student` set `name`='张三', `birthday`=current_time where `id` = 2;
    
  • 删除语句

    -- 不指定条件删除数据(避免这样写,会导致表中所有列数据被删除)
    delete from `student`;
    
    -- 删除指定数据
    delete from `student` where `id` = 3;
    
    -- 完全清空一个数据库表
    truncate `student`

    deletetruncate清空数据库的区别:

    • 它们都能删除数据,都不会删除表结构
    • truncate会重新设置自增列,即自增计数器会归零
    • truncate不会影响事务
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值