数据库语言SQL是高级的非过程化编程语言,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以不同底层结构的数据库系统可以使用相同的SQL语言作为数据输入与管理的接口。(我的简单理解:SQL就是与数据库聊天的工具)

SQL的分类

一、DDL(针对数据库对象,以表为例)

创建表:

①、
SQL> create table student
  2  (id number(4),                      
  3  name varchar2(20),
  4  birthday date);

Table created.

SQL> desc student;
 Name					   Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID						        NUMBER(4)
 NAME						    VARCHAR2(20)
 BIRTHDAY					    DATE


②、
SQL> create table student as select * from stu where 0=1;      //直接复制表结构

Table created.

③、
SQL> create table student as select * from stu where 1=1;       //复制表结构和数据

Table created.

修改表:

SQL> alter table student rename to stu1;       //表重命名


SQL> alter table stu1 rename column name to score;     //列重命名


SQL> alter table stu1 modify score number(3);      //修改列的数据类型


SQL> alter table stu modify(class default 1);      //设置默认值


SQL> alter table stu1 add name varchar2(20);       //添加列


SQL> alter table stu drop column id;        //删除列

删除表:

SQL> drop table stu1;

二、DML

插入:

SQL> insert into stu (name,class) values('H',1);      //指定列插入,注意字段和值的对应关系


SQL> insert into stu  values('S',1);  //按表的字段顺序插入,注意数量、类型、顺序要和表的字段一致


SQL> insert into stu  (class) values(3);  //指定字段插入,未指定的默认为空



SQL> select * from stu;

NAME			  CLASS
-------------------- ----------
H			      1
S			      1
			      3

修改:

SQL> update stu set name='L';

3 rows updated.

SQL> select * from stu;

NAME			  CLASS
-------------------- ----------
L			      1
L			      1
L			      3

SQL> update stu set name='P' where class=3;

1 row updated.

删除:

SQL> delete from stu where name='P';        //指定行删除

1 row deleted.

SQL> delete from stu;      //删除所有表数据

2 rows deleted.

SQL> truncate table stu;     //删除所有表数据

Table truncated.

三、DQL(select)

select .. from .. 
where ..
group by ..  (having..)
order by .. 

查询此用户有哪些表:
    SCOTT@myh>select * from tab;
查看表内容:
    SCOTT@myh>select * from emp;

①、NULL:    
    不确定,不存在,未知
    空值不等于0,不等于空白    
    空值与任何数字运算结果为空
  对于NULL的处理:
    nvl(列名,数值) 将此列中的空值替换为某个数值,数值必须是此列的属性
    例:
    SCOTT@myh>select ename,nvl(comm,'a') from emp;         错误
    因为:COMM    NUMBER(7,2)   comm列为number类型,所以替换必须为数字
    SCOTT@myh>select ename,nvl(comm,0) from emp;      正确

②、字符串链接:
    SQL> select ename||'salary is '|| sal from emp;
  特殊字符处理:Q'['string]'
    SQL> select ename||Q'['salary is ]'|| sal from emp;

③、去重:
    SQL> select distinct deptno from emp;

④、函数

    大写变小写:lower()
    小写变大写:upper()
    SQL> select lower(ename) from emp;
    SYS@myh>select username,account_status from dba_users where lower(username)='scott';

⑤、比较操作符
    >
    <
    >=
    <=
    <> != ^=
    between...and        <=....<=....
    in(a,b,c,...)    

SQL> select ename,sal from emp where sal between 1100 and 2450;  

ENAME                SAL
-------------------- ----------
ALLEN               1600
WARD               1250
MARTIN               1250
CLARK               2450
TURNER               1500
ADAMS               1100
MILLER               1300
 

⑥、模糊查找 like
    %  多个字符
    _  一个字符
    例:第二个字符占L
        SQL> select * from emp where ename like '_L%';

⑦、is null
    is not null
    例:奖金为空:
    SQL> select * from emp where comm is null

⑧、转译:escape
    SQL> select * from emp where ename like 'A/_%' escape '/';     //查询以A_开头的名字,而_为特殊字符

⑨、优先级:
    not>and>or    
    例:
    SQL> select * from emp where job='CLERK' or job='MANAGER' and sal>1250;
    等于  SQL> select * from emp where job='CLERK' or (job='MANAGER' and sal>1250);
四、DCL (授予权限)

SQL> grant create session to test;

Grant succeeded.

SQL> revoke create session from test;

Revoke succeeded.