【数据库】SQL语言基础

SQL:结构化查询语言,数据库操作的国际标准语言。

SQL分为数据定义(CREATE、ALTER、DROP)、数据查询(SELECT)、数据操纵(INSERT、DELETE、UPDATE)

数据控制(GRANT、REVOKE、COMMIT、ROLLBACK)。

SQL关键字、对象名、列名不区分大小写。

1.按某一列(column)升序排列

select * from table order by column;

select * from scott.emp order by sal;

2.删除表(表的结构、属性以及索引也会被删除)

drop table 表名称;

3.删除表中所有记录(记录清空,表结构还在)【效率比delete高】

truncate table 表名称;

4.删除表中某条/所有记录(记录清空,表结构还在)

delete from 表名称;

delete from 表名称 where 列名称=值;

5.查询表中某列/所有记录

select * from 表名称;

select 列名称 from 表名称;

select * from scott.emp;
select sal from scott.emp;

6.创建表结构

create table dwq_test

(

   id              number(7) not null,

   name            varchar2(20) not null,

   email           varchar2(20) not null,

   status          number(1) not null,

   primary key (id)

);

 create table dwq_test
  (
  id number(7) not null,
  name varchar2(20) not null,
  email varchar2(20) not null,
  status number(1) not null,
  primary key(id)
  );

7.插入表记录

insert into dwq_test (id,name,email,status) values (1,'dwq','[email protected]',1);

insert into dwq_test (id,name,email,status) values (1,'dwq','[email protected]',1);

8.查询表所有记录

select * from dwq_test;

select * from dwq_test;

9.更新表记录中的列

【更新记录中的一列】update dwq_test set name='jtd' where name='dwq';

update dwq_test set name='jtd' where name='dwq';

【更新记录中的多列】update dwq_test set name='lch', email='[email protected]', status=0 where id=1;

update dwq_test set name='lch', email='[email protected]', status=0 where id=1;

10.删除表中记录

delete from dwq_test where id=1;

delete from dwq_test where id=1;

11.排序记录【不可同时排序多个,order by后只能排序一列】

【默认升序排列(省略结尾的ASC),字母/数字】

<1>select * from dwq_t order by email;

<2>select * from dwq_t order by email ASC;

注:<1>和<2>等价。

【指定逆序排列,字母/数字】select * from dwq_t order by email DESC;

12.使用多条件查询记录

【and只能查询一条记录】select * from dwq_t where id=1 and name='dwq';

【or查询多条记录】select * from dwq_t where id=1 or name='jtd';

【and和or同时出现,需要加括号判定结合型】select * from dwq_t where id=1 or (name='lch' and name='dwq');

13.去掉一个列中重复的记录【只能去掉一个列】

select distinct email from dwq_t;

14.PL/SQL中修改表数据

【加表名.rowid才能修改数据】select dwq_t.* , dwq_t.rowid from dwq_t;

15.查询表中前两行记录

select * from dwq_tt where rownum <=2;

16.查询一列中满足前缀/后缀的记录

【满足前缀】select * from dwq_tt where email like '570%';

【满足后缀】select * from dwq_tt where email like '%qq.com';

【满足一段(模糊查询)】select * from dwq_tt where email like '%0045%';

【不满足一段(模糊查询)】select * from dwq_tt where email not like '%0045%';

【用_代替一个字符,查询第一个字符之后的字母,不适用于数字】select * from dwq_ttt where name like '_wq';

【用_代替多个字符,不适用于数字】select * from dwq_ttt where name like '_w_';

17.以某一列/多列为主进行组合

【一列为主组合】select Customer,sum(OrderPrice) from test_customer1 group by Customer;

【多列为主组合】

<1>select Customer,sum(OrderPrice) from test_customer1 group by Customer,OrderPrice;【】

<2>select Customer,OrderPrice from test_customer1;【插入数据顺序】

<3>select Customer,sum(OrderPrice) from test_customer1 group by OrderPrice;【报错,不能以OrderPrice为主】

注:查询结果顺序不同。

原表顺序:

语句<1>顺序:

语句<2>顺序:

18.有函数时使用where条件查询【where 和 函数不能同时出现】

select Customer,sum(OrderPrice) from test_customer1 group by Customer having sum(OrderPrice)>3000;

19.返回数据记录的条数

【根据表中某一列(没必要)】select count(OrderPrice) from test_customer1;

【根据表中某一列中满足条件的记录的条数(把列名“count(Customer)”改为“orders”)】select count(Customer) as orders from test_customer1 where Customer='dwq';

【表中所有记录的条数】select count(*) from test_customer1;

【根据某一列的值(不重复计数)】select count(distinct Customer) from test_customer1;

20.求平均数【NULL 值不包括在计算中】

【非嵌套】select avg(orderprice) as avg from test_customer1;

【嵌套】select customer from test_customer1 where orderprice>(select avg(orderprice) from test_customer1);

21.查询某一列的第一条/最后一条记录【oracle中无first/last关键字】

【oracle报错】select first(orderprice) as first_orderprice from test_customer1;

【oracle报错】select last(orderprice) as last_orderprice from test_customer1;

22.求最大/小值【NULL 值不包括在计算中】

【比较一列的最大值】select max(orderprice) as max_orderprice from test_customer;

【比较一列的最小值】select min(orderprice) as min_orderprice from test_customer;

【比较一行(即多个列表)的最大值和最小值】

select id, chinese, math, english, greatest (chinese, math, english) max,least(chinese, math, english) min from score;

23.oracle注释:【”--“后为注释】

select * from test_customer;   --查询

24.返回多列第一个非空值

select coalesce (chinese,math,english) from score;

25.字母大小写转换

【大写】select upper(customer) as Upname from test_customer;

【小写】select lower(customer) as Upname from test_customer;

26.求和

【某一列】select sum(orderprice) as sum_orderprice from test_customer;

27.

select * from test_customer;

select customer,max(orderprice) from test_customer;

(不是单组分组函数)

【改为:select customer,max(orderprice) from test_customer group by customer;】

28.check约束

CREATE TABLE Persons ( Id_P int NOT NULL CHECK (Id_P>0), LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) )

29.default默认值

CREATE TABLE Persons ( Id_P int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) DEFAULT 'Sandnes' )

30.unique不可重复

CREATE TABLE Persons ( Id_P int NOT NULL UNIQUE, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) )

31.级联删除【如果主表记录删除,则主表中作为外键的记录也删除】

 表a保持不变 

create table a

(

    id varchar(20) primary key,

    password varchar(20) not null

);

 表b外键信息中加入级联删除 

create table b

(

name varchar(50) not null,

userId varchar(20),

foreign key (userId) references a(id) on delete cascade

);

32.case when

http://www.cnblogs.com/eshizhan/archive/2012/04/06/2435493.html

33.decode

http://www.cnblogs.com/vinsonLu/p/3512526.html

http://jingyan.baidu.com/article/3a2f7c2e16162a26afd611be.html

34.over

partition by是批量分组,而group by是对某一个分组。

http://blog.csdn.net/an342647823/article/details/9039339

http://www.cnblogs.com/shined/archive/2013/01/16/2862809.html

http://www.cnblogs.com/wuyisky/archive/2010/02/24/oracle_over.html

http://www.cnblogs.com/sumsen/archive/2012/05/30/2525800.html

35.nvl

nvl(参数1,参数2)

当参数1为空时,值为参数2;

当参数1不为空时,值为参数1.

http://blog.sina.com.cn/s/blog_46e9573c01015ik8.html

36.日期函数

【获取系统日期】select sysdate from dual;

【把系统日期转换成字符串】select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual;

【将日期字符串转换为日期类型]】insert into emp(hiredate) values(to_date('2004-10-10','yyyy-mm-dd'));

【将日期字符串转换成数字类型】select to_number(to_char(sysdate,'hh12')) from dual; 

http://www.cnblogs.com/chuncn/archive/2009/04/29/1381282.html

37.字符串截取

select substr('abcdef',1,3) from dual;

38.查找子串位置

select instr('abcfdgfdhd','fd') from dual ;

39.连接字符串

select 'HELLO'||'hello world' from dual;

40.字符串长度

select length('abcdef') from dual

41.修改列类型

【新类型和旧类型兼容,旧类型列值为空】alter table 表名 modify(列名 新类型);

【新类型和旧类型不兼容,旧类型有列值】

//修改旧列名

alter table 表名 rename column 旧列名 to 新列名;

//增加与旧列名相同的列

alter table 表名 add 列名 列类型;

//将修改后的列值更新到增加的列中

update 表名 set 增加的列名=trim(修改后的列名);

//删除修改后的列

alter table 表名 drop column 修改后的列名;

42.sign

oracle sign 函数  取数字n的符号,大于0返回1,小于0返回-1,等于0返回0

SQL> select sign(100),sign(-100),sign(0) from dual;

sql执行顺序

  1. FROM:对FROM子句中前两个表执行笛卡尔积生成虚拟表vt1

  2. ON: 对vt1表应用ON筛选器只有满足 join_condition 为真的行才被插入vt2

  3. OUTER(join):如果指定了 OUTER JOIN保留表(preserved table)中未找到的行将行作为外部行添加到vt2,生成t3,如果from包含两个以上表,则对上一个联结生成的结果表和下一个表重复执行步骤和步骤直接结束。

  4. WHERE:对vt3应用 WHERE 筛选器只有使 where_condition 为true的行才被插入vt4

  5. GROUP BY:按GROUP BY子句中的列列表对vt4中的行分组生成vt5

  6. CUBE|ROLLUP:把超组(supergroups)插入vt6,生成vt6

  7. HAVING:对vt6应用HAVING筛选器只有使 having_condition 为true的组才插入vt7

  8. SELECT:处理select列表产生vt8

  9. DISTINCT:将重复的行从vt8中去除产生vt9

  10. ORDER BY:将vt9的行按order by子句中的列列表排序生成一个游标vc10

  11. TOP:从vc10的开始处选择指定数量或比例的行生成vt11 并返回调用者

猜你喜欢

转载自blog.csdn.net/qq_23996157/article/details/82712543