sql(Structured Query Language):结构化查询语言

注:括号内为单词的英文翻译

一、重要常用单词

  • 操作某个表的内容的:增、删、查、改
    • insert (插入)
    • delete (删除)
    • select (选择)
    • update (更新)
  • 操作(或对象) 本身
    • creat (创建)
    • drop (放下、掉下:这里可以理解为删除)
    • alter (改变:这里可以理解为选择某个表并进行操作)
  • 数据控制
    • grant (给予、赋予)
    • revoke (撤销)

二、操作的对象

  • 模式 schema(概要、图解、略图)这里创建的是外模式,了解模式内模式外模式(这里是个引用,以后补充)
    • creat schema
    • drop schema
  • table(表格)
    • creat table
    • drop table
    • alter table
  • 视图 view(视图)
    • creat view
    • drop view
  • 索引 index(索引)
    • creat index
    • drop index
    • alter index

三、对模式的操作

  • 创建模式 authorization(授权)
    • CREATE SCHEMA <模式名> AUTHORIZATION <用户名>[<表定义子句>|<视图定义子句>|<授权定义子句>]
    • 创建一个模式名为“school”并授权给用户teacherWang
      create schema school authorization teacherWang;
      
    • 创建一个模式名为“school”并创建子表“student”授权给teacherWang
      create schema school authorization teacherWang
      create table student(
      	name char,
      	age int
      	);
      
  • 删除模式 cascade(级联),restrict(限制)
    • DROP SCHEMA <模式名> <CASCADE|RESTRICT>
    • 删除模式“school”同时其中定义的表也被删除。
      drop schema school cascade;
      

四、对表的操作

  • 创建表
    • CREATE TABLE <表名>(列名 数据类型 [列级完整性约束条件如unique、primary key、not null、check()],…,[表级约束性条件如,primary key(属性1、属性2…)、foreign key (属性)references 表名(属性)])

    • 创建一个表名为“student”设置学号为主键,学号为外键,参照表是“grade”

      create table student(
      Sno char(20) primary key,
      name char(20) not null unique,
      sex char(10) check(sex in ("男""女")),
      primary key (Sno),
      foreign key (Sno) references grade(Sno)
      ); 
      
    • 创建一个表“student”在模式“school”下

      create table school.student(
      Sno char[20] primary key,
      name char[20] not null unique,
      );
      
  • 删除表
    • DROP TABLE <表名> [cascade|restrict]
    • 删除表“student”模式是“限制”
      drop table student restrict;
      
  • 改变表
    • 增加一列
    • ALTER TABLE <表名> ADD [COLUMN] 列名 数据类型 约束
      alter table student add column age int not null;
      
    • 增加约束
    • ALTER TABLE <表名> ADD 约束
      alter table student add primary key(Sno);
      
      alter table student add constraint pk_sno primary key(Sno); // 增加约束并将约束命名为pk_Sno
      
    • 删除一列
    • ALTER TABLE <表名> DROP 列名 [cascade|restrict]
      alter table student drop column age cascade;
      
    • 删除约束
    • ALTER TABLE <表名> DROP constraint 约束名
      alter table student drop primary key;  // 删除primary key约束
      
      alter table studetn drop constraint pk_Sno;  // 删除约束名为pk_Sno的约束
      
    • 改变一列
    • ALTER TABLE <表名> ALTER [COLUMN] 列名 数据类型
      alter table student alter column age char(10);  // 修改age列的数据类型
      
      alter table student alter column age rename to Sage;  // 改变age列的名字为Sage
      

五、对视图的操作

  • 创建视图
    • CREATE VIEW <视图名>([列名、…]) as <选择子句>
      create view v_stu as select Sno,age from student where age>12;
      
  • 删除视图
    • DROP VIEW <视图名>
      create view v_stu as select Sno,age from student where age>12;
      

六、对索引的操作

  • 创建索引
    • CREATE [UNIQUE] [CLUSTER] INDEX <索引名> on <表名> 列名 次序(asc,desc)、…
      create unique index i_s on student(age asc); // 在表student的age列创建索引升序
      
  • 删除索引
    • DROP INDEX <索引名>
      drop index i_s;
      
  • 改变索引
    • ALTER INDEX OLD_NAME RENAME TO NEW_NAME;
      alter index i_s rename to index_s;
      

七、操作表中的内容

  • 增加 insert

    • INSERT INTO <表名>(属性1、属性2、…) VALUES(val1,val2,…)
      insert into student(Sno,name,age) values(1243243,张良,12);
      
    • INSERT INTO <表名>(属性1、属性2、…) <select子句>
      insert into student(Sno,name,age) select val1,val2,val3 from info;
      
    • SELECT 属性 INTO 表2 FROM 表1 <条件子句>
      select * into student from info; //  从info中找到属性插入student表中
      
  • 删除 delete

    • DELETE FROM <表名> where <条件语句>
      delete from student where age<10;
      
      delete from student where Sno in (select Sno from info where age>10);
      
  • 修改 update

    • UPDATE <表名> set 属性=值 where <条件子句>
      update student set name=张良 where Sno=2131242;
      
      update student set age=12 where Sno in (select Sno from info where birth_year=2010);
      
  • 查询 select

    • 关键字:where、group by[having]、order by[asc|desc]、limit、offset、distinct、between and、not between and、in、not in、is null、is not null、and、or、like、not like、as、join、count、sum、avg、max、min、any、some、all、exists、not exists、union、intersect、except。

    • group by having

      // 按照指定列分组,该列值一样为一组,常在每组中作用聚集函数。having对组进行筛选显示。
      select Sname,sum(score) from score group by Sname having sum(score)>500;
      // 选择总分大于500的学生的姓名和成绩
      
    • order by [asc|desc]

      select * from student order by name;
      //选择student表中所有信息并按照名字排序,asc增序,desc降序
      
    • limit-offset-:limit后面跟着的数字是返回的行数,offset后面跟着的数字是跳过几行。

      select * from student order by name desc limit 1 offset 1;
      //选择student表中名字排在第二位的同学的信息,asc增序,desc降序
      
    • distincet

      select distinct name from scores;// 如果查询到多个相同的值,只选择一个。
      
    • between and/not between and

      select name from student where age [not]between 12 and 14;
      
    • int/not in

      select name from student where age [not]in(11,12,13);
      
    • is null/is not null

      select * from student where age is [not]null;
      
    • and/or

      select * from student where age=12 and[or] sex='男';
      
    • like/not like

      select * from student where name like '张%'; // %匹配任意长度字符,_匹配单个长度字符。
      
    • as (Alias)

      select name as '姓名' from student;  // 给属性改个名显示出来
      select s.name,i.age from student as s,info as i where s.name='xxx' and i.age>12; 
      
    • join

      select name from student inner[left][right][all] join info on student.age=info.age;
      
    • count、sum、avg、max、min

      select count(*) from student; //返回student表中的记录数量
      select sum(age) from student; //年龄的总和
      select avg(age) from student; //年龄的平均
      
    • union、intersect、except

      // 参加集合查询的各个查询子句结果的列数和对应的数据类型必须相同。
      select Sno from student union[intersect][except] select Sno from info; 
      // union 并集相当于或。intersect 交集相当于and。except差集。
      
    • any、some、all、exists、not exists。

      select * from student where age>any(12,13,14); 大于1个就成立。all必须大于所有。。
      

八、数据控制操作

  • GRANT <权限> on <对象类型> <对象> to <用户> [WITH GRANT OPTION;有这个则用户还可以授权给别人];
grant select on table student to user1;
grant all privilige on view s_v to user2,user3;
grant select on table student to public;
  • REVOKE <权限> on <对象类型> <对象> from <用户> [cascade,restrict];
revoke select on table student from user1 cascade;

更多推荐

SQL数据库语句知识汇总