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
SQL-SQL基础的更多相关文章
- [SQL] SQL学习笔记之基础操作
1 SQL介绍 SQL 是用于访问和处理数据库的标准的计算机语言.关于SQL的具体介绍,我们通过回答如下三个问题来进行. SQL 是什么? SQL,指结构化查询语言,全称是 Structured Qu ...
- SQL server基础知识(表操作、数据约束、多表链接查询)
SQL server基础知识 一.基础知识 (1).存储结构:数据库->表->数据 (2).管理数据库 增加:create database 数据库名称 删除:drop database ...
- 数据库开发基础-SQl Server 基础
SQL Server 基础 1.什么是SQL Server SQL:Structured Query Language 结构化查询语言 SQL Server是一个以客户/服务器(c/s)模式访问.使 ...
- 【SQL Server】SQL Server基础之存储过程
SQL Server基础之存储过程 阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储 ...
- Sql Server 基础知识
Sql Server 基础知识: http://blog.csdn.net/t6786780/article/details/4525652 Sql Server 语句大全: http://www.c ...
- Oracle数据库编程:PL/SQL编程基础
2.PL/SQL编程基础: PL/SQL块: declare 定义部分 begin 执行部分 exception 异 ...
- Oracle Pl/SQL编程基础
Pl/SQL简介 提高应用程序的运行性能, 提供模块化的程序设计, 自定义标示符, 具有过程语言控制结构, 良好的兼容性, 处理运行错误. Pl/SQL语言基础 sql是关系数据库的基本操作语言. s ...
- SQL Tuning 基础概述10 - 体会索引的常见执行计划
在<SQL Tuning 基础概述05 - Oracle 索引类型及介绍>的1.5小节,提到了几种"索引的常见执行计划": INDEX FULL SCAN:索引的全扫描 ...
- SQL数据库基础知识-巩固篇<一>
SQL数据库基础知识-巩固篇<一>... =============== 首先展示两款我个人很喜欢的数据库-专用于平时个人SQL技术的练习<特点:体积小,好安装和好卸载,功能完全够用 ...
- oracle PL/SQL语法基础
目录 数据类型 定义变量 PL/SQL控制结构 参考资料 Oracle10g数据类型总结 PL/SQL之基础篇 数据类型 学习总结 字符类型 char.nchar.varchar.nvarchar:有 ...
随机推荐
- 磁盘爆满导致MySQL无法启动:Disk is full writing './mysql-bin.~rec~' (Errcode: 28). Waiting for someone to free space...
今天收到监控邮件说博客访问失败.打开页面一看,硕大的502 Bad Gateway,ping了一下VPS发现是通的,SSH连接上去看了下Nginx日志发现没问题,重启lnmp的时候发现Mysql起不来 ...
- 改变PS1变量的颜色
2016.1.11今天学了改变PS1的颜色,怎么增加PS1变量找到文件(.bash_profile),或者bashrc export PS1="\[\e[32;1m\]Test $PWD&g ...
- ylbtech_dbs_article_五大主流数据库模型
ylbtech_dbs_article 摘要:什么是数据模型? 访问数据库中的数据取决于数据库实现的数据模型.数据模型会影响客户端通过API对数据的操作.不同的数据模型可能会提供或多或少的功能.一般而 ...
- 排序效率小PK
上个小demo看看 import com.google.common.primitives.Chars; import java.util.*; /** * java中数组转List使用Arrays. ...
- Java之JDBC学习
(一),MySql数据库 1,MySql数据库的数据类型定义 2,完整性约束: 3,索引: 作用:唯一作用就是加快对表查询速度,索引通过快速路径方法访问来快速定位数据,从而减少磁盘的II/O; 缺点: ...
- SSO单点登录系列6:cas单点登录防止登出退出后刷新后退ticket失效报500错
这个问题之前就发现过,最近有几个哥们一直在问我这个怎么搞,我手上在做另一个项目,cas就暂时搁浅了几周.现在我们来一起改一下你的应用(client2/3)的web.xml来解决这个2b问题,首先看下错 ...
- Node.js 读取博客首页并获得文章标题
app.js // 内置http模块,提供了http服务器和客户端功能 var http=require("http"); // 内置文件处理模块 var fs=require(' ...
- JAVA Eclipse如何开发Android的多页面程序
Fragment可以认为是Activity的一个界面的组成部分,Fragment必须依存于Activity. 在layout文件夹中新建一个xml文件,布局方式采用RelativeLayout,注 ...
- 在HTML页面中实现一个简单的Tab
参考:http://blog.sina.com.cn/s/blog_6cccb1630100m23i.html HTML页面代码如下: <!DOCTYPE html PUBLIC "- ...
- 【Shell】建立一个脚本统计当前登录用户数
who命令 who命令是显示目前登陆系统的用户信息,执行who命令可以得知目前哪些用户登入系统,单独执行who命令会列出登入账号,使用的终端机,登入的时间以及从何处登入或正在使用哪个显示器. 统计用户 ...