常见的SQL语句

举报
谢公子 发表于 2021/11/18 23:36:12 2021/11/18
【摘要】 目录 基础SQL语句 增 删 改 查 字符编码 基础SQL语句 注释符: #           /**/ 查看所有的数据库:show  databases; 查看所有的表:       show  table...

目录

基础SQL语句

字符编码


基础SQL语句

注释符: #           /**/
查看所有的数据库:show  databases;
查看所有的表:       show  tables;

查看user表中的所有列:show columns from user;
创建数据库:          create  database  test;
创建数据库并且制定默认字符集: create database db_student  default  charset utf8;
创建表:create table t_student(id int,name char,sex boolean);

  •       设置主键:primary key   (该项可以不填)
  •       默认值:default         (该项可以不填)
  •       自增:auto_increment   (该项可以不填)
  •       不允许为空:not null    (该项不填就允许为空 该项填了就不允许为空)

选择进入数据库:              use  db_student;
查看当前所在的数据库:   select  database();
 
查看创建数据库语句:  show create  database  test;
查看创建表语句:      show create  table  t_student;
查看表结构:             desc   t_student;     等价于 show columns from t_student;
 
外键是数据库一级的一个完整性约束
添加外键: 
create table  t_sc(sno char(10) not null,cno char(10) not null,degree decimal(4,1),primary key(sno,cno),
constraint fk_student_Sc_sno foreign key(sno) references t_student(sno),
constraint fk_course_sc_cno foreign key(cno) references t_course(cno));


向表中插入数据: insert  into t_student   value  (值1,值2,值3);
自定列插入数据: insert  into t_student    (列名)     values   (值);
一次插入多条数据:insert  into  t_student  values (值1,值2) , (值1,值2 ), (值1,值2);
增加一列:alter table t_student add age tinyint after id;

删除数据库:  drop  database  db_student;
删除表:      drop  table  t_student;
删除表中的一行: delete  from  t_student  where  id=1;
删除一列:  alter  table  t_student   drop   age;
  

修改表中的数据: update  t_student  set  name='xie'  where  id=2;
将表中id全部加1:update t_student set id=id+1;
修改表名(user—>users):rename  table  user  to  users;
修改一列的完整性约束条件:alter  table  t_student  modify name  varchar(12);
修改一列的名字(id—>ids):alter table two change id ids int;

查询表中的数据
查询结构:

select 列名 from 表名 where 条件1   group by条件2    having条件3  order by条件4   union 运算符   into outfile 输出文件名    limit[m,n];
 

查询学生表中的所有数据:select * from t_student;
从student选择不重复的id:select distinct id form t_Student;
选择前三条数据:select * from t_Student limit 3;
从第二行开始,选择四条数据:select*from t_Student limit 1,4;
获取当前的系统日期:select curdate();
从学生表中查询姓名和年龄:select sname,year(curdate())-year(sbirth) from t_student;
选择name,并且将name命名为名字:select name as  “名字”  form t_stu;
将student表中学生的成绩乘以1.2倍:select  degree*1.2  from t_student;
将选择的列创建新的表(查询结果的输出):create table  t_new  select id , name from t_stu;
写到文件中(查询结果的输出):select*from t_stu  into outfile ‘c:/a.txt’;

查询成绩大于80分的学生学号:select id from t_Student where degree>80;
从学生表中查询id在200502-200602的数据:select * from t_student where id between 200502 and 200602;
查询非计算机工程系的学生信息:select*from t_student where not sdept=’计算机工程系’;
查询数学系,计算机科学系学生信息:select*from t_student  where sdept  in  (‘数学系’,’计算机科学系’);
查询除了数学系以外其他系的学生信息:select*from t_student where  sdept not in (‘数学系’);
涉及空值的查询(查询没有成绩的学生信息):Select*from t_student where sdept is  null;


like模糊查询    %:任意(0-n)多个字符     _:任意一个字符
从学生表中查询姓名中含有 勇 的数据:select * from t_student where name like  “%勇%”;
从学生表中查询三个字的姓名,中间的字是勇的数据:select * from t_student where name like  “_勇_”;
 
union联合查询
union   重复的数据只显示一个
union all  显示所有重复的数据
当 select * from demo1 union select * from demo2;  必须得demo1表和demo2表拥有相同的列数才可以。
当 select id from demo1 union all select username from demo2; 数据显示为1列,重复数据也会显示出来
 
总数sum        平均 avg    最高max    最低min
查询学生总数:select count(*) from t_student;
查询学生总成绩:select  sum(degree)  from  t_sc;
查询学生平均成绩:select  AVG(degree)  from  t_sc;
查询学生的最高分和最低分:select MAX(degree) 最高分,MIN(degree)  最低分 from t_sc;
查询学号为20050102的学生的总成绩和平均成绩:select SUM(degree), AVG(degree) from t_sc where id=20050102;
查询有考试成绩的学生人数:select count(distinct sno) from t_sc where degree is not null;
 
GROUP BY   HAVING
查询student各系学生人数:select sdept,count(*) from t_student  group by sdept;
查询student表中男女学生人数:select ssex,count(*) from student group by ssex;
查询student表中各系男女学生人数:select sdept,ssex,count(*) from t_student group by sdept,ssex;
查询student表中各系女生人数:select sdept,count(*) from t_Student where ssex=’女’ group by sdept;
或者   select sdept,count(*) from t_student group by sdept,ssex having ssex=’女’;
查询选修了3门以上课程的学生学号:select sno from t_sc group by sno  having count(*) >3;
 
排序   order by        asc升序    desc降序
对student表中的数据按id按升序排列:select * form t_student order by id  asc;
判断student表中有多少列:select *from t_student where id=1 order by 5 desc;    如果报错就把5变为4,直到成功


正则表达式
查询姓名以“谢”开头的学生信息:select * from  t_student  where  name  REGEXP  “ ^谢 ” ;
查询姓名以“国”结尾的学生信息:select * from  t_student  where  name  REGEXP  “ 国$ ” ;
查询姓名是“谢*国”格式的学生信息:select * from  t_student  where  name  REGEXP  “ 谢.国 ” ;
 
 
多表连接查询
交叉连接(得到结果集的行数是两个表的行数的乘积):select t_Sc.*,t_course.* from t_Sc,t_course; 或者 select * from t_Sc,t_course;
自连接
内连接 INNER JOIN(返回两个表的交集):select * from t_sc,t_course where t_course.cno=t_sc.cno;
或者  select * from t_sc  INNER JOIN  t_course  ON  t_course.cno=t_sc.cno;
外连接
(1) 左外联LEFT OUTER JOIN     左表为主,右表为辅
(2) 右外联RIGHT OUTER JOIN   右表为主,左表为辅
(3) 全外联FULL OUTER JOIN    mysql不支持全外联

字符编码

查看字符集:show variables like '%character%';
查看mysql所支持的字符集:show charset;
查看表的字符集:show table status from test like '%admin%';
修改表的字符集:alter table admin convert to character set utf8;
查看表中所有的字符集:show full columns from admin;
修改列的字符集:alter table admin modify column password character set utf8;
 
修改字符集
只对当前连接有效
设置建立连接使用的编码:set character_set_connection=utf8;
设置数据库的编码:set character_set_database=utf8;
设置结果集的编码:set character_set_results=utf8;
设置数据库服务器的编码:set character_set_server=utf8;
设置数据库客户端的编码:set character_set_client=utf8;
设置文件系统的编码:set character_set_filesystem=utf8;

相关文章:https://www.w3school.com.cn/sql/sql_syntax.asp

                  MySQL 详细学习笔记

文章来源: xie1997.blog.csdn.net,作者:谢公子,版权归原作者所有,如需转载,请联系作者。

原文链接:xie1997.blog.csdn.net/article/details/82875868

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。