1.创建数据库
create database databaseName(数据库名称)
2.删除数据库
drop database dbname(数据库名称)
3.创建数据表
create table tableName(表名)(filedName1 type(长度) [not null] [primary key],fileldName2 type [not null]....)
4.删除数据表
drop table tableName
5.增加一个字段
alter table tableName add column columnName
6.删除一个字段
alter table tableName drop column columnName
7.修改数据表
update tableName set colum1="1",colum2="2" where ....
8.模糊匹配
create table table_new like table_old(使用旧表创建新表)
9.别名(as)
create table table_new as select col1,col2....from table_old definition only
10.创建索引
在表中创建索引,可以方便快速的查询数据。用户无法看到索引,他们只能被用来加速搜索/查询
1.允许使用重复的值:
create index index_name on table_name(column_name) (column_name规定需要索引的列)
2.不允许有重复的值:
create unique index index_name on table_name(column_name desc/asc)
如果希望索引不止一个列,可以加入其他的列用逗号隔开:
create index persionIndex on pserson(lastname,firstname)
删除索引
drop index indexName on tableName
注:索引不可更改
11.创建视图
create view view_name as select column_name(s) from table_name where condition
12.删除视图
drop view syntax drop view view_name
注:mysql不支持对视图的查询(支持创建和删除)
13.查询数据表
1.简单查询
select 列名 from 表名
select * from 表名
选择不重复的结果
select distinct 列名 from 表名
2.多选项查询条件:in(在列表中的都会被查询出)
select column_name(s) from table_name where column_name in(value1,value2...)
3.界值查询:between and(查询出范围之内的数据)
select column_name(s) from table_name where column_name between value1 and value2
注:对于边界值,即value1和value2的处理,会根据数据库的不同而不同(有些会包含边界值,有些则不会包含)
4.查询匹配:like
select * from table1 where fileld1 like '%value1%'
5.通配符
1.%替代一个或多个字符
2._代替一个字符
3.[charlist]字符列中的任何单一字符
4.[!charlist]或者[^charlist]不在字符列中的任何单一字符
6.排序:order by(默认按照升序排序)
select * from tableName order by colomnName
7.总数:count(column_name)函数返回指定条件的行数
select count(*) from tableName
8.求和:sum()
select sum(fileld1) as sumvalue from tableName
9:平均:avg(column_name)函数返回数值列的平均值,其中null值不包括在计算中
select avg(fileld1) as avgvalue from tableName
10.最大:max()
select max(column_name) from table_name
11.最小:min()
select min(column_name) from table_name
12.分组:ordetr by
select * from table_name order by column_name1,column_name2
13.对合计函数的结果进行筛选:having(where关键字无法与合计函数一起使用)
select * from table_name order by column1 having column_name<2000
14.指定字段第一个记录:first()
select first(column_name) from table_name
15.指定字段最后一个记录:last()
select last(column_name) from table_name
16.指定返回N条记录:top()
select top number|psercent column_name from table_name limit 5
14.插入数据
insert into 表名 values(值1,值2) where ....