MySQL语法知识点

来源:互联网 发布:淘宝生意参谋标准版 编辑:程序博客网 时间:2024/04/25 00:19

MySQL语法知识点

一、数据库:

1.增加/创建:

createdatabase mydb1 character set utf8 collate utf8_general_ci;

2.删除:

dropdatabase mydb1;

3.查看:

查看哪些库:

showdatabases;

查看创建库的详细信息:

showcreate database mydb1;

4.备份:Windows——CMD

mysqldump–uroot –proot mydb1>c:\mydb.sql

5.恢复数据:mysql中

创建新库:

createdatabase newdb;

usenewdb;

 

Sourcec:\mydb.sql

---------------------------------------------------------------------

二、表

1.创建:

createtable person

(

idint,

namevachar(20),

birthdaydate,

jobvarchar(20),

salarydecimal,

resumetext

);

2.查看表

查看有哪些表:show tables;

查看创建表的详细信息:show create table person;

查看创建表的结构:desc person;

查看表中的数据信息:select *from person;

3.增加记录——增

altertable person add image blob;

 

增加记录——insert into

1>insertinto person(id,name,birthday,job,salary,resume)

values(1,’tom’,’1990-01-01’,’programmer’,5000,’hello’);

2>insertinto person(id,name,birthday,job,salary,resume)

values(1,’tom’,null,null,5000,null);

3>insertinto person(id,name)

values(1,’tom’);

4.删除——删

删除表:drop table person;

删除某一字段:alter table person drop sex;

删除表中某一条记录:delete from person where id=1;

删除表中所有记录:delete from person;

使用truncate删除表中记录。(再创建一张空表):truncate table employee;

 

5.修改——改

修改表的字符集:alter table person character set gbk;

表重命名:rename table person to user;

修改字段数据类型:alter table person modify jobvarchar(40);

修改字段名:

alter table person change column name username varchar(40);

---------------------------------------------------------------------

修改所有记录的字段值:update person set salary=5000;

修改指定记录的字段值:update person set salary=5000 whereid=1;

updateperson set salary = salary+1000 where id=1;

修改指定记录的多条字段的值:

updateperson set salary=5000,job=’programmer’ where id=1;

---------------------------------------------------------------------

6.查询——查

创建数据

createtable student(

    id int,

    name varchar(20),

    chinese float,

    english float,

    math float

);

insertinto student(id,name,chinese,english,math) values(1,'张小明',89,78,90);

insertinto student(id,name,chinese,english,math) values(2,'李进',67,98,56);

insertinto student(id,name,chinese,english,math) values(3,'王五',87,78,77);

insertinto student(id,name,chinese,english,math) values(4,'李一',88,98,90);

insertinto student(id,name,chinese,english,math) values(5,'李来才',82,84,67);

insertinto student(id,name,chinese,english,math) values(6,'张君宝',55,85,45);

insertinto student(id,name,chinese,english,math) values(7,'黄蓉',75,65,30);

---------------------------------------------------------------------

查询所有学生信息:select * from student;

查询表中所有学生的姓名和对应的英语成绩。

selectname,english from student;

过滤表中重复的英语数据。

select distinct english from student;(注意:不能选择其他列)

在所有学生总分上加10分特长分。

select name,(chinese+english+math)+10 from student;(可以选择)

统计每个学生的总分。

select name,(chinese+english+math) from student;

 

使用别名表示学生分数。--别名: 1. as 2.  as可以省略 3. 跟双引号

select name as 姓名,(chinese+english+math)+10 as 总分 fromstudent;

select name 姓名,(chinese+english+math)+10  总分 from student;

查询姓名为王五的学生成绩

select* from student where name='王五';

 

查询英语成绩大于90分的同学

select* from student where english>'90';

(分数不是字符串也可以,最好不用引号)

查询英语分数在 80-90之间的同学。

selectname from student where english>80 and english<90;

selectname from student where english between 80 and 90;

查询数学分数为89,90,91的同学。

select* from student where mathin(89,90,91);

查询所有姓李的学生成绩。

select* from student where name like '李%';

select* from student where name like '李_';(俩字姓名)

查询姓名中间带“_”的学生的信息;

select*from student where name like ‘%\_%’;

查询数学分>80,语文分>80的同学。

select* from student where math>80 and chinese>80;

---------------------------------------------------------------------

排序:

 

对数学成绩排序后输出。

selectname,math from student orderby math; (升序)

对总分排序后输出,然后再按从高到低的顺序输出

select name 姓名,(chinese+english+math) 总分 from studentorder by (chinese+english+math) desc;

select name 姓名,(chinese+english+math) 总分 from studentorder by 总分 desc;

对姓李的学生成绩排序输出

selectid,name,english,math,chinese,(chinese+english+math) 总分 from student where namelike '李%' order by (chinese+english+math) desc;

---------------------------------------------------------------------

统计:

统计一个班级共有多少学生?(合计函数)

selectcount(name) from student;   

selectcount(*) from student;     

统计数学成绩大于80的学生有多少个?

selectcount(*) from student where math>80;

统计总分大于250的人数有多少?(判断分数大于250 不能用别名)

selectcount(*) from student where (chinese+english+math)>250;

 

统计一个班级语文、英语、数学各科的总成绩

selectsum(chinese),sum(english),sum(math) from student;

统计一个班级语文、英语、数学的成绩总和

selectsum(chinese+english+math) from student;

统计一个班级语文成绩平均分

selectsum(chinese)/count(*) from student;

统计一个班级语文成绩平均分(注意是否存在空的元素)avg不包含null

selectavg(chinese) from student;

 

求班级最高分和最低分

selectmax(chinese+math+english) 最高分,min(chinese+math+english) 最低分 from student;

 

 

=====================================================================

补充:

select * from orders limit 0,2;

意思是:从第0条查询两条

0 0