携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第5天,点击查看活动详情
1、 DML 数据操纵语言
用于完成对数据表中的数据增加、删除、修改操作
1.1 插入
实战表格:
create table stus (
num char(8) primary key,
name varchar(20) not null,
gender char(2) not null,
age int not null,
tel char(11) not null unique,
qq varchar(11) unique
);
语法:
insert into 表名 (columnName,...) values (value1,value2...);
实际操作:
// 像表格中插入指定数据
insert into stus (num,name,gender,age,tel) values ('1234','zs','y',17,'1234567');
// 向表格中插入全部数据
insert into stus (num,name,gender,age,tel,qq) values ('1234','zs','y',17,'1234567');
// 或者
insert into stus values ('1234','zs','y',17,'1234567');
注意事项: 如果表格中字符约束不是not null ,可以插入也可以不插入,不插入系统会默认为null
向表格中插入所有列数据时,数据表名后面的额字段列表可以省略,但是不建议,保证数据库的稳定性
插入结果:
mysql> select * from stus;
+-----+------+--------+-----+-----------+------+
| num | name | gender | age | tel | qq |
+-----+------+--------+-----+-----------+------+
| 1 | aq | y | 18 | 123456789 | NULL |
+-----+------+--------+-----+-----------+------+
1 row in set (0.00 sec)
1.2 删除
从数据表中删除满足指定条件的所有记录
语法:
delete from 表名 [where conditions ];
实战:
delete from stus where num=1;
delete from stus where age>=18;
delete from stus;
注意: 如果没有where 条件判断,则表示删除该表中的所有记录,其他的合理使用where 条件判断就可以删除我们想要的记录了、
1.3 修改
对数据表中已经添加的记录进行删除
语法:
updata 表名 set columnName = value [where conditions];
实战:
update stsu set num = 1 where num = 7;
update stus set name = 'zs',gender = 'o' where num =10;
注意:与删除 相似,如果where不写的话,修改的是数据表中全部的字段的值,可以同时修改不同的字段,用逗号隔开即可
2. 日期函数和字符串函数(补充)
2.1 日期函数
还是在我们上面展示的stus表中,先添加一个日期函数 :alter table stus add time datetime;
使用方式:直接实战演示
方法一: 手动指定时间 格式必须为:'yyyy-MM-dd hh:mm:ss'
insert into stus (num,name,gender, age,tel,qq,time) values (4,'simth','x',31,19999,128734889,'2022-08-13 11:48:49');
方法二: 使用now()函数
insert into stus (num,name,gender, age,tel,qq,time) values (4,'simth','x',31,19999,128734889,now());
方法三:使用sysdate()函数
insert into stus (num,name,gender, age,tel,qq,time) values (6,'bob','x',19,177777,177777,sysdate());
运行结果:
mysql> select * from stus;
+-----+-------+--------+-----+----------+-----------+---------------------+
| num | name | gender | age | tel | qq | time |
+-----+-------+--------+-----+----------+-----------+---------------------+
| 10 | mazi | o | 19 | 12345678 | 999999 | NULL |
| 3 | mazi | x | 17 | 1234567 | 888888 | NULL |
| 4 | simth | x | 31 | 19999 | 128734889 | 2022-08-13 11:48:49 |
| 5 | tom | y | 29 | 18888 | 188788889 | 2022-08-13 11:54:13 |
| 6 | bob | x | 19 | 177777 | 177777 | 2022-08-13 11:55:22 |
+-----+-------+--------+-----+----------+-----------+---------------------+
5 rows in set (0.00 sec)
注意 :
- 手动添加:字符串的格式必须为 yyyy-MM-dd hh:mm:ss
- 如果获取当前系统时间添加到⽇期类型的列,可以使⽤ now() 或者 sysdate()
- #通过now和sysdate获取当前系统时间
select now();
select sysdate();
2.2 字符串函数
通过sql指令对字符串进行处理
2.2.1 concat 拼接
语法:
concat (colnum1,colunm2,...)拼接多列
select concat (num,'=====' ,name,gender,'---->',time) 'concat' from stus where num = 6;
可以 改变 字段的顺序,只要符合资源的基本数据类型,where 是条件,如果不添加指令哪儿一行进行拼接,默认将整张表进行拼接,拼接只是一个结果,并不会改变表格
2.2.2 upper(column) 将字段的值转换成大写
实例:
select upper(name) from stus;
2.2.3 lower(column) 将字段的值转换成大写
实例:
select lower(name) from stus;
提示: 字符串函数的使用并不会对原来的表格进行修改!
2.2.4 substring (column,start, len)截取字符串
语法:
substring (column,start, len)从指定的位置开始,字符串从1开始计数,start : 开始位置 ,len :结束位置 [ start , len ] ,都是闭区间
实例:
select name ,substring(tel,1,4) from stus ;
3. 总结:
感觉案例写多了,mysql 数据库的入门也不是很难,后面where 语句的判断与java中的判断有点像是,遇到一个问题,同一个人想出的同意中办法使用不同语言去解决的时候,只是语法不相同,思路都是一样的,时间函数、字符函数,中使用的方法都和 之前学过的有些相似,加油学,小伙子,贵在坚持!