DML
1 插入元组
- 在my_test数据库的students表中插入数据
先查看students表中的内容
use my_test
show tables;
select * from students;
插入元组
insert into students (sno, sname, age, sex, score, birth) values
(4, 'zl', 19, 'nv', 70, 20190101);
不是空表的话不用写 (sno, sname, age, sex, score, birth)
insert into students values
(4, 'zl', 19, 'nv', 70, 20190101);
- 分条显示
select * from students\G;
- 一次插入多个元组
insert into students (sno, sname, age, sex, score, birth) values
(5, 'wc', 3, 'nan', 20,20190203),
(6, 'lili', 20, 'nv', 90,20190801);
2 更新语句
- 把学生分数全部改成90
update students set score=90;
- 修改zs的分数为60
update students set score=60
where sname = 'zs';
- 同时修改多个字段
update students set age=30, score=70
where sname = 'ls';
- 在wc的年龄上加1岁
update students set age=age+1
where sname='wc';
- 退出当前用户
exit
或quit
所有用户信息都存在mysql数据库中
3 修改数据库密码
-- 数据库内
use mysql
-- before5.7
update user set password=password('123456')WHERE User='root';
-- after5.7
update user set authentication_string=password('123456') where user='root' and Host = 'localhost';
-- after 8.0(navicat 常因为密码格式不匹配,导致连接不上,此时用此语句修改密码)
alter user 'root'@'localhost' identified with mysql_native_password by '新密码';
-- 修改完密码后不需要刷新MYSQL的系统权限相关表
-- 数据库外
-- other way 通用8.0和5.7
mysqladmin -u root -p password 1234
4 删除操作
- 删除符合要求的语句
delete from students where sname='ww';
- 删除全部内容
delete from students;
- 删除原表创建新表结构
truncate table students;
delete 删除表中的数据,表结构还在;删除后数据可以找回
但是truncate删除是把表直接drop掉,然后再创建一个同样的新表。删除的数据不能找回,执行速度比delete快。