mysql基本语法和练习题

数据定义语言(DDL):用于改变数据库结构,包括创建、更改和删除数据库对象  create alter drop

数据操纵语言(DML)    用于检索、插入和修改数据 对表中的数据进行操作  select insert update delete  

order by、Group by、Having、聚合函数、distinct、limit、left join、right join、inner  join、case when then、AND /OR/IN/ LIKE /IS NULL/>=/<=/index

创建表

create table classes(

    cid int(10) auto_increment primary key, //主键自增

    cname varchar(30)

);

创建表的时候可能用到select关键字

复制旧表部分字段

create table 新表名 as select 字段名,字段名 from 表名;

复制旧表

create table 新表名 as select * from  旧表名;

 插入行

insert into 表名 values(对应字段类型值,对应字段类型值)      //value和values效果一样

insert into 表名(任意字段名) value(对应字段类型值)

修改字段值

update 表名 set 字段=新字段值 where 字段=原字段值

查看表结构

desc +表名

修改表结构

加字段

alter table 表名 add 新字段 字段类型 //如果字段类型是varchar必须声明长度

加字段也可声明字段位置

alter table 表名 add 新字段 字段类型 first/after某字段

删字段

alter table 表名 drop 字段

主键外键

alter  table 表1 add(

foreign key(外键字段) REFERENCES 表2(主键)

);

删除表

drop table +表名 

清空表

delete  from  表名

删除行

delete from 表名 where 字段=字段值

单表

查找值 where

select    字段们   from  表名     where  字段=字段值

select    字段们   from  表名     where  字段 between 值  and  值;//

select    字段们   from  表名     where  字段 in (值, 值, 值);   

查找某个字段最大值、最小值、平均值、和

select max(字段)  AS 别名 FROM 表名

select min(字段)   AS 别名 FROM 表名

select avg(字段)   AS 别名 FROM 表名

select sum(字段)  AS 别名 FROM 表名

逻辑操作符 and or not

select    字段们   from  表名     where  字段1>=某值 and 字段2=某值

select    字段们   from  表名     where  字段1>=某值  or   字段2=某值

select    字段们   from  表名     where  字段 not in  (字段1,字段2,字段3);

order by         ASC:升序, 缺省,DESC: 降序

select    字段们   from  表名     order by 字段, 字段 desc/esc;

Group by 

Select 字段1,sum(字段2) from 表名 group by 字段1;  

Having

select 字段1,sum(字段2) from 表名 group by 字段1 HAVING sum(字段2)<某值

distinct 消除冗余

select distinct deptno,job from emp;  

LIKE  模糊查询

select * from 表名 where 字段 like  '_%' //_代表一个字符,%代表0或多个

IS NULL

select    字段们   from  表名    where   字段 IS NULL; //用 IS NULL操作符来检查有无空值

LIMIT 分页

select  * from 表名 limit 0,3;  //0 代表从第1行开始查  3代表查3行数据

select  * from 表名 limit 3 offset 0;//跟上条效果一样

case when then

select degree, case when degree>=90 then '优秀' when degree >=80 and degree<90 then '良好' when degree>=70 and degree<80 then '及格' else '不及格' end '成绩' from score ;//end后跟新加字段名

多表

两表

select 表1别名.字段,表2别名.字段 from 表1 表1别名 , 表2 表2别名 where 表1别名.某字段=表2别名.某字段;

四表

select c1.cityname,c2.cityname,c3.cityname,c4.cityname from city1 c1,city2 c2, city3 c3,city4 c4 where c2.pid=c1.id and c3.pid=c2.id and c4.pid=c3.id and c2.type=1;

左联            join 碰见where 使用 on 代替   left join 显示左边表的全部 右边表无值默认为null

select 表1别名.字段,表2别名.字段 from 表1 表1别名 left join  表2 表2别名 on 表1别名.某字段=表2别名.某字段;

内联   

select 表1别名.字段,表2别名.字段 from 表1 表1别名 inner join  表2 表2别名 on 表1别名.某字段=表2别名.某字段;

右联   right join 显示右边表的全部 左边表多余行不显示

select 表1别名.字段,表2别名.字段 from 表1 表1别名 right join  表2 表2别名 on 表1别名.某字段=表2别名.某字段;

视图

create view 视图名 as select 字段名 from 表名

获得当前时间

select sysdate() from 表名;    

select date(now()) from 表名;

运算时可以用虚表

select 2*3 from 表名;

删除用户

drop user 用户名 ;

回收权限    

revoke select on 数据库名点表名 from 用户名 ;

给新用户授权

grant select on 数据库点表名 to '用户名';

创建一个新的用户

create user '新用户名’ identified by '密码';

index 创建索引 create index 索引名 on 表名 (字段名);

查看索引 show index from 表名;

模糊查询 select * from 表名 where 字段名 like '%2%';  给一个字段添加索引之后模糊查询的效率更高

删除索引 drop index 索引名 on 表名

数据库练习题

1.用一条SQL语句查询出每门课都大于80的学生姓名

准备数据的 sql 代码:

create table student(
id int primary key auto_increment,
name varchar(20),
subject varchar(20),
score int
);

insert into student values
(null,'张三','语文',81),
(null,'张三','数学',75),
(null,'李四','语文',76),
(null,'李四','数学',90),
(null,'王五','语文',81),
(null,'王五','数学',100),
(null,'王五 ','英语',90);

答案: select distinct name from student where name not in (select distinct name from student where score<=80)

 2. 所有部门之间的比赛组合
一个叫 team 的表,里面只有一个字段 name,一共有 4
条纪录,分别是 a,b,c,d,对应四个球队,现在四个球队进行比赛,用一
条 sql 语句显示所有可能的比赛组合.
答:select a.name,b.name from team a, team b where a.name < b.name
    

猜你喜欢

转载自blog.csdn.net/Demis_demis/article/details/81584189
今日推荐