mysql简单的sql操做语句

一,经常使用、简单的SQL操做语句

  1.数据库操做:正则表达式

    1)建立数据库: create database database_name; 建立并设置字符编码 create database database_name character set utf8; sql

    2)删除数据库: drop datebase database_name; 数据库

    3)查看数据库字符集编码: show variables like 'character_set_database'     若是使用可视化工具要切换到所查数据库,或者使用: use database_name; 命令使用所查数据库
函数

    4)修改数据库字符编码:  alter database_name character set utf8; 工具

  2.数据表的操做:编码

    1)建立表: create table table_name(field1 int primary key,field2 varchar(20) not null ...) spa

    2)删除表: drop table table_name 3d

    3)插入表: insert into table_name(field1,field2) values(value1,value2) code

    4)查询表: select * from table_name where 查询条件 blog

    5)添加列: alter table table_name add col_name varchar(20) not null 

    6)删除列: alter table table_name drop column col_name 

    7)修改列: alter table table_name modify column col_name varchar(50) 

    8)更新列: update table_name set col1=value1... where 条件... 

  3.约束

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

    2)添加约束: alter table table_name add constraint 约束名 约束类型 

         好比: alter table student add constraint fk_1 foreign key(class_id) references class(class_id); 

    3)删除约束: alter table table_name drop 约束类型 约束名称  注意:删除主键时,应先删除引用了它的外键

         好比: alter table student drop foreign key fk_1 

三.经常使用的查询语句

 

  1.简单的查询:

    1)无条件查询: select * from table_name;||select col1,col2,... from table_name; 

    2)条件查询: select * from table_name where 条件; 

    3)排序查询: select col1,col2,...from table_name where 条件 .. order by 列名 desc/asc    desc:从大到小排序。asc:从小到大排序  ,默认是asc

      好比: select s_id,s_name,s_score from student where s_score>=60 order by s_id asc 译:查询出及格的学生,并按学生的id从小到大排序

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

      例: select * from student where s_name like '张%' 译:查询学生中姓张的,两个字,三个字的均可以查出来,如:张3、张麻子

      例: select * from student where s_name like '张_' 译:查询学生中姓张的,且只有两个字的,如:张3、张四

      例: select * from student where s_name like '[张李王]三' 译:查询学生中姓名为:张3、李3、王三 的信息

    5)分组查询: select * from table_name group by 列名  关键字 group by ,将统计的列中相同数据进行分组

         好比: select s_score,count(*) '人数' from student group by s_score 译:查询学生中每一个分数有多少人,就是对相同的成绩进行了分组

      分组查询经常使用函数:

        (1)max:求最大值     例: select s_name,max(math_score) from student group by s_name  查询数学成绩最高的学生姓名

        (2)min:求最小值      例: select s_name,min(math_score) from student group by s_name 查询数学成绩最低的学生姓名

        (3)avg:求平均值      例: select class_id,avg(math_score) from student group by class_id  查询每一个班的平均数学成绩

        (4)sum:求总数和  例: select sum(s_id) from student   查询表中一共有多少学生

        (5)count:求总行数 

    6)having用法:筛选成组后的各类数据,它能够筛选真实表中没有的数据做为查询条件

            好比: select s_name,sum(s_score) from student group by s_name having sum(s_score)>600  查询总成绩大于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  表示去全部信息的 第3条后面的5条记录:三、四、五、六、7

    8)简单的多表查询: 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)链接查询

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

          

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

           语法: select col1,col2,col3 from ta left join tb on 条件     on后面放链接的一些条件,跟where后面跟条件同样

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

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

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

            语法: select col1,col2,col3 from ta right join tb on 条件 

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

           结果:   把表的位置换了一下,咱们能够看出结果是同样的,左链接和右链接只是链接的方向不一样

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

           语法: select ta1.*,ta2.* from ta1 inner join ta2 on 条件 

           例: SELECT class.*,s_id,s_name FROM student INNER JOIN class   咱们这里不跟条件,查询的就是两个表的全部交集

                

           例: SELECT class.*,s_id,s_name FROM student INNER JOIN class ON class.class_id=student.class_id    有条件后,都知足条件的才会查询出来

              

相关文章
相关标签/搜索