首页 > 数据库 >常见的SQL语句

常见的SQL语句

时间:2022-10-10 21:33:18浏览次数:71  
标签:语句 set 常见 查询 student SQL where id select

目录

​基础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 详细学习笔记​



标签:语句,set,常见,查询,student,SQL,where,id,select
From: https://blog.51cto.com/csnd/5745344

相关文章

  • 面试官:如何快速定位慢SQL
    开启慢查询日志在项目中我们会经常遇到慢查询,当我们遇到慢查询的时候一般都要开启慢查询日志,并且分析慢查询日志,找到慢sql,然后用explain来分析系统变量MySQL和慢查询相关的......
  • 面试官:如何用explain分析sql执行性能?
    如何根据慢查询日志定位慢SQL?在项目中我们会经常遇到慢查询,我们如何定位这些慢查询,并分析慢的原因呢?我们一步一步来注:文章大部分内容总结自《MySQL是怎样运行的?》系统变量My......
  • C++和Java多维数组声明和初始化时的区别与常见问题
    //C++只有在用{}进行初始化的时候才可以仅仅指定列数而不指定行数,因为可以通过直接//初始化时的元素个数自动计算出行数。而仅声明/创建数组而不初始化时,Cpp要求必须写明//......
  • Mysql之其他知识汇总
    日志类型 数据类型1.更小的通常更好能正确存储的最小数据类型,优点是占用磁盘、内存和CPU都少2.简单就好3.尽量避免使用null在数据库中null不等于null对游湖和索引和值......
  • python编程从入门到实践--第5章 if语句
    一。条件测试符号:==,>,>=,<,<=,!=,     逻辑符号:and,or,not测试有没在列表中cars=['audi','bmw','subaru','toyota']forcarincars:......
  • Flask 学习-87.Flask-APScheduler 持久化定时任务保存到mysql数据库
    前言APScheduler有四种组件,分别是:调度器(scheduler),作业存储(jobstore),触发器(trigger),执行器(executor)。jobstores存储jobstores支持四种任务存储方式memory:......
  • Navicat客户端因为版本问题连接不上mysql解决办法
    原因:新旧版本的密码加密方式不同解决办法:登录mysql中mysql-uroot-p1234usemysqlALTERUSER'root'@'localhost'IDENTIFIEDWITHmysq......
  • mybatis动态sql语句拼接总结
    mybatis中如果需要在where后面追加条件判断语句中有多个判断条件我们可以在if条件后面写上类似三元运算符的关系表达式其中a.pub_timeISNOTNULL为条件如果......
  • 服务器搭建(CenOS 7 Apache + PHP _Mysql环境(LAMP))
    服务器搭建(CenOS7Apache+PHP_Mysql环境(LAMP))第一步、更换阿里云yum源curl-o/etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo......
  • mysql CPU过高排查
    最近发现mysqlCPU使用率高,将排查步骤记录一下一、top命令找到PID为24319二、vmstat1查看一下CPU使用率三、pidstat-u查看一下CPU使用率​四、pidstat-t-p2431......