您的位置:首页 > 数据库

SQL-SQL基础

2015-08-21 12:43 337 查看
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


注:在查询所有字段的数据时,尽量不要用*,第一因为效率比较低,第二当表结构发生变化时,容易导致程序异常,第三用具体字段可以减少网络开销。

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: