1、数据库操作:
查看数据库:show databases;
创建数据库:create database db_name; //db_name为数据库名
使用数据库:use db_name;
删除数据库:drop database db_name;
2、表操作:
查看数据库中可用的表:show tables;
创建表:
create table tableName
(
cloum1 definition1 not null,
cloum2 definition2 not null,
cloum3 definition3 not null,
……
);
查看表的结构:describe tableName/show columns in tableName/
show columns from tableName;
复制表:create table tableName1 select * from tableName2;
部分复制表:create table tableName1 select column_name1,column_name2,... from tableName2;
表重命名:rename table tableNameOld to tableNameNew/alter table tableNameOld rename tableNameNew;
删除表:drop table tableName;
添加主键: alter table tableName add primary key(column_name);
删除主键: alter table tableName drop primary key(column_name);
创建索引:create index indexName on tableName(column_name,...);
删除索引:drop index indexName; //索引是不可更改的,想更改必须删除重新建。
增加列:
alter table tableName add(column_name1 definition1, column_name2 definition2,…… );
修改列:
alter table tableName modify(column_name1 newdefinition1, column_name2 newdefinition2);
删除列:
alter table tableName drop(column_name1,column_name2);
===========================================================
增:
insert into tableName (column_name1, column_name2, column_name3,……) values (value1, value2, value3,……);
insert into tableName1(column_name1,column_name2) select column_name1,column_name2 from tableName2;
删:
delete from tableName where condition
改:
update tableName setcolumn_name1=newData,
column_name2=newData,…… where condition
查:
select */select_list from tableName where condition
3、视图操作:
视图的特殊作用:
a、简化表之间的联结(把联结写在select中);
b、重新格式化输出检索的数据(TRIM,CONCAT等函数);
c、过滤不想要的数据(select部分)
d、使用视图计算字段值,如汇总这样的值。
创建视图:
create view viewName as select * from tableName where condition;
删除视图:drop view viewName;
4、条件控制:
where语句:
select */select_list from tableName where condition
having语句:
select column_name from tableName group by column_name having count(*)>2
相关条件控制符:
=、>、<、<>、in(1,2,3...)、between a and b(包含边界值)、not
and 、or
like 用法中 % 为匹配任意、_ 匹配一个字符(可以是汉字)
is null 空值检测
5、连表:
A
B、 左外连接 left join
左外连接(左连接) 就是说如果左表的某行记录在右表中没有匹配项,则在返回结果中右表的所有选择列表均为空。
C、右外连接 right join
右外连接(右连接):与左外连接相反,将右边表中所有的数据与左表进行匹配,返回的结果显示出了匹配成功的记录,还包含右表中未匹配成功的记录,并在其左表对应列补空值。
D、全外连接 full join
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。