1.创建(删除)数据库
CREATE DATABASE DATABENAME
例如:CREATE DATABASE KIKI(KIKI为库名)
DROP DATABASE DATABENAME
例如:DROP DATABASE KIKI
2.创建(删除)新表
create table tablename
(
col1 type1 primary key,/xx/
col2 type2 not null,/xx/
…
)
例如:
create table student
(
num int(10) primary key,/学号/
name varchar(10) not null,/姓名/
sex varchar(10) not null,/性别/
age int(2) not null,/年龄/
)
drop table tabname
例如:drop table student
3.根据旧表创建新表
select * into newtable from oldtable
如果不想导入记录,只想生成表结构:
select * into newtable from oldtable where 1=2;
如果newtable已经存在,想导入记录:
insert newtable select *from oldtable where…(有条件加where ,没有就不用)
4.索引
创建索引:create index indexname on table(col(length))
blob和text类型必须指定长度
例如:create index findnum on student(num(2))
添加索引(修改表结构):alter table tablename add index indexname(col)
例如:alter table student add index findnum(num)
删除索引:drop index indexname on table
例如:drop index findnum on student
索引可以在创建表的时候直接指定
唯一索引:
create unique index indexname on table(col(length))
alter table table tablename add unique indexname(col(length))
5.列操作
增加列:alter table tablename add colname type
例如:alter table student add del int(11)
修改列类型:alter table tablename modify column colname newtype
修改列名:
alter table tablename changer colname newcolname type
删除列:alter table tablename drop column colname
6.视图
提高重用性
例如:
测试表:goods表:id name price
user表:id name age sex
ug表:id userid goodsid
如果要频繁获取goods的name和user的name;
则需要不断使用
select a.name as username ,b.name as goodsname from user as a ,goods as b ,ug as c where
a.id=c.userid and b.id=c.goodsid
如果用视图
create view one as select a.name as username ,b.name as goodsname from user as a ,goods as b ,ug as c where a.id=c.userid and b.id=c.goodsid
则只需
select * from one 就可
对数据库重构却不影响运行
例如:user表分成usera 和 userb
usera :id name age
userb :id name sex
create view two as select a.id ,a.name,a.age ,b.sex from usera as a ,userb as b where a.id=b.id
提高安全性
不同用户可设定不同的视图,只让用户看见你想让他看见的东西
例如不让用户看见sex
create view three as select a.name
数据更加清晰
想要什么就创建什么视图
7.表内操作
查找:select xxx from where xxx 后面跟条件
插入:insert into tablename (col1,col2,col3) values(xxx,xxx,xxx) 如果是字符类型需要” ”
删除:delect xxx from tablename where xxx
更新(修改):update tablename set xxx=xxx where xxx 修改的值如果是字符类型需要’ ’
8.各种查询
like
用于模糊查询
例如:select * from student where name like ‘林%’
找全部姓林的同学的信息
一般不区分大小写,如果要区分
例如:select * from student where name like binary ‘Bzz%’ 查找Bzz开头的同学的信息
like要对字段数据进行扫描搜索,效率低
排序
对于查询的数据进行排序
例如:select * from student order by num asc;
asc升序 desc降序
总数
select count as xxx from tablename
例如:select count(student) as stunum from student
select count(*) from student where sex=’man’
求和:select sum(col) as xxx from tablename
平均:select avg(col) as xxx from tablename
最大:select max(col) as xxx from tablename
最小:select min(col) as xxx from tablename
一般用于表里有数值
例如select sum(price*num) as totalprice from ugs
9. UNION 组合
例如:select name from kischool UNION select name from hischool
如果在UNION 后面加ALL 则会显示重复选项
左连接:select a.a ,a.b ,b.c ,b.d from table1.a LEFT JOIN table2.b on a.e=b.f显示左表有的和右表跟左表都有的
右连接:select a.a ,a.b ,b.c ,b.d from table1.a RIGHT JOIN table2.b on a.e=b.f显示右表有的和右表跟左表都有的
内连接:select a.a ,a.b ,b.c ,b.d from table1.a INNER JOIN table2.b on a.e=b.f显示两表都有的
mysql不支持全连接,全连接用左连接和右连接配合UNION
10. group by 分组
与其他语句组合
例如:select * from courses GROUP BY student , class order by student,score DESC
Copyright © 2004-2024 Ynicp.com 版权所有 法律顾问:建纬(昆明)律师事务所 昆明市网翼通科技有限公司 滇ICP备08002592号-4