MYSQL基础知识之DML

数据库备份与还原

  • 备份

 mysqldump.exe -h localhost -P 3306(端口号)  -u root  -p 库名 > E:/库名20240719.sql
  • 还原

 mysql.exe -h 106.55.169.91 -P 3306 -u root -p haha < E:/xiao2.sql

数据表的新增

 insert into 表名 (字段名,字段名,....,字段名)  values/value (值,值,...,值);
全字段的插入(2个方式)
  •  insert into 表名 (字段1,字段2) values(字段1值,字段2值);
  •  insert into 表名 values(字段1值,字段2值);
 -- 方式一
 insert into student (sid,sname,birthday,ssex,classid)
 values(9,'张三','2007-1-1','男',1);
 -- 方式二
 insert into student values(null,'李四','1989-1-1','男',2);
 insert into student values(default,'李四','1989-1-1','男',2);
 ​
 -- 部分字段插入
 insert into student(sname,ssex) values ('齐同学','女');
 ​
 -- 给性别设置了非空约束,非空约束不写值就要写一个default
 alter table student modify ssex varchar(10) not null;
 -- 非空约束不写值就要写一个default,
 --否则会报错: 1364 - Field 'ssex' doesn't have a default value
 insert into student(sname) values('王小熊');
 ​
 alter table student modify ssex varchar(10) not null default '保密';

一次性添加多条数据(3种)
  •  insert into 表名 (字段名..) values(值..),(值..)...
 -- 方式一(常用)
 -- insert into 表名 (字段名..) values(值..),(值..)...
 insert into student(sname,ssex)
 VALUES('杨月','男'),('周晓','女'),('冯顺','男');
 ​
 -- 方式二(不常用)
 -- insert into select
 -- 插入和被插入的表都必须存在
 create table newstu(
     xingming varchar(10),
     xingbie varchar(10),
     classid int
 );
 insert into newstu(xingming,xingbie,classid)
 select sname,ssex,classid from student;
 ​
 -- 方式三(不常用)
 -- create table select
 -- 被插入的表不能存在
 -- 被插入的表没有任何约束
 ​
 create table stu1
 select sid,sname,birthday from student;
 ​

数据表的修改

  update 表名 set 字段名=值,字段名=值,....,字段名=值
  [where 子句条件]
  -- where子句 中的条件是对表中的每一条数据进行判断,
  -- 判断成立该数据的父句执行,
  -- 判断不成立该数据的父句不执行
 update stu1 set birthday='2002-2-2'
 where sname='李云';
 ​
 -- 修改newstu表中xingbie不等于女的人classid设为300
 update newstu set classid=30 where xingbie <> '女';

 -- 修改newstu表中xingbie不等于男的人classid设为200
 update newstu set classid=200 where xingbie != '男';

 -- 修改newstu表中classid 小于260的,将其性别改为保密
 update newstu set xingbie='保密' where classid < 260; 

 -- 修改newstu表中classid在30至60之间的人,将其性别改为外星人
 update newstu set xingbie='外星人' 
 where classid >= 30 and classid <= 60;

 -- 30 50 70 他们的性别变为地球人
 update newstu set xingbie='地球人'
 where classid = 30 or classid = 50 or classid =70; 

数据表的删除

 delete from 表名 [where子句]
 //删除stu1表中sid为1的
 delete from stu1 where sid=1;

清空表

 truncate 表名

drop、delete、truncate区别

delete只删数据

drop不仅把数据删掉,还删除了索引,表结构也删了

truncate不仅把数据删掉,还删除了索引

  • 13
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值