一:插入语句
方式一
语法
表名
列名
新值
insert into 表名(列名)
value(值);
注:有些字符集不能插入汉字,utf8可以
实例
INSERT INTO beauty(id,NAME,sex,borndate,phone,photo,boyfriend_id)
VALUES(13,'唐艺昕','女','1990-4-23','19888888888',NULL,2);
1 queries executed, 1 success, 0 errors, 0 warnings
查询:insert into beauty(id,name,sex,borndate,phone,photo,boyfriend_id) values(13,'唐艺昕','女','1990-4-23','19888888888',null,2)
共 1 行受到影响
执行耗时 : 0.002 sec
传送时间 : 0 sec
总耗时 : 0.003 sec
#
若执行
INSERT INTO beauty(id,NAME,sex,borndate,phone,boyfriend_id)
VALUES(13,'唐艺昕','女','1990-4-23','19888888888',2);
与之前结果一样,photo列默认为null
#
列的顺序可以调整
列和值的个数必须一致
可以省略列名,默认所有列,且列的顺序和表中的列的顺序一致
方式二
语法
insert into 表名
set 列名=值,列名=值,...;
实例
mysql> insert into beauty
-> set id=19,name='刘涛',phone='888';
Query OK, 1 row affected (0.00 sec)
没写的列为默认值
方式一支持一次插入多行,方式二不支持
方式一支持子查询,方式二不支持
insert into beauty
values(13,'唐艺昕1','女','1990-4-23','19888888888',null,2),(14,'唐艺昕2','女','1990-4-23','19888888888',null,2),(15,'唐艺昕3','女','1990-4-23','19888888888',null,2);
insert into beauty(id,name,phone)
select 26,'宋希','159875364';
insert into beauty(id,name,phone)
select id,boyname,'159875364'
from boys where id<3;
二:修改语句
1、修改单表的记录
语法
update 表名
set 列=新值,...
where 条件
3、修改多表的记录
语法
SQL92语法
update 表1 别名,表2 别名
set 列=值,...
where 连接条件
and 筛选条件
SQL99语法
update 表1 别名
inner|left|right join 表2 别名
on 连接条件
set 列=值,...
where 筛选条件;
实例
mysql> update beauty set phone='123456789'
-> where name like '唐艺昕_';
Query OK, 3 rows affected (0.00 sec)
mysql> update boys set boyname='鹿晗',userCP=800 where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
UPDATE boys b
INNER JOIN beauty bo ON bo.boyfriend_id=b.id
SET bo.phone='114'
WHERE b.boyName='张无忌';
三:删除语句
方式一
语法
单表删除
delete from 表名 where 筛选条件
多表删除
delete 表 别名 from 表1 别名,表2 别名
where 连接条件 and 筛选条件
SQL99
delete 表1或表2 别名
from 表1 别名
inner|left|right join 表2 别名
on 连接条件
where 筛选条件
方式二
truncate table 表名
只能删除表的所有内容
#比较
1、delete可以加where筛选条件,truncate不能加
2、可以使用truncate时效率比delete高一点
3、假如要删除的表中有自增长列
如果用delete删除后,再插入数据,自增长列的值从断电开始
而truncate删除后,再插入数据,自增长列的值从1开始
4、truncate删除没有返回值,delete删除有返回值
5、truncate删除不能回滚,delete删除可以回滚
实例
mysql> delete from beauty where phone like '%9';
Query OK, 6 rows affected (0.00 sec)
#多表删除
mysql> delete b
-> from beauty b inner join boys bo
-> on bo.id=b.boyfriend_id
-> where bo.boyName='张无忌';
Query OK, 3 rows affected (0.00 sec)
DELETE b,bo
FROM beauty b INNER JOIN boys bo ON b.`boyfriend_id`=bo.`id`
WHERE bo.`boyName`='黄晓明';
四:库和表的管理
一、库的管理
创建、修改、删除
二、表的管理
创建、修改、删除
创建:create
修改:alter
删除:drop
一:库的管理
1、库的创建
不需要进入某个数据库里,直接创建
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database books;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| books |
| db_mrks |
| first |
| girls |
| myemployees |
| mysql |
| performance_schema |
| sys |
| test |
| university |
+--------------------+
11 rows in set (0.00 sec)
mysql> create database if not exists books;
Query OK, 1 row affected, 1 warning (0.00 sec)
不存在时创建
2、库的修改
修改字符集
mysql> alter database books character set gbk;
Query OK, 1 row affected (0.00 sec)
3、库的删除
mysql> drop database books;
Query OK, 0 rows affected (0.00 sec)
mysql> drop database if exists books;
Query OK, 0 rows affected, 1 warning (0.00 sec)
存在时删除
二:表的管理
1、表的创建
mysql> create table book(
-> id int,
-> bname varchar(20),
-> price double,
-> authorid int,
-> publicdate datetime);
Query OK, 0 rows affected (0.02 sec)
2、表的修改
修改列名
mysql> ALTER TABLE book CHANGE COLUMN publishdate pubdate DATETIME;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
修改列的类型或约束
修改数据类型
mysql> alter table book modify column pubdate timestamp;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
添加新列
mysql> alter table author add column salary double;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
删除列
mysql> alter table author drop column salary;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
修改表名
mysql> alter table author rename to book_author;
Query OK, 0 rows affected (0.01 sec)
3、表的删除
drop table book_author;
4、表的复制
INSERT INTO book_author(id,au_name,nation) VALUES
(1,'莫言','中国'),
(2,'村上春树','日本'),
(3,'冯唐','中国'),
(4,'金庸','中国');
1、仅仅复制表的结构
CREATE TABLE copy1 LIKE book_author;
2、复制表的结构+数据
CREATE TABLE copy2
SELECT * FROM book_author;
3、只复制部分列、部分数据
CREATE TABLE copy3
SELECT id,au_name
FROM book_author
WHERE nation='中国';
4、只复制部分列不含数据
create table copy4
select id,au_name
from book_author
where 1=2;