一,常用、简单的SQL操作语句

  1.数据库操作:

create database 创建并设置字符编码 create database database_name character set

drop

show variables like 'character_set_database'    如果使用可视化工具要切换到所查数据库,或者使用: use 命令使用所查数据库

alter database_name character set

  2.数据表的操作:

create table table_name(field1 int primary key,field2 varchar(20) not null

drop table

insert into table_name(field1,field2) values(value1,value2)

select * from table_name where

alter table table_name add col_name varchar(20) not null

alter table table_name drop column col_name

alter table table_name modify column col_name varchar(50)

update table_name set col1=value1... where

  3.约束

    1)种类:primary key(主键约束)、default(默认约束)、not null(非空约束)、unique(唯一约束)、foreign key(外键约束)、check(检查约束)

alter table table_name add constraint

alter table student add constraint fk_1 foreign key(class_id) references

alter table table_name drop

alter table student drop foreign key

三.常用的查询语句

 

  1.简单的查询:

select * from table_name;||select col1,col2,... from

select * from table_name where

select col1,col2,...from table_name where 条件 .. order by 列名 desc/asc

select s_id,s_name,s_score from student where s_score>=60 order by s_id asc

    4)模糊查询:查询关键字 like 主要使用 % 、 _ 、[ ] 三个字符 ,表示匹配0个或多个字符(通配符),  _  匹配一个字符,[ ]  匹配其中中的一个(类似正则表达式)

select * from student where s_name like '张%'

select * from student where s_name like '张_'

select * from student where s_name like '[张李王]三'

select * from table_name group by

select s_score,count(*) '人数' from student group by

      分组查询常用函数:

select s_name,max(math_score) from student group by

select s_name,min(math_score) from student group by

select class_id,avg(math_score) from student group by

select sum(s_id) from

        (5)count:求总行数 

    6)having用法:筛选成组后的各种数据,它可以筛选真实表中没有的数据作为查询条件

select s_name,sum(s_score) from student group by s_name having sum(s_score)>600

          只有每科的成绩,这时就可以用having了,where就不能来筛选总成绩大于600的学生了。

          having和where的区别:

            having:having对查询结果中的列发挥作用,筛选数据

            wherer:where针对表中的列发挥作用,查询数据

    7)limit用法:limit 主要是用于分页,limit n,m 表示从n+1开始取m条数据

select * from student limit 2,5

select table1.*,table2.* from table1,table2 where

  2.子查询和连接查询

    1)where子查询: 把内层查询结果当作外层查询的比较条件

select s_name,s_score from student where s_score in (select s_score from student where s_score>=60)

    2)from子查询:把子查询的结果作为一个表,进行再次查询

          比如: 查询成绩及格学生的姓名个班级,这里将子查询作为一个新表(stu) 再进行查询  ,这里有班级表(calss)和学生表(student)

select s_name,class_name from class,(select s_name,class_id from student where s_score>=60) as stu where class.class_id = stu.class_id

 

 

    3)exists子查询:把子查询结果拿到内层,看内层的查询是否成立

         比如:查询班级中的学生姓名,

select class_id,s_name from student where exists(select * from class where class.class_id=student.class_id)

    4)连接查询

      连接查询我们把表的数据给出来,方便对照查看

        

MYSQL数据库的具体使用方式 mysql使用数据库语句_子查询

  

MYSQL数据库的具体使用方式 mysql使用数据库语句_MYSQL数据库的具体使用方式_02

      left join 左连接:以左表为准,去右表找数据,如果没有匹配的数据,则以null补空位

select col1,col2,col3 from

SELECT class.*,s_id,s_name FROM class LEFT JOIN student ON class.class_id=student.class_id

            结果:

MYSQL数据库的具体使用方式 mysql使用数据库语句_数据_03

  查询班级里的学生,没有学生的就用 null 补位了

     right join 右连接:以右表为准,去左表找数据,如果没有匹配的数据,则以null补空位  和左连接相反

            语法: select col1,col2,col3 from

            例: SELECT class.*,s_id,s_name FROM student RIGHT JOIN class ON class.class_id=student.class_id

           结果: 

MYSQL数据库的具体使用方式 mysql使用数据库语句_数据库_04

  把表的位置换了一下,我们可以看出结果是一样的,左连接和右连接只是连接的方向不同

     inner join 内连接:查询的结果是所连接2个表的交集,

select ta1.*,ta2.* from

SELECT class.*,s_id,s_name FROM student INNER JOIN class

                

MYSQL数据库的具体使用方式 mysql使用数据库语句_MYSQL数据库的具体使用方式_05

SELECT class.*,s_id,s_name FROM student INNER JOIN class ON class.class_id=student.class_id

              

MYSQL数据库的具体使用方式 mysql使用数据库语句_子查询_06