1.创建数据库:create database 数据库名 ;
2.删除数据库:drop database 数据库名 ;
3.查看数据库:show databases ;
4.查看所有表:show tables ;
5.查看表结构:desc / describe 表名 ;
6.创建表:create table 表名() ; Tips:加判断 if not exists
7.删除表:drop table 表名 ; Tips:加判断 if exists
8.创建临时表:create temporary table 表名 ( 查询语句 );
《更改》alter 更改
1.修改表名:alter table 旧表名 rename 新表名 ;
2.添加新列:alter table 表名 add 列名 数据类型 [约束] ;
3.删除旧列:alter table 表名 drop 列名 ;
4.修改旧列:alter table 表名 change 旧列名 新列名 数据类型 [约束] ;
5.添加主键:alter table 表名 add primary key (列名) ;
6.删除主键:alter table 表名 drop primary key ;
7.添加外键:alter table 外表名 add constraint 外键约束名 foreign key (外键列名) references 主表名 (主键列名) ;
8.删除外键:alter table 外表名 drop foreign key 外键约束名 ;
9.添加复合主键:alter table 表名 add primary key(列名一,列名二...) ;
《增添》insert 插入
1.插入单条数据:insert into 表名 [(列名)] values (值) ;
2.插入多条数据:insert into 表名 [(列名)] values (值) , (值)... ;
3.将查询结果复制到新表中:create table 新表名 ( select * from 旧表名 ) ;
3.将查询结果复制到新表中:create table 新表名 ( select * from 旧表名 ) ;
《更新》update 更新
1.无条件:update 表名 set 列名=值 ; Tips:整个列全改了!
2.少条件:update 表名 set 列名=值,列名=值 where 条件 ;
3.多条件:update 表名 set 列名=值... where 条件一 or 条件二 ;
4.改数值:update 表名 set 值=值+数值 where 值<某值 ; < > <= >= + - (xml中:小于< 大于> )
《删除》delete 删除
1.有条件:delete from 表名 where 条件 ; Tips:标识列不会重新编号
2.全删除:truncate table 表名 ; Tips:标识列会重新编号
《查询》select 挑选
1.查询所有列:select * from 表名 ;
2.查询部分列:select 列名 , 列名 ... from 表名;
3.综合查询:select 列名 /表达式 /函数 /常量 from 表名 where 条件 order by 列名 asc /desc ;
4.别名查询:select 列名 as 新名 from 表名 ;
5.合并多列变新列:select 列1 + 列2 ... as 新列名 from 表名 ; Tips:只对 "数值型" 有效
6.查询空值:select 列名... from 表名 where 列名 is null ; Tips: is not null 是 "有值"
7.使用常量列:select 列名 as 别名 ,定义列 as 定义值 from 表名 ;
《事务》transaction 事务
1.开始事务:start transaction; 或 begin;
2.提交事务:commit;
3.回滚事务:rollback;
注意一:使用事务应该先将自动提交关闭:set autocommit=0;
注意二:查看当前sql语句是否为自动提交:show variables like 'autocommit';
《视图》
1.创建视图:create view 视图名 as select查询语句;
2.删除视图:drop view if exists 视图名;
3.查看视图:select 列名 from 视图名;
4.查看所有视图:select * from views;
《索引》
1.创建索引:create index 索引名 on 表名( 列名 );
2.删除索引:drop index 索引名 on 表名;
3.查看索引:explain select 列名 from 表名;
《备份和恢复》
1.黑窗口防乱码:set names gbk;
2.备份数据库:mysqldump -u 用户名 -p 数据库名 [表名] >路径(d:\myschool.sql); Tips:在黑窗口中操作但不登录数据库
3.恢复数据库:mysql -u 用户名 -p 数据库名 [表名] <路径(d:\myschool.sql); Tips:需要登录,且存在名为要恢复的数据库
4.source恢复:source 路径; Tips:创建数据库,使用数据库,恢复数据库
5.导出数据:select * from into outfile '路径';
6.导入数据:load data infile '路径' into table 表名;
创建数据库
Create DATABASE databasename
Create DATABASE databasename
删除数据库
drop database databasename
drop database databasename
创建新表
create table tabname(col1 type1 [not null] [primary key] [AUTO_INCREMENT]
,col2 type2 [not null],..)--primary key为主键 auto_increment表示自增长
create table tabname(col1 type1 [not null] [primary key] [AUTO_INCREMENT]
,col2 type2 [not null],..)--primary key为主键 auto_increment表示自增长
删除表
drop table tabname--这是将表连同表中信息一起删除但是日志文件中会有记录
drop table tabname--这是将表连同表中信息一起删除但是日志文件中会有记录
删除信息
delete from table_name-这是将表中信息删除但是会保留这个表
delete from table_name-这是将表中信息删除但是会保留这个表
增加列
Alter table table_name add column_name column_type [default 默认值]--在表中增加一列,[]内的内容为可选项
Alter table table_name add column_name column_type [default 默认值]--在表中增加一列,[]内的内容为可选项
删除列
Alter table table_name drop column column_name--从表中删除一列
Alter table table_name drop column column_name--从表中删除一列
基本语句
(1) 数据记录筛选:
sql="select * from 数据表 where字段名=字段值 order by字段名[desc]"(按某个字段值降序排列。默认升序ASC)
sql="select * from 数据表 where字段名like '%字段值%' order by 字段名 [desc]"
sql="select * from 数据表 where字段名in ('值1','值2','值3')"
sql="select * from 数据表 where字段名between 值1 and 值2"
(1) 数据记录筛选:
sql="select * from 数据表 where字段名=字段值 order by字段名[desc]"(按某个字段值降序排列。默认升序ASC)
sql="select * from 数据表 where字段名like '%字段值%' order by 字段名 [desc]"
sql="select * from 数据表 where字段名in ('值1','值2','值3')"
sql="select * from 数据表 where字段名between 值1 and 值2"
(2) 更新数据记录:
sql="update 数据表 set字段名=字段值 where 条件表达式"
sql="update 数据表 set 字段1=值1,字段2=值2 …… 字段n=值n where 条件表达式"
sql="update 数据表 set字段名=字段值 where 条件表达式"
sql="update 数据表 set 字段1=值1,字段2=值2 …… 字段n=值n where 条件表达式"
(3) 删除数据记录:
sql="delete from 数据表 where 条件表达式"
sql="delete from 数据表" (将数据表所有记录删除)
sql="delete from 数据表 where 条件表达式"
sql="delete from 数据表" (将数据表所有记录删除)
(4) 添加数据记录:
sql="insert into 数据表 (字段1,字段2,字段3 …) values (值1,值2,值3 …)"
sql="insert into 数据表 (字段1,字段2,字段3 …) values (值1,值2,值3 …)"
(5) 数据记录统计函数:
AVG(字段名) 得出一个表格栏平均值
COUNT(*;字段名) 对数据行数的统计或对某一栏有值的数据行数统计
MAX(字段名) 取得一个表格栏最大的值
MIN(字段名) 取得一个表格栏最小的值
SUM(字段名) 把数据栏的值相加
AVG(字段名) 得出一个表格栏平均值
COUNT(*;字段名) 对数据行数的统计或对某一栏有值的数据行数统计
MAX(字段名) 取得一个表格栏最大的值
MIN(字段名) 取得一个表格栏最小的值
SUM(字段名) 把数据栏的值相加
引用以上函数的方法:
sql="select sum(字段名) as 别名 from 数据表 where 条件表达式"
sql="select sum(字段名) as 别名 from 数据表 where 条件表达式"
(6) 数据表的建立和删除:
CREATE TABLE 数据表名称(字段1 类型1(长度),字段2 类型2(长度) …… )
CREATE TABLE 数据表名称(字段1 类型1(长度),字段2 类型2(长度) …… )
(7) 单列求和:
SELECT SUM(字段名) FROM 数据表
SELECT SUM(字段名) FROM 数据表
#常用的sql优化
1.可以用 union all 代替 or
select * from `order` where hid=1 union all select * from `order` where hid=2;
select * from `order` where hid=1 union all select * from `order` where hid=2;
2.用like 代替 substring()
select hid from `order` where tel like '150%';
select hid from `order` where tel like '150%';
3.尽量少些*
select hid,tel,`date`,content from commen
select hid,tel,`date`,content from commen