今天看了看之前收藏的一篇 mysql 的文章,扫了一眼是关于语法的。本来不想看的,后来一想,以后我可能会常用到 mysql,还是看看吧。结果一看发现好多不会的,到现在还是有一些不是很确定的,用到了再搜吧,看一遍就记住不太实际,也没必要。所以先随便记一下。
mysql -h addr -P port -u name -p password
show processlist 显示线程
show variable 显示系统变量
select database(); 查看当前数据库
select now(), user(), version();
show databases like ‘xxx’;
show create database xxx character set xxx;
create table xxx … charset=xxx;
show engines;
show engine xxx logs/status;
auto_increment = xxx 起始自增行数
data directory = ‘xxx’
index directory = ‘xxx’
comment = ‘xxx’;
看表结构
show create table …
desc/describe/explain/show columns from TABLENAME like ‘xxx’;
show table status [from DBNAME] like ‘xxxx’;
修改表:
alter table xx engine=MYISAM;
rename table xxx to xxx; 可以转移到另一个库,指定库名
alter table xxx
add xxx xxx-type
after xxx
first
add primary key(xxx)
add unique [name] (xxx)
add index [name] (xxx)
drop [column] xxx
modify [column] xxx xxx-type // 不能改字段名
change [column] old new new-type // 可以改名和类型
drop primary key
drop index xxx
drop foreign key xxx
drop table [if exists] xxx
create table xxx like xxxx
create table xxx [as] select xxxxxxxx
INET_ATON(‘127.0.0.1’) 将IP转为整型
INET_NTOA(2130706433) 将整型转为IP
truncate 重置auto_increment
mysqldump db_name talbes
–database db1 db2
–all-database
-uname -ppassword db_name tablename > filename
-uname -ppassword db_name tb1 tb2 > filename
–lock-all-tables --database xx > filename
-w where条件
source filename
mysql -uname -ppassword dbname < filename
视图:简化业务逻辑,对客户端隐藏真实的表结构
事务:
ddl不能回滚,事务不能嵌套
begin/start transaction 开启 commit/rollback结束
savepoint xxx/rollback to xxx/release savepoint xxx; 回滚点
set autocommit=0/1
delete from a left join b using xxx where xxx
insert into xxx values(xxx) on duplicate key update xxxx
xxx set xxx=xxx
导入导出单表数据
select * into outfile file_path options from xxx;
load data [local] infile [replace/ignore] xxxx options; // 如何处理重复数据
options:
fields
terminated by xxx
enclosed by xxx
escaped by xxx
using 必须同名字段,natural join 是自己找同名字段
子查询,
select * from (select xxx) a
表型子查询,生成一个临时表,会释放原表锁定的数据,必须有别名
select * from a where id = (select * from a)
标量子查询,不用起别名,不生成临时表
select *from a where id in (select * from a) 列子查询
select * from a where (id, name) in (select id, name from a) 行子查询
特殊:!= all/= some()/!=some()
1NF 字段不能再分
2NF 不能出现部分依赖,无联合主键即可
3NF 不能出现传递依赖,某字段依赖主键,另一字段依赖改字段,是传递依赖
select *from table1 use index (key1,key2) where xxx
select *from table2 ignore index(key1) where xxx
lock tables xx as xx;
unlock tables;
having 必须引用 group by 中或合计函数中的列;
where中不能用别名,having是把数据查出来再弄它,所以可以用别名
insert into a vlaues(default, xx) // 强制插入默认值
alter table xx auto_increment=x // 改他妈的初始值
外键,存在外键的表成为从表,指向的表为主表,
alter table xx add constraint xxxx foreign key(xx) references t2(id) on delete cascade/set null/restrict on update …
optimize [local/no_write_to_binlog] table xxx,xxx
repair [local/no_write_to_binlog] table xxx [quick][extend]
analyze [local/no_write_to_binlog] table xxx
[label:] repeat
sql;
until condition;
end repeat [label];
[label:] while condition do
sql;
end while [label];
if condition then
sql;
elseif condition then
sql;
else
sql;
end if;
create function xx() returns type deterministic/reads sql data/no sql
begin …