美文网首页java技术贴
ORACLE数据库开发(SQL基础语法)

ORACLE数据库开发(SQL基础语法)

作者: 忠胜 | 来源:发表于2017-09-28 19:50 被阅读0次

    课程来源:慕课网-Oracle数据库开发必备利器之SQL基础; 课程地址:http://www.imooc.com/learn/337
    这是我所学习总结的笔记,新手报到,有不对的地方请留言指教。

    Oracle数据库开发之函数

    一、用户

    1.登录用户:
    username/password

    2.切换用户:
    connect username/password

    3.启用用户语句:
    alter user username account unlock

    4.查看当前用户:
    show user

    二、表空间

    5.创建表空间:
    create [temporary] tablespace tablespace_name tempfile|datafile 'xxx.dbf' size file_size(10M);
    ss
    6.查看表空间位置:
    desc dba_data_files;
    select file_name from dba_data_files where tablespace_name='TABLE_SPACENAME';

    7.设置表空间联机/脱机,只读/读写状态:
    alter tablespace tablespace_name online/offline|read only/read write;

    8.为表空间添加/删除数据文件:
    alter tablespace tablespace_name add/drop datafile 'xxx.dbf' [size 10M];

    9.删除表空间[和表空间的数据文件]:
    drop tablespace tablespace_name [including contents];

    三、管理表修改表

    10.创建表:
    create table table_name
    ( column_name number(6,0),
    column_name varchar2(30)...
    );

    11.为表添加字段:
    alter table table_name add new_column datatype;

    12.更改表字段的数据类型:
    alter table table_name modify column datatype;

    13.删除字段:
    alter table table_name drop column column_name;

    14.修改字段名:
    alter table table_name rename column column_name to new_column_name;

    15.修改表名:
    rename table_name to new_table_name;

    16.截断表(删除表中的数据):
    truncate table table_name;

    17.删除表(删除数据同时表也删除了):
    drop table table_name;

    18.为表添加值:
    insert into table_name (column1,column2,...) values(value1,value2,...);

    19.为表字段设置默认值:
    alter table table_name modify column default default_value;

    20.复制表(创建的时候复制):
    create table table_name as select (column1,columnn3...)|* from copy_table_name;

    21.复制表(插入表的时候复制):
    insert into table_name (column1,...) select (column1,...) from copy_table_name;

    22.修改,更新表:
    update table_name set column_name='***' [where conditions]

    23.删除表数据:
    delete from table_name [where conditions];

    四、约束

    24.非空约束:
    alter table table_name modify column_name datatype NUT NULL;

    25.主键约束:
    1:create table table_name (column column_name primary key,...);(创建表时直接在需要设置主键约束的字段后面添加 primary key)
    2:create table table_name (col col_name,...,constraint constraint_name1 key(col1,col2,...);(创建表时设置了约束名constraint)
    3:修改表时添加主键约束:
    alter table table_name add constraint constraint_name primary key(column);

    26.修改主键名:
    alter table table_name rename constraint constraint_name to new_name;

    27.创建表时添加外键约束:
    create table table2(col2 datatype references table1(col1),...);

    28.创建表时设置外键约束(接连删除):
    create table table2(col_new datatype,..., constraint constraint_name foreign key(col_new) references table1(col)[on DELETE CASCADE]);

    29.修改表时添加外键约束:
    alter table table2 add constraint constraint_name foreign key(col2) references table1(col1);

    30.禁用/删除外键约束:
    alter table table_name disable|enable constraint constraint_name;
    alter table tablename drop constraint constraint_name;

    31.创建唯一约束:(唯一约束的字段可以为null,而主键约束不能;一张表中唯一约束可以为多个,主键约束只能有一个)
    create table table_name (col datatype unique,...);
    或者:create table table_name (col datatype,constraint constraint_name unique(col));

    32.修改表的时候添加唯一约束:
    alter table table_name add constraint constraint_name unique(column);

    33.检查约束:
    create table table_name (col datatype check(expressions),...);

    34.检查约束:
    alter table table_name add constriant constrian_name check(expressions);

    35.删除主键约束:
    alter table table_name drop primary key;

    五、查询

    36.基本查询:
    select [distinct] *|col_name,.. from table_name [where conditions];(distinct去掉重复的记录)

    37.更改查询结果显示的字段名:
    col|column col_name heading col_name_new;

    38.设置查询结果显示的字段长度:
    col|column col_name format a10/999.9(字符/数字的格式例$99.9|¥999.9);

    39.清除设置的字段格式:
    col column_name clear;

    40.查询的时候更改查询结果字段名的显示:
    select col as new_col,col2 as col2_new from table_name;

    41.运算符优先级:
    比较运算符高于逻辑运算符,not高于and高于or

    42.模糊查询(关键字:like,通配符:,%):
    select * from table_name where username like 'a%'|'a
    _'|'a%';(代表一个字符,%代表一个或者多个字符。这里查询用户名已a开头|其他类型);

    43.范围运算符(between and,in/not in):
    select * from table_name where salary between 800 and 2000;
    select * from table_name where username in('aaa', 'bbb');

    44.对查询结果进行排序(order by...desc/asc):
    select ... from table_name [where] order by column1 desc/asc,...

    45.case...when...then语句:
    例1:select username,case username when 'aaa' then '计算机部门' when 'bbb' then '市场部门' else '其他部门' end as 部门 from table_name;
    例2:select salary,case when salary<1000 then '底薪阶层' when salary between 1000 and 3000 then '中等阶层' when salary>5000 then '高新阶层' else '其他' end as 薪资阶层 from table_name;

    46.decode函数:
    select username,decode(username,'aaa','计算机部门','bbb','市场部门','其他')as 部门 from table_name;


    结束语:合抱之木,生于毫末; 九层之台,起于累土; 千里之行,始于足下。

    相关文章

      网友评论

        本文标题:ORACLE数据库开发(SQL基础语法)

        本文链接:https://www.haomeiwen.com/subject/zgpvextx.html