創(chuàng)建數(shù)據(jù)庫:
create database 數(shù)據(jù)庫名;
顯示數(shù)據(jù)庫列表:
show databases;
刪除數(shù)據(jù)庫:
drop database 數(shù)據(jù)庫名;
用數(shù)據(jù)庫:
use 數(shù)據(jù)庫名;
顯示數(shù)據(jù)表結(jié)構(gòu):
desc 表名;
顯示數(shù)據(jù)庫里面的所有表:
show tables;
創(chuàng)建表:
create table 表名(列名 該列的數(shù)據(jù)類型 【是否設(shè)置主鍵】,
列名 該列的數(shù)據(jù)類型,
列名 該列的數(shù)據(jù)類型,
······
);
查找表:
select * from 表名;
模糊查詢:
select * from 表名 where 要查的列名 like ‘%關(guān)鍵字%’
刪除表:
drop table 表名;
向表中插入數(shù)據(jù):
insert into 表名 value( , , );(text類型用單引號)
刪除表中的一條數(shù)據(jù):
delete from 表名 where 范圍;
清空表:
delete from 表名;
修改表中的數(shù)據(jù):
update 表名 set 列名=要改的值 where 范圍;
排序:
select * from 表名 order by 列名 desc;
求做大值:
select max(列名) as maxvalue from 表名;
求最小值:
select min(列名) as minvalue from 表名;
求平均值:
select avg(列名) as avgvalue from 表名;
求和:
select sum(列名) as sumvalue from 表名;
總數(shù):
select count(*) as totalcount from 表名;
添加觸發(fā)器:
alter table 表名(小弟表) add foreign key (小弟表內(nèi)的字段) references 主鍵表(關(guān)聯(lián)的主鍵) on delete cascade on update cascade ;
添加外鍵關(guān)聯(lián):
CONSTRAINT '外鍵的名字'(隨意起的名字) FOREIGN KEY ('本表的字段名') REFERENCES 表名(要關(guān)聯(lián)的主鍵字段);