数据操作语言(DML)包括:INSERT、DELETE、UPDATE。
INSERT
-- 语法格式
INSERT INTO 表名(字段名1,字段名2,字段名3...) VALUES(值1,值2,值3...);
注意:字段名和值要一一对应。数量要对应,数据类型要对应。
-- 创建一个学生表
学号、姓名、年龄、性别、邮箱地址
DROP TABLE IF EXISTS t_student;
CREATE TABLE t_student(
num INT,
name VARCHAR(32),
age INT(3),
sex CHAR(1),
email VARCHAR(255)
);
DESC t_student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| num | int | YES | | NULL | |
| name | varchar(32) | YES | | NULL | |
| age | int | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| email | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+--+
-- 插入普通数据
INSERT INTO t_student(num,name,age,sex,email) VALUES(1,'zhangsan',18,'男','zhangsan@123.com');
INSERT INTO t_student(num) VALUES(3);
+------+----------+------+------+------------------+
| num | name | age | sex | email |
+------+----------+------+------+------------------+
| 1 | zhangsan | 18 | 男 | zhangsan@123.com |
| 3 | NULL | NULL | NULL | NULL |
+------+----------+------+------+------------------+
INSERT INTO t_student(name) VALUES('wangwu');
+------+----------+------+------+------------------+
| num | name | age | sex | email |
+------+----------+------+------+------------------+
| 1 | zhangsan | 18 | 男 | zhangsan@123.com |
| 3 | NULL | NULL | NULL | NULL |
| NULL | wangwu | NULL | NULL | NULL |
+------+----------+------+------+------------------+
注意: INSERT语句但凡是执行成功了,那么必然会多一条记录。没有给其它字段指定值的话,默认值是NULL。
INSERT中的字段名可以省略。
INSERT INTO t_student VALUES(2,'lisi','女',20,'lisi@123.com');
等价于全部都写上
INSERT INTO t_student(num,name,age,sex,email) VALUES(2,'lisi','女',20,'lisi@123.com');
-- 插入日期
数字格式化:FORMAT(数字,'格式')
SELECT ename,sal FROM emp;
+--------+---------+
| ename | sal |
+--------+---------+
| SMITH | 800.00 |
| ALLEN | 1600.00 |
| WARD | 1250.00 |
| JONES | 2975.00 |
| MARTIN | 1250.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
| TURNER | 1500.00 |
| ADAMS | 1100.00 |
| JAMES | 950.00 |
| FORD | 3000.00 |
| MILLER | 1300.00 |
+--------+---------+
SELECT ename,FORMAT(sal,'$999,999') AS sal FROM emp;
+--------+-------+
| ename | sal |
+--------+-------+
| SMITH | 800 |
| ALLEN | 1,600 |
| WARD | 1,250 |
| JONES | 2,975 |
| MARTIN | 1,250 |
| BLAKE | 2,850 |
| CLARK | 2,450 |
| SCOTT | 3,000 |
| KING | 5,000 |
| TURNER | 1,500 |
| ADAMS | 1,100 |
| JAMES | 950 |
| FORD | 3,000 |
| MILLER | 1,300 |
+--------+-------+
STR_TO_DATE:将字符串 VARCHAR类型转换成 DATE类型。
STR_TO_DATE('字符串日期','日期格式')
DROP TABLE IF EXISTS t_user;
CREATE TABLE t_user(
id INT,
name VARCHAR(32),
birth DATE,
create_time DATETIME
);
DESC t_user;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(32) | YES | | NULL | |
| birth | date | YES | | NULL | |
| create_time | datetime | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
INSERT INTO t_user(id,name,birth) VALUES(1,'zhangsan',STR_TO_DATE('01-10-1990','%d-%m-%Y'));
+------+----------+------------+-------------+
| id | name | birth | create_time |
+------+----------+------------+-------------+
| 1 | zhangsan | 1990-10-01 | NULL |
+------+----------+------------+-------------+
如果你提供的日期字符串是%Y-%m-%d格式,则无需STR_TO_DATE函数转换。
DATE_FORMAT:将 DATE类型转换成具有一定格式的 VARCHAR字符串类型。
DATE_FORMAT(日期类型数据,'日期格式')
SELECT id,name,DATE_FORMAT(birth,'%m/%d/%Y') AS birth FROM t_user;
+------+----------+------------+
| id | name | birth |
+------+----------+------------+
| 1 | zhangsan | 10/01/1990 |
+------+----------+------------+
SELECT id,name,birth FROM t_user;
+------+----------+------------+
| id | name | birth |
+------+----------+------------+
| 1 | zhangsan | 1990-10-01 |
+------+----------+------------+
这个SQL语句实际上是进行了默认的日期格式化,自动将数据库中的 DATE类型转换成 VARCHAR类型。并且采用的格式是MySQL默认的日期格式:'%Y-%m-%d'
-- 插入多条数据
INSERT INTO 表名(字段名1,字段名2) VALUES(),(),();
INSERT INTO t_user(id,name,birth,create_time) VALUES(1,'zhangsan','1980-11-1',NOW()),(2,'lisi','1988-04-01',NOW()),(3,'wangwu','2022-3-18',NOW());
+------+----------+------------+---------------------+
| id | name | birth | create_time |
+------+----------+------------+---------------------+
| 1 | zhangsan | 1980-11-01 | 2022-02-14 09:28:44 |
| 2 | lisi | 1988-04-01 | 2022-02-14 09:28:44 |
| 3 | wangwu | 2022-03-18 | 2022-02-14 09:28:44 |
+------+----------+------------+---------------------+
-- 查询结果插入到一张表
CREATE TABLE dept_bak AS SELECT * FROM dept;
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
INSERT INTO dept_bak SELECT * FROM dept;
+--------+------------+----------+
| 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 |
+--------+------------+----------+