常见SQL语句(仅供自己复习)

本文是关于SQL语句的复习,涵盖了启动数据库、DDL(数据定义语言)、DML(数据操作语言)的基本操作,包括创建、删除、修改数据库和表,以及插入、删除、更新和查询数据等。
摘要由CSDN通过智能技术生成

                                          常见SQL语句(仅供自己复习)

DDL数据定义语言(表,库,视图…)

create    drop   alter    show

增        删      改     查

DML 数据操作语言

Insert    delete   update   select

DCL 数据控制语句(权限)

grant   revoke

 

 

1.启动数据库(linux系统下)

        service mysqld start;  启动 数据库的服务

 

2.DDL语句

       create 库结构 表 视图 触发器  存储过程

        1.库的操作:

             create

                     create  database  [if not exists] 0623;  添加一个数据库【不存在就创建】

             drop  

                      drop  database [if exists]  0623; 删除一个数据库

             use

                     use  0623;使用数据库

            show

                       show      databases; 查看所有的库

                      show create database 0623; 查看一个库的创建信息

      2.表的操作:

                create table table_name(名字,类型,约束 [注释])

                         create table stu( sid varchar(10),name varchar(20),sex enum             (“man”,“woman”));      创建一个表                               括号(名字,类型,字段约束) 字段约束(主键(primary key) ,外键(foreign key), 唯一键                                               (UNIQUE),非空(not null),默认(default ))

                desc  stu  查看表的字段信息

                        show create tables stu;查看创建时候表的字段信息

                        show tables;   查看所有的表

                        drop table stu; 删除一个表

                 alter

                            1.字段类型   modify

                                   alter table stu modify sid vachar(20);  alter 表名 modify 字段名称 类型

                            2.字段名称  change

                                   alter table stu change sid  id vachar(20); 也可以修改字段类型

                            3.添加一个字段  add  (after,first)

                                   alter table stu add score float default 0;  add +名称+类型(放在最后一个)

                                   alter table stu add score float default 0 after id;  放在after id后面  first  在…的前面

                            4.删除字段  drop

                                   alter table stu drop score;  删除类型

                            5.修改表名  rename

                                   alter table stu rename stua;

3.DML数据操作语句

    insert   delete   update   select

    1.添加数据

          insert into stu(表名)  values(内容)(“001”,“张三”,“man”,20); 每次数据写在()里面用逗号隔开。  大批量  load

          insert into stu(id,name,sex) values(“002”,”lisi”,”woman”); 当我们输入的信息少于字段信息需要制定输入的是某些字段信

   2.删除数据  

            delete from stu (where id = “006”);  删除表的数据(限定条件)

   3.修改数据

               update stu set age = 19 (where id = “002”); 修改数据(指定条件 )

    4.查询数据

             1.普通查询

                    select * from stu;//  通匹查询

                    select id , name,sec,age from stu; 查询某些字段的信息

                      select  * from stu where id = “001” ; 带着过滤条件

            2.去重查询  distinct

                       select distinct age from  table_name;

             3.排序查询  order by   升序 asc  降序 desc 

                         select distinct age from stu order by age; 放在最后  不写的话 默认升序

              4. 分组查询  group by   (count 统计条数,year 年份)

                          select id,Sum(score) from result group by id; 根据 id 分组  sum 总分

             5.  多表查询

                       等值查询

                                 select stu.id,score from stu,result where stu.id = result.id and age < 20 and score < 60;(指明id 是哪的)

                     1.外连接查询

                            1.左外连接查询

                                   select a.id,score

                                   from      (select id,age from stu where age < 20) a      left join     (select id, score from result where score                                        < 60) b             on a.id = b.id          where b.score is not null;

                            2.右外连接查询

                                   select a.id,score      from       (select id,age from stu where age < 20) a    right join

                                   (select id,score from result where score < 60) b     on a.id = b.id      where a.id is not null;

                            3.全外连接查询

                                   select a.id,score      from   (select id,age from stu where age < 20) a      full join

                                   (select id,score from result where score < 60) b    on a.id = b.id       where a.id is not null;

                     2.内连接查询

                            1.内连接查询   只筛选匹配项

                                   select a.id,score       from      (select id,age from stu where age < 20) a    inner join

                                   (select id,score from result where score < 60) b      on a.id = b.id;

 

           6.聚合查询 union(去重) | union all (不去重)

                       select * from stu    union        select * from teach;

                           

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值