SQL-SQL基础

SQL(Structured Query Language)是通用的数据库查询语言,各个数据库厂商均对SQL-92标准做了支持,同一时候各家又再次基础上做了相应扩展,比如oracle的PL/SLQ。

SQL主要分为四大类:
DDL:Data Defined Language,数据定义语言。
DML:Data Munipulation Language,数据改动语言。
DQL:Data Query Language,数据查询须要。
DCL:Data Control Language,数据控制语言。

说明:
利用DDL对数据库对象(数据库、数据表、视图、索引、序列、存储过程、触发器、事务)进行创建、改动、删除。利用DML可对数据表中的数据进行增、删、改操纵。

DQL提供对数据表中数据的查询接口。DCL负责对数据库中的权限等进行控制。

以下对这几种类型的数据库操作语言做简要描写叙述。

  • 首先,是DDL数据定义语言。


    对数据库中对象的定义都属于DDL,以创建表为例。
    1)创建表
    grammar schema :

create table table_name(
    column_name datatype [not null || null]
    ...
    [constraint]
);

注意:约束的类型主要有 主键约束、外键约束、检查约束、非空约束、唯一约束

2)改动表
alter table table_name operation_type
eg:
加入列:alter table table_name add column column_name datatype;
改动列:alter table table_name modify column column_name datatype;
删除列:alter table table_name drop column column_name;
重命名列:alter table table_name rename column oldname to newname;
重命名表:alter table table_name rename oldname to new name;

3)删除表
drop table table_name;

外篇:
约束的使用:主键约束、外键约束、唯一约束、检查约束、非空约束。

1)主键约束:主键约束在一个数据库表中仅仅能有一个,一个主键能够有一列或多列组成。

加入主键有三种方式
在列定义的时候指定主键(这样的方式仅仅能指定一列为主键约束)

create table table_name(
          .....
          column_name data_type primary key,
          ......
);

在列声明的后面加入主键约束的声明(这样的方式能够声明多列为主键约束)

create table table_name(
    ....
    column_name data_type,
    constraint constraint_name primary key(column1,column2...)
);

在表定义外加入主键约束

alter table table_name add constraint constraint_name primary key(column1,column2,column3...)

删除主键约束

alter table table_name drop constraint constraint_name;

2)外键约束:能够保证使用外键约束的列与所引用的主键约束的数据列一致。一个数据表中能够有多个外键。

外键的加入方式与主键同样,基本的语法为:

constraint constraint_name foreign key column(columen_name)
references table_name(column_name) [on delete cascade]

3)唯一约束:可设置在表中输入的字段都是一味的,与主键相似,差别在于主键仅仅能有一个,而唯一约束能够有多个。

唯一约束的加入方式与主键同样。基本的语法为

constraint constraint_name unique(column_name)

4)检查约束:检查约束能够规定每列能够输入的值,进而保证数据的正确性。

创建方式同上,基本的语法为。

constraint constraint_name check(condition)

eg:

constraint constraint_name check(age>=18 and age<=30)

5)非空约束:在创建表时。为列加入非空约束,保证该字段必须输入值。

创建语法为:

create table table_name(
    ...
    column_name datatype not null,
);

移除非空约束:

alter table table_name modify column column_name null;
  • 另外一种是DML语句

    DML包含对数据的增、删、改操作。


    1)加入数据
    ①向表中直接插入数据

insert into table_name(column1,column2...) values (data1,data2...)[,(data1,data2...)]

②通过还有一个查询语句的结果向表中插入数据

insert into table_name1(column1,column2...) select data1,data2... from table_name2

③在创建表时。直接从一个源表中取出数据并插入

create table  table_name as select column1,column2... from source_table_name

2)改动数据

update table_name set column_name1=data1,column_name2=data2...
[where condition]

不写where字句回向表中的全部数据更新。

3)删除数据

delete from table_name [where condition]

不写where字句时会删除表中的全部数据。

4)其它数据操作语句
truncate:truncate语句和delete语句一样都是为了完毕删除数据表中的数据的。但两者是有差别的。用truncate删除表数据和没有where字句的delete一样都是删除表中的全部数据,但使用truncate会更快一些。

delete语句每次删除一行,并在事务日志中为全部删除的每个记录一项,truncate通过释放存储表数据所用的数据页来删除数据。而且仅仅在事务日志中记录释放的页数,所以用truncate删除表中全部数据能够释放存储空间,delete则不会。

truncate删除表中的全部行,但表结构及其列、约束、索引等保持不变。


truncate不能激活触发器。不能用于參与了视图的表。

delete删除数据会产生碎片。truncate不会。

  • 第三章是DQL 查询语句

select是查询语句必备的关键字,select语句由一系列字句组成,终于检索数来的数据是由字句决定的。


select语句依照复杂程度可分为:
1)简单查询
2)where条件查询
3)多表查询
4)子查询

DQL-select有的基本的语法格式为

select [distinct] column_list from table_list
[where condition] [group by column_name]
[having condition] [ordre by column_name]

1)获取指定字段的数据

select column1,column2,column3 from table_name

2)获取全部字段的数据

select column1,column2, ... columnn from table_name
select * from table_name

注:在查询全部字段的数据时,尽量不要用*,第一由于效率比較低,第二当表结构发生变化时,easy导致程序异常,第三用详细字段能够降低网络开销。

3)使用别名替代表中的字段名查询

select column_name as '别名' from table_name

4)使用函数操作查询字段

select substr(productid,1,6) from productinfo

5)去除检索数据中的反复记录

select distinct category from productinfo

注意:distinct后面假设跟着多个字段,那么distinct会将这些字段看成一个总体来去除反复数据。

6)对检索出来的数据进行排序

order by
{expr | position | c_alias} [ASC | DESC] [NULLS FIRST | NULLS LAST][,....]

能够按字段名称,位置。别名来排序,当中nulls first和nulls last是对空值的处理方式,ASC为降序排列(默认),DESC为升序排列。

WHERE查询
where字句中使用的操作符主要有:关系操作符,比較操作符,逻辑操作符。
关系操作符有:<,>,<=,>=,=,!=,<>
比較操作符有:IS NULL,LIKE,BETWEEN…AND…,IN
逻辑操作符有:AND,OR。NOT

7)使用单一限制条件查询

select productname,quantity from productioninfo where quantity > 20

8)使用多个限制条件查询

select productname,quantity form productioninfo where
quantity > 20 and quantity < 50
select productname,quantity from productioninfo where 
quantity between 20 and 50

between…and为闭区间。

9)模糊查询
“_”代表一个字符,“%”代表多个字符。

select productname,productprice from productioninfo where
productionname like ‘%三星%’

10)查询条件在某个集合内 in

select productname productprice from productioninfo where
catagory in ('010030002','0100010001')

使用字查询
在非常多情况下,where后面的条件不是一个确定的值。而是从另外一个查询语句中的查询结果,这时就要用到子查询。
子查询不仅仅出如今select句子中。也出如今delete和update语句中。

11)子查询返回单行记录

select productname, productprice from productinfo where category = (select categoryid from categoryinfo where categoryname = 'MP3');

12)子查询返回多行数据(in,any some,all)
ANY:标示满足子查询结果中的随意一个。与<,<= 或 >,>=连用
SOME:使用方法与ANY同样
ALL:标示满足子查询中的全部结果
IN:等于子查询中的随意一个

eg:
in:

select productname, productprice from productioninfo
where category in (select categoryid from categoryinfo where categoryname = 'TV' or categoryname = 'MP3')

any:
重产品表中查询出价格低于指定价格列表中的最大值

select productname, productprice from productioninfo
where productprice < any(select productprice from productionifno where category='010003002') and category
<> '010003002'

some:

select productname, productprice from productioninfo
where productprice = some(selct productprice from productinfo where category = '010003002'and category <> '010003002'

all:检索数比指定价格还低的数据

select productname, productprice from productioninfo where 
productprice < all(select productprice from productinfo where category = '010003002')
  • 最后。是连接查询

    在数据库设计时。我们一般会吧现实世界的数据依照某种规则拆分成独立的数据,而这些独立的数据会依照拆分规则进行联结。当从数据库中查询现实世界须要的数据时,就要依照拆分规则进行查询,而这样的规则就是表与表之间的关系。
    实如今存在关系的表之间进行查询。就须要连接查询。

连接查询的分类:内连接、外连接、全连接和自连接。

1)最简单的内连接

select * from productinfo,categoryinfo

这样的查询结果是两张表的笛卡尔ji,实际情况中。这样的结果没有太大的意义。

2)等值内连接
查询出productinfo 和 categoryinfo中产品类型编码一致的数据

select p.productname,p.productprice,c.categoryname from 
productinfo p, categoryinfo c where p.category = c.categoryid

或(inner join … on ..)

select p.productname, p.productprice,c.categoryname from 
productinfo p inner join categoryinfo c on p.category = p.categoryid

3)不等值内连接
使用方法与等值连接同样,将=改为>,>=,<,<=,<>,!=

4)自连接
获取表productinfo中数量同样。不同产品的记录

select p.productname,p.productprice,pr.productname,pr.productprice from productinfo p, productinfo pr where p.quality = pr.quality and p.rowid < pr.rowid

5)外连接

 左外连接:返回左表的全部记录和右表符合条件的全部记录
 右外连接:
 全外连接:返回全部匹配成功的记录。并同一时候返回左、右表未匹配成功的记录

eg:
左外连接
查询出productinfo表中每个产品相应的产品类型

select p.productname, p.productprice, p.category, c.categoryid, c.categoryname from productinfo p
left join categoryinfo c on p.category = c.categoryid

右外连接:略

全外连接:用全外连接对产品类型编码进行匹配

select p.productname, p.productprice p.category , c.categoryid ,c.categoryname from productinfo p full join categoryinfo on p.category = c.categoryid
原文地址:https://www.cnblogs.com/tlnshuju/p/7226045.html