Sql语句

1.显示所有数据库
 show databases;//展示所有数据库
2,创建数据管理系统
 create database 数据库名字;
3,删除数据库
 drop database 数据库名字;
4,使用数据库
 use 数据库名字;-->database changed;
5,创建数据表
 creat table 表名(字段名 字段类型(约束),字段名2 字段类型(约束),);
  字段类型有:int(Integer) 整数型;
     float/double 单精度/双精度浮点型
     date 日期类型
     datetime 日期时间类型
     char 定长字符串
     varchar 可变长度字符串
     text 大文本对象,可用于存储超长字符串
     bold 二进制大对象,可用于存储图片等二进制数据
6,查看创建的数据表
 show tables;//仅仅是表名的展示;
7,查看表的结构
 desc 表名//desc student;
8,修改数据表名称
 alter table 表名 rename 新表名;//alter table student rename stu;
9,删除数据表
 drop table 表名;//delete table stu;
10,添加表字段
 alter table 表名 add column 字段名 字段类型;//alter table student add column phone varchar(20);
 添加带默认值的字段名
 alter table student add column sex varchar() default'男';
11,修改表字段类型
 alter table 表名 modify 字段名 修改的类型;//alter table student modify sex varchar(10);
12,删除表字段
 alter table 表名 drop 字段名//alter table student drop phone;
13,约束 NOT NULL:非空约束;
  UNIQUE:唯一约束;
  PRIMARY KEY:主键,唯一标识该记录,非空且唯一;
  FOREIGN KEY:外键,指定该记录从属的主表记录;
   添加非空约束和主键;
    主键添加:alter table student add column id int primary key;
14,创建表数据时设置主键自增 ,以及名字非空;
 create table student(
 id int primary key auto_increment,
 name varchar(20) not null,
 age int,
 birthday date,
 xuefen float(3,1),//3代表一共有三位,1代表小数点后的位数.
 );
15,索引:索引的作用类似于书的目录,一个表可以有多个索引,每个索引都可用于加速该列的查询速度
 创建索引 creat index 索引名称 on 表名(字段名);
    create index nameindex on student(name);//创建指向student name
 删除索引 drop index 索引名称 on 表名;
    create index nameindex on student;
 劣势:当数据进行修改时,索引也需要进行更新,较为麻烦,同时索引信息需要一定的磁盘空间。
16,中文乱码需要键入 set names gbk;//解决名字乱码问题;
17,插入数据 insert into 表名(字段名1,字段名2) values(数据1,数据2);
 不带自定义id的插入学生信息:
  insert into student(name,age,birthday,xuefen) values('科比',39,'1978-8-30',1.8);
 展示输入的学生所有信息:
  select * from student;
 展示表名中的部分学生信息:
  select age,xuefen from student;
 自带id的插入学生信息:
  select into student(id,name,age,birthday,xuefen) values(5,'杜兰特',29,'1988-9-29',1.9);
 若此时再进行不带自定义id的形式插入学生信息,则会从自带id的后面进行自增长 
  insert into student(name,age,brithdaty,xuefen) values('艾弗森',42,'1975-6-7',3.0);
 若输入的顺序和展示出来表的字段的顺序相同,可省略字段部分;
  insert into student values(2,'汤普森','27','1990-2-8','3.5');
18,修改数据 update 表名 set 字段1=值1,字段2=值2,where条件,
  没有添加where条件的会修改全部记录,
   uodate student set sex=男;
  添加where 一般指定id;
   update student set xuefen=3.0 where id=4;
19,删除数据 delete from 表名 where 条件;//以行为单位进行整行删除
    delete from student where id=7;
20,查询数据{单表查询,联合查询};
 单表查询:单条件查询,去重查询,查询条件中的运算符,函数查询,分组查询,分页查询,结果排序
   20.1 单条件查询:select * from 表名;(全部元素都会显示出来);
        select * from student;
     展示特殊字段 select 字段1,字段2 from 表名
         select name,age from student;
   查询特殊条件的 select * from 表名 where 条件;
       select * from student where age>30;
   查询多个特殊条件的;条件1 and 条件2;//与
        条件2 or 条件2;//或
   20.2 去重查询:可以使用distinct关键字从查询结果中消除重复数据;
     select distinct 字段1,字段2 from 表名;
     select distinct name,xuefen from student;name和学分都重复才会去除;
   20.3 查询条件中的运算符;比较运算符=,!=,<> ,>,>=,<,<=;
           逻辑运算符 and:与 or 或 not:非 优先级 not>and>or
      优先级的检测;
      select * from student where 1<>1 and 1=1 or 1=1 or 1=1;
      select * from student where 1=1 or 1=1 or 1=1 and 1<>1;
     使用between and
      between 16 and 20 ;//可以取到16 和20,这两个边界值。
     使用in运算符
      select * from student where age in(10,30,40,29);
      跟表中的年龄进行比对,看有没有能够匹配相等的,有,则显示
     使用like运算符
      模糊运算符,像
      select * from student where name like '%张';
      '%'代表无限个字符  '%张'是指以张结尾的名字
      '%张%'中间有张字的名字 '张%'是指以张开头的名字
      '__张'以'张'字结尾的三个字的名字,'_'代表一个字符;
     使用is null运算符
   20.4 函数查询
     max:求最大值,min:求最小值,avg:求平均值,sum:求和,count:求数量
     case 函数,类似于switch;
      select max(age) from student;//选择最大年龄;
      最大年龄对应的名字
      select age,max(age) from student;
      //展示max(age)对应的所有信息;
      //select max(age) from student-->返回的是一个最大年龄的表单
      select * from student where age=(select max(age) from student);
     count的应用:
      select count(1) from student;
     case:的应用
      select * ,case
      where age>30 then '老年人'
      where age<30 then '中年人’
      end
      from student;
     实例1:查询所有大于平均年龄的学生记录
     //平均年龄 select avg(age) from student;
     //select * from student when age>(select avg(age) from student);
   20.5 分组查询
     group by 分组字段;
      //案例:平均年龄按照男女性别分组
      select avg(age) from student gronp by sex;
      //显示男女性别,同时平均年龄按照男女性别分组
      select age,avg(age) from student group by sex;
     having 分组条件
      //案例,显示男女性别,同时平均年龄按照男女性别分组但是大于平均年龄的要小于3个
      select sex avg(age) from student group by sex having count<3;
     where 和having 的区别
     where用于过滤行,having 用于过滤组,where 子句中不能使用组函数,
      having子句中可以使用组函数
20.6 分页查询 limit x,y;x代表下标,y代表个数
     select * from student limit 0,2;起始下标为0的位置显示两天信息;
20.7 排序查询 order by 字段//默认是升序
     select * from student order by age (asc);//默认按年龄升序排列
     select * from studnet order by age desc;//按年龄降序排列;
      案例1:先按年龄升序排序,再取出年龄最大的两个
       select * from student by age asc limit 0,2;
      案例2:先按年龄升序排序,如果年龄相同再按学分降序排序
       select * from student by age ase,xuefen desc;
  ##分组的顺序优先级:
  select * from student group by () having where order by limit;
21 关联查询
  21.1 连接查询
     交叉连接,使用on子句的连接,左连接,右连接;
      21.1.1 交叉连接
      select * from student cross join class;
      添加别名进行交叉连接选择
      //用于挑选一个学生选择一门课程时的展示数据
      select * from student s cross join class c where s.kid=c.id;
      //挑选所有选了某个课程的学生
      select s.* from studnet s cross join class c where s.kid=c.id and c.classname="crossover";
      21.1.2 on连接
         select * from student s join class c on;
      21.1.3 左连接
        select * from student s left join class c on s.kid=c.id;
   21.1.4 右连接
     select * from student s right join class c on s.kid=c.id;
  21.2 子查询
      1.出现在from语句后当成数据表,
   2.出现在where条件后作为过滤条件的值,
     //案例查询所有选择某门课程中学生年龄最大的;
     //所有选择某门课程的学生
     select * from student s join class c on s.kid=c.kid and classname="";
     //筛选年龄最大的
     先进行排序再进行分页
      select * from( select * from student s join class c on s.kid=c.kid and classname="") t
      order by t.age desc limit 0,1;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容