sql编程基础总结

文章转载至http://blog.csdn.net/liangtianmeng/article/details/64129639

作为了解储备基础知识

SQL是访问和处理数据库的标志的计算机语言,SQL是结构化查询语言(Structured Query Language),常用数据库包括,Access,MySql,Oracle,Sql Server,Sybase,DB2等。

一个数据库一般含有一个或多个表,每个表由一个名字标识,表包含带有数据的记录航

常用的SQL编程语法:下面以RUNOOB 数据库中创建了 Websites 表为例

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | 菜鸟教程      | http://www.runoob.com/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
+----+--------------+---------------------------+-------+---------+

sql对大小写不敏感,语句最好以分号结尾。

1、select用于从数据库表中选取数据,结果被放在结果集中。select name, url from Websites;

2、distinct 用于返回列中不同的值。select distinct contry form Websites;

3、where用于字句条件判断,过滤记录。select name, url, alexa from Websites where alexa > 20; (<> like between in not and or is null)

4、and和or 逻辑与和或,select * from Websites where alexa > 20 and country ='CN';

5、order by 对结果集进行排序,select * from Websites where alexa > 20 order by alexa;其中默认ase升序,desc降序

6、insert into 向表中插入新数据,insert into Websites(name,url,alexa,country)values(‘aaa’,'www.baidu.com','39','USA');

7、update更新表中记录,update Websites set alexa='39', country = 'USA' where name = 'Google';

8、delete删除表中记录,delete table Websites where name = 'Google';

9、limit或rownum 限制结果集个数  select * from Websites limit 2;

10、like 模糊匹配,select * from Websites where name like 'G%';

11、通配符,%表示0个或多个字符, _表示一个字符,[charlist]字符列中任意一个字符,[^charlist]或[!charlist]不在字符列中的任意一个字符

select * from Websites where name like '[!abc]%'

12、in,允许where字句中规定多个值 select * from Websites where alexa in (1,2,20);

13、between,筛选出用于两个临界值范围内的值,select * from Websites where alexa between 1 and 20;

14、as,用于声明别名,select name a, country b from Websites; select w.name, w.country form Websites w where w.alexa = 20;

15、join,分为left jion, right jion, full join, join四种类型,

left join左连接,返回左表所有的行

right join 右连接,返回右表所有行

full join 全连接,返回两表所有行

inner join 内连接,返回两表匹配行 select Websites.id, Websites.name, access_log.count, access_log.date from Websites inner join access_log on Websites.id =access_log.id;

16、union 合并两个或多个select语句的结果,union中的每个select语句必须是相同数量的列,列的数据类型相同,列的顺序一致

select country from Websites union select country from Access order by country;

17、select into 从一个表复制数据,然后粘贴到新表中,select country , name into newtable from Websites;

18、insert into select 从一个表复制数据粘贴到已存在的表中,insert into Websites(name, country) select app_name,app_country from App;

19、create database 创建数据库  create database b;

20、create table 创建数据库表,create table Websites (name varchar(222), country varchar(20));

21、约束 用户数据库中的数据规则:

not null 不能为空

unique 每行的唯一值

primary key 主键 not null和unique的结合,用于唯一标识

foreign key 外键 保证一个表的数据匹配另一表中的值的参照完整性

check 检查列中的值是否符合指定条件

default 没有赋值时的默认值

create table App (id int not null primary key, name varchar(225) not null, country varchar(200) not null default 'CN', p_id int not null check (p_id > 0),

constraint fk_cons foreign key (p_id) references  Websites(id));

22、create index 创建索引,在不读取整个表的情况下,能够快速的查找数据

create index index_name on table Websites(country);

23、drop 删除索引,表,数据库,drop index index_name;  drop table table_name; drop database database_name;

24、alter table 添加,删除及修改列,alter table Websites add app_name varchar(20); alter table Websites modify column name int; alter Websites drop columnapp_name;

25、auto increment自增张 oracle中表示为sequence create sequence name minvalue 1 start with1 increment 1 cache 10;  oracle中使用sequence需要使用nextval字段

26、视图,sql语句结果集的可视化的表 create view view_name as select name,country from Websites;

27、null值,插入一条不带值的记录,此时表中的值会使用null保存  用is null ,is not null来选取数据

28、oracle中用NVL(value,0)函数来判断是否是null值

29、sql通用数据类型

数据类型 Access SQLServer Oracle MySQL PostgreSQL
boolean Yes/No Bit Byte N/A Boolean
integer Number (integer) Int Number Int
Integer
Int
Integer
float Number (single) Float
Real
Number Float Numeric
currency Currency Money N/A N/A Money
string (fixed) N/A Char Char Char Char
string (variable) Text (<256)
Memo (65k+)
Varchar Varchar
Varchar2
Varchar Varchar
binary object OLE Object Memo Binary (fixed up to 8K)
Varbinary (<8K)
Image (<2GB)
Long
Raw
Blob
Text
Binary
Varbinary

30、group by 对结果集分组

31、having 可以对分组后的数据进行筛选

SELECT Websites.name, Websites.url, SUM(access_log.count) AS nums FROM (access_log
INNER JOIN Websites
ON access_log.site_id=Websites.id)
GROUP BY Websites.name
HAVING SUM(access_log.count) > 200;

猜你喜欢

转载自blog.csdn.net/w_han__/article/details/79540098