欢迎来到Felix的博客

雨恨云愁,江南依旧称佳丽。水村渔市。一缕孤烟细。天际征鸿,遥认行如缀。平生事。此时凝睇。谁会凭阑意
返回顶部

mysql数据库

数据库操作

创建数据库
create database if not exists xxx;
create database xxx character set utf8;

使用数据库
use xxx;

查看数据库列表
show databases;

修改数据库
alter database xxx character set gbk;

查询正在使用中的数据库
select database();

数据表操作

查看数据表结构
desc xxx;

复制表结构
create table xxx like xxx1; (不复制数据)

删除数据表 
drop table xxx; 
drop table if exists xxx;

重命名 
rename table xxx to xxx1;
alter table xxx rename xxx1;
alter table xxx rename to xxx1;

修改字符集
alter table xxx character set gbk;

最后位置插入列
alter table xxx add column col int not null;
alter table xxx add col int not null;

插入在某列后
 alter table xxx add col int after age;

插入首位 
alter table xxx add col int first;

删除某列 
alter table xxx drop xxx;
alter table xxx drop column xxx;

修改列
 alter table xxx change oxxx nxxx int not null; 

可重命名可修改属性
 alter table xxx modify xxx 属性;

数据操作

插入 
insert into 表名(x,xx,xxx) values(x,xx,xxx);
insert into 表名 values(x, xx, xxx);

删除数据
delete from 表 where xxx;
delete from 表;
truncate table xxx; 先删除表,再建一个一样的表,执行效率快

修改数据
update 表名 set a=1,b=2 where; 不加约束会修改所有数据

查询数据
查询
select
	字段列表
from
	表名列表
where
	条件列表
group by
	分组字段
having
	分组之后的条件
order by (默认升序, desc降序)
	排序
limit
	分页限定(sqlserver 可以用 select top 5 (percent))

基础查询
select distinct xxx as x from 表   去重,返回结果重命名

where 
	> 、< 、<= 、>= 、= 、<>(!=)
	* BETWEEN...AND  
	* IN( 集合) 
	* LIKE:模糊查询 
	* _:单个任意字符
	* %:多个任意字符
	* IS NULL  
	* and  或 &&
	* or  或 || 
	* not  或 !

JOIN
select * from A a left join B b on a.id = b.id;
select * from A a inner join B b on b.id = a.id;
select * from A a right join B b on a.id = b.id;
select * from A a full outer join B b on a.id = b.id;
INNER JOIN:如果表中有至少一个匹配,则返回行
LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行
RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行
FULL JOIN:只要其中一个表中存在匹配,则返回行

UNION
将表1和表二搜索结果串起来(会去除重复)(要求搜索结果的列数必须相同)
UNION ALL 不去重
select id from A union select id from B;

复制并创建表
create table xxx as select * from x;

复制信息
insert into XXX select from xxx;
posted @ 2021-02-02 13:58  felixtester  阅读(47)  评论(0)    收藏  举报