SQL语言

数据定义语言(DDL)

     其语句包括动词 create 和 drop 。在数据库中创建新表或删除表(creat table 或 drop table);为表加入索引等。DDL 包括许多与人 数据库目录中获得数据有关的保留字。它也是动作查询的一部分

1.库
      (1)创建库

    create database [if not exists] dbname; 
    eg:create database [if not exists] CY1269; 

      (2)删除库

    drop database [if exists] dbname;
    drop database [if exists] CY1269;

      (3)查看所有库

    show databases;

2.表
      (1)创建表 create

    create table tbname(字段名称 字段类型 [字段约束],字段名称 字段类型 [字段约束],....);

      创建学生表 id、name、sex、age

    create table stu(
     id varchar(10) primary key COMMENT "学号",
     name varchar(10) not null COMMENT "姓名",
     sex enum("man","woman") COMMENT "性别",
     age int COMMENT "年龄"
    );

      创建成绩表 sid、pid、score

create table result
(
 sid varchar(10) not null comment '学号',
 pid varchar(10) not null comment '课程编号',
 score float comment '成绩'
);

char与varchar的区别
   char 开辟空间是定长,插入的字节长度小于定义长度时,剩余的用空格表示,varchar 开辟的空间是变长,插入的字节长度小于定义长度时,按实际长度存储。
    char 的占用空间固定,所以存取的速度比 varchar 快,但是占用的空间比 varchar 大。varchar 的存取速度慢,占用空间小。
    char 最多能存取 255 个字符,varchar 最多能存取 65532 个字符。varchar 的最大有效长度由最大行大小和使用的字符集确定。整体最大长度是 65532 字节。

主键 primary key
外键 foreign key 
唯一 unique
非空 not null
为空 default null

      (2)删除表 drop

drop table tbname;

      (3)修改表 alter
      修改字段类型 modify

 alter table stu  modify name varchar(50);

      修改字段名称 change

 alter table stu change id sid varchar(20);

      添加字段 add [after first]

 alter table stu add score1 float not null first;
 alter table stu add score2 float not null after score1;

      删除字段 drop

 alter table stu drop score1;

      修改表名 rename

 alter table stu rename student;

      (4)查看表
      查看表的创建

 show create table tbname;

      查看字段信息

  desc tbname;

数据操作语言(DML)

     其语句包括动词 insert,update 和 detele。它们分别用于添加,修改和删除表中的行。也称为动作查询语言。
insert delete update select
1.插入数据 insert load

 insert into tbname[(field1,field2,...)] values(data1,data2,...);
  insert into stu values("002","lisi","woman",19),
                        ("003","wangwu","man",20);
  insert into stu(id,name) values("004","zhaoliu");

2.删除数据 delete

  delete from stu;
  delete from stu where id = "001";

3.修改数据 update

  update stu set age = 22;
  update stu set age = 22  where name = "lisi";

4.查询数据 select
    (1)普通查询

1.select * from stu;
select id,name,sex,age from stu;
2.select name from stu where age > 20;  

    (2)去重查询 distinct

  select distinct age from stu;

   (3) 排序方式 order by asc|desc

   select distinct age 
   from stu
   order by age desc;

    (4)分组查询 group by

   select sid,SUM(score) all_score
   from result
   group by sid;

    (5)多表查询
查询年龄小于二十岁的学生不及格信息
1.等值查询

select name,score 
from  stu,result
where id = sid and age < 20 and  score <60;

2.连接查询
      1.外连接查询
            1.左外连接查询

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

            2.右外连接查询

select sid,name,score from stu right join score on id=sid;
  select name,score
  from 
  (select id,name from stu where age < 20) a
  right join
  (select sid,score from result where score < 60) b
  on id= sid
  WHERE name is not null;

            3.全外连接查询

  select name,score
  from 
  (select id,name from stu where age < 20) a
  full join
  (select sid,score from result where score < 60) b
  on id= sid
  WHERE name is not null;

      2.内连接查询
            1.内连接查询

  select name,score
  from 
  (select id,name from stu where age < 20) a
  inner join
  (select sid,score from result where score < 60) b
  on id= sid;

(6)联合查询 union | union all

  select * from stu
   union all 
   select * from teach;

数据控制语言(DCL)

     它的语句通过 grant 或 revoke 获得许可,确定单个用户和用户组对 数据库对象的访问。某些 rdbms 可用 grant 或 revoke 控制对 表单个列的访问。
grant

grant all on CY1269.* to user1;

revoke

 revoke select on CY1269.* from user1;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值