insert into 表名(字段名1,字段名2,字段名3...)values(值1,值2,值3);
字段名和值要一一对应(顺序对应,数据类型对应)
insert into t_student(no,name,sex,age,email) values(1,'Jack','b',12,'123456@123.com');
+------+------+------+------+----------------+
| no | name | sex | age | email |
+------+------+------+------+----------------+
| 1 | Jack | b | 12 | 123456@123.com |
+------+------+------+------+----------------+
insert into t_student(email,name,sex,age,no) values('lisi@123.com','lisi','f',20,2);
+------+------+------+------+----------------+
| no | name | sex | age | email |
+------+------+------+------+----------------+
| 1 | Jack | b | 12 | 123456@123.com |
| 2 | lisi | f | 20 | lisi@123.com |
+------+------+------+------+----------------+
insert into t_student(no) values(3);
+------+------+------+------+----------------+
| no | name | sex | age | email |
+------+------+------+------+----------------+
| 1 | Jack | b | 12 | 123456@123.com |
| 2 | lisi | f | 20 | lisi@123.com |
| 3 | NULL | NULL | NULL | NULL |
+------+------+------+------+----------------+
insert
语句只负责插入数据,不负责修改数据。
没有给其它字段指定值的话,默认值是NULL。
通过关键字default
可以设置字段的默认值,例如:
sex char(1) default 'm',
insert
语句中的字段名可以省略,但是values
中的值就必须全部写上,并且一一对应。
create table t_user(
id int(11),
name varchar(32),
birth date
);
insert into t_user values(123,'Jack',str_to_date('01-10-1990','%d-%m-%Y'));
使用str_to_date
函数将字符串类型转换成data
类型。
str_to_date('字符串日期','日期格式');
MySQL日期格式:
%Y 年// 注意这里Y大写
%m 月
%d 日
%h 时
%i 分
%s 秒
通常使用在插入insert
方面,因为插入的时候需要一个日期类型的数据,需要通过该函数将字符串转换成date
。
如果你提供的日期字符串是%Y-%m-%d
这个格式,str_to_date
函数就不需要了!!!
查询的时候可以使用date-format
函数将日期类型转化成特定格式的字符串。
date_format('日期类型数据', '日期格式')
mysql> select * from t_user;
+------+------+------------+
| id | name | birth |
+------+------+------------+
| 123 | Jack | 1990-10-01 |
| 124 | Lili | 1991-06-21 |
+------+------+------------+
默认的日期显示格式是%Y-%m-%d
自定义显示格式:
mysql> select id,name,date_format(birth,'%Y/%m/%d') as birth from t_user;
+------+------+------------+
| id | name | birth |
+------+------+------------+
| 123 | Jack | 1990/10/01 |
| 124 | Lili | 1991/06/21 |
+------+------+------------+
date
是短日期,只包括年月日
datetime
是长日期,包括年月日,时分秒
mysql短日期默认格式:%Y-%m-%d
mysql长日期默认格式:%Y-%m-%d %h:%i:%s
create table t_user(
id int,
name varchar(32),
birth date,
create_time datetime
);
insert into t_user(id,name,birth,create_time) values(123,'Tim','1990-10-01','2021-06-21 12:12:52');
mysql> select * from t_user;
+------+------+------------+---------------------+
| id | name | birth | create_time |
+------+------+------------+---------------------+
| 123 | Tim | 1990-10-01 | 2021-06-21 12:12:52 |
+------+------+------------+---------------------+
now()
函数可以获取系统的时间,并且是datetime类型的。
insert into t_user (id,name,birth,create_time) values(122,'Jan','2012-12-23',now());
mysql> select * from t_user;
+------+------+------------+---------------------+
| id | name | birth | create_time |
+------+------+------------+---------------------+
| 123 | Tim | 1990-10-01 | 2021-06-21 12:12:52 |
| 122 | Jan | 2012-12-23 | 2022-07-17 12:28:10 |
| 111 | Nik | 2011-06-23 | 2022-07-17 12:28:34 |
+------+------+------------+---------------------+
当然了,datetime
也是支持函数date_format
进行格式化显示的
mysql> select id,name,birth,date_format(create_time,'%h/%i/%s %Y-%m-%d')as create_time from t_user;
+------+------+------------+---------------------+
| id | name | birth | create_time |
+------+------+------------+---------------------+
| 123 | Tim | 1990-10-01 | 12/12/52 2021-06-21 |
| 122 | Jan | 2012-12-23 | 12/28/10 2022-07-17 |
| 111 | Nik | 2011-06-23 | 12/28/34 2022-07-17 |
+------+------+------------+---------------------+
update 表名 set 字段名1=新值1,字段名2=新值2,字段名3=新值3...where 条件
这里的where条件是用来判断修改哪一行的数据。
mysql> select * from t_user;
+------+------+------------+---------------------+
| id | name | birth | create_time |
+------+------+------------+---------------------+
| 123 | Tim | 1990-10-01 | 2021-06-21 12:12:52 |
| 122 | Jan | 2012-12-23 | 2022-07-17 12:28:10 |
| 111 | Nik | 2011-06-23 | 2022-07-17 12:28:34 |
+------+------+------------+---------------------+
update t_user set birth='2013-12-01',create_time=now() where id=123;
mysql> select * from t_user;
+------+------+------------+---------------------+
| id | name | birth | create_time |
+------+------+------------+---------------------+
| 123 | Tim | 2013-12-01 | 2022-07-17 12:58:55 |
| 122 | Jan | 2012-12-23 | 2022-07-17 12:28:10 |
| 111 | Nik | 2011-06-23 | 2022-07-17 12:28:34 |
+------+------+------------+---------------------+
如果没有了where条件,就是更新了所有。
update t_user set create_time=now();
mysql> select * from t_user;
+------+------+------------+---------------------+
| id | name | birth | create_time |
+------+------+------------+---------------------+
| 123 | Tim | 2013-12-01 | 2022-07-17 13:00:18 |
| 122 | Jan | 2012-12-23 | 2022-07-17 13:00:18 |
| 111 | Nik | 2011-06-23 | 2022-07-17 13:00:18 |
+------+------+------------+---------------------+
delete from 表名 where 条件;
如果没有条件,整个表都会被删除!!
delete from t_user where id=111;
mysql> select * from t_user;
+------+------+------------+---------------------+
| id | name | birth | create_time |
+------+------+------------+---------------------+
| 123 | Tim | 2013-12-01 | 2022-07-17 13:00:18 |
| 122 | Jan | 2012-12-23 | 2022-07-17 13:00:18 |
+------+------+------------+---------------------+
insert into t_user (id,name,birth,create_time) values
(1,'zs','1980-10-11',now()),
(2,'lisi','1981-10-11',now()),
(3,'wangwu','1982-10-11',now());
mysql> select * from t_user;
+------+--------+------------+---------------------+
| id | name | birth | create_time |
+------+--------+------------+---------------------+
| 1 | zs | 1980-10-11 | 2022-07-17 13:41:43 |
| 2 | lisi | 1981-10-11 | 2022-07-17 13:41:43 |
| 3 | wangwu | 1982-10-11 | 2022-07-17 13:41:43 |
+------+--------+------------+---------------------+
语法:
insert into t_user(字段名1,字段名2...) values
(),
(),
(),
();
create table emp2 as select * from emp;
mysql> show tables;
+------------------------+
| Tables_in_bjpowernnode |
+------------------------+
| dept |
| emp |
| emp2 |
| salgrade |
| t_student |
| t_user |
+------------------------+
mysql> select * from emp2;
+-------+--------+-----------+------+------------+---------+---------+--------+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
+-------+--------+-----------+------+------------+---------+---------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
原理是将一个查询结果当作一张新表,实现快速表的复制
create table mytable as select empno,ename from emp where job = 'MANAGER';
mysql> select * from mytable;
+-------+-------+
| empno | ename |
+-------+-------+
| 7566 | JONES |
| 7698 | BLAKE |
| 7782 | CLARK |
+-------+-------+
用的不多,了解就行了。
create table dept_bak as select * from dept;
mysql> select * from dept_bak;
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
insert into dept_bak select * from dept;
mysql> select * from dept_bak;
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
使用delete
不增加where
条件也能删除表中所有数据
delete from dept_bak;
mysql> select * from dept_bak;
Empty set (0.00 sec)
但是它可以回滚数据:
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from dept_bak;
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
8 rows in set (0.00 sec)
但是从底层逻辑上来说,delete是一个个来删除表中的数据,效率比较低。
另外一个快速删除表中所有数据的方法是truncate
truncate table 表名;
这个操作属于DDL
中的操作。
使用truncate
可以快速删除表中的所有数据,但是不支持数据回滚,因此要考虑再三再去使用它。
delete语句删除数据的原理?(delete属于DML语句!!!)
表中的数据被删除了,但是这个数据在硬盘上的真实存储空间不会被释放!!!
这种删除缺点是:删除效率比较低。
这种删除优点是:支持回滚,后悔了可以再恢复数据!!!
truncate语句删除数据的原理?
这种删除效率比较高,表被一次截断,物理删除。
这种删除缺点:不支持回滚。
这种删除优点:快速。
在DDL
中也有删除表的操作drop
,但是drop
是删除整个表,表和表的数据全部删除,truncate
删除的仅仅只是表中的所有数据,表的结构还在。