// 创建数据库
create database `test` default character set utf8 collate utf8_general_ci;
//删除数据库
drop database test;
//查看数据库
show create database test;
//使用数据库
use test;
//创建表
CREATE TABLE IF NOT EXISTS `user`(
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL DEFAULT '',
`pwd` VARCHAR(100) NOT NULL DEFAULT '',
PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
//删除表
drop table user;
// 清空表(数据清空,表结构保留)
truncate user;
//查看表
show create table user;
desc user;
// 添加新字段
alter table 表名 add 列名 类型
alter table user add sex tinyint(1) unsigned not Null default 1;
// 添加字段并指定位置
alter table 表名 add 列名 类型 after 列名;
alter table user add ip varchar(32) after pwd;
//修改字段类型
alter table 表名 modify 列名 类型;
alter table user modify sex varchar(10);
// 删除表字段
alter table 表名 drop 列名;
alter table user drop ip;
//修改指定的字段
alter table 表名 change 旧列名 新列名 字列类型
alter table user change sex school varchar(20);
//修改表名
alter table 表名 rename to 新表名
alter table user rename to user2;
//插入数据
insert into user (name, pwd) values ('test1', '123456');
批量插入数据
insert into user (name, pwd) values ('test2', '123456'), ('test3', '123456');
// 修改数据
update user set name='test4' , pwd='123' where id = 1;
//批量修改数据
update user set
name = CASE id
when 1 then 'test1'
when 2 then 'test5'
end,
pwd = CASE id
when 1 then '1234'
when 2 then '1234'
end
where id in(1, 2);
//删除数据
delete from user where id = 1;
//批量删除数据
// 加入limit防止误操作
delete from user where id in (2, 3) limit 2;
//查询数据
select
id,
name,
pwd
from user
where id = 5;
alter table user AUTO_INCREMENT=1;
//查看展示所有数据库
show databases;
//查看某个数据库中的所有表
show tables;
// 查看服务器状态信息
show status;
// 查看引擎
show engines;
// 查看设置的默认引擎
show variables like 'deafult_storage_engine';
//查看以‘deafult’为开头的变量
show variables like 'deafult%';
//查看授权用户的安全权限
show grants;
//查看所有信息
select * from 表名;
select * from user;
//查询表中列1,列2,列3...等字段的信息
select 列1,列2,列3... from 表名;
select id, name, pwd from user;
//查询数据表中该条件语句下所有信息
select * from 表名 where 条件;
select * from user where id = 1;
// like查询:查询列中以'X'开头的所有数据
select * from 表名 where 列 like 'X%';
select * from user where name like 'test%';
//between and 区间查询:查询在条件M和N之间的数据
select * from 表名 where 列 between 'M' and 'N';
select * from user where id between 1 and 5;
//in查询:查询列在固定条件M个N中的数据
select * from 表名 where 列 in (M,N);
select * from user where id in (1, 5);
//distinct去重查询
select distinct 列 from 表名;
select distinct pwd from user;
// 查看数据表中范围条件数据 >、<、=
select * from 表名 where 列1 >、<、= 值1;
select * from user where id > 1;
// or或查询:查询条件不同值的数据
select * from 表名 where 列 = 值1 or 列 = 值2;
select * from user where id = 1 or id = 2;
// count总数查询
select count(*) from 表名
select count(*) from user;
// sum求和查询
select sum(列) from 表名
select sum(id) from user;
// avg平均值查询
select avg(列) from 表名
select avg(id) from user;
// max最大值查询
select max(列) from 表名
select max(id) from user;
// min最小值查询
select min(列) from 表名
select min(id) from user;
// order by 排序查询:asc升序(默认) 和 desc降序
select * from 表名 order by 列 asc 、desc;
select * from user order by id asc;
// order by 多字段的排序
select * from 表名 order by 字段1 desc |asc,...字段n desc| asc;
select * from user order by id desc, name asc;
// group by 分组查询
select * from 表名 group by 列;
select * from user group by pwd;
// limit限制查询:查询前m行数据
select * from 表名 limit M;
select * from user limit 3;
// limit 分页查询:查询从M行开始下的N行数据
// limit计算:limit (n-1)*数量 ,数量
select * from 表名 limit M,N;
// 别名查询
select 列名 as 别名 from 表名;
select name as n from user;
(1)inner join 内连接或等值连接
取得同时符合A和B的一组数据
select * from A inner join B on A.name = B.name;
(2)left join 左连接
取得左表(table1)完全记录,即是右表(table2)并无对应匹配记录
// 推荐
select * from A left join B on A.name = B.name;
// 不推荐
select * from A left outer join B on A.name = B.name;
(3)right join 右连接
取得右表(table2)完全记录,即是左表(table1)并无匹配对应记录
select * from A right join B on A.name = B.name;
(4)union、union all全连接
mysql不支持full join ,全连接使用union来完成,推荐使用union all
select * from user union all select * from user3;