zoukankan      html  css  js  c++  java
  • ORACLE增删改查以及case when的基本用法

    1.创建table

    create table test01(
           id int not null primary key,
           name varchar(8) not null,
           gender varchar2(2) not null,
           age int not null,
           address varchar2(20) default ‘地址不详’ not null,
           regdata date
    );

    约束

    非空约束  not null

    主键约束  primary key

    外键约束

    唯一约束  unique

    检查约束  check

    联合主键
      constraint pk_id_username primary key(id,username);
    查看数据字典
      desc user_constraint
    修改表时重命名
      rename constraint a to b;
    
    --修改表删除约束--
    禁用约束
      disable
    constraint 约束名字; 删除约束
      drop constraint 约束名字;
      drop primary key;直接删除主键
    外键约束
    create table typeinfo(typeid varchar2(20) primary key, typename varchar2(20));
    create table userinfo_f( id varchar2(10) primary key,username varchar2(20),typeid_new varchar2(10) references typeinfo(typeid));
    insert into typeinfo values(1,1);
    
    创建表时设置外键约束
    constraint 名字 foregin
    create table userinfo_f2 (id varchar2(20) primary key,username varchar2(20),typeid_new varchar2(10),constraint fk_typeid_new foreign key(typeid_new) references typeinfo(typeid));
    create table userinfo_f3 (id varchar2(20) primary key,username varchar2(20),typeid_new varchar2(10),constraint fk_typeid_new1 foreign key(typeid_new) references typeinfo(typeid) on delete cascade);
    
    外键约束包含
    删除外键约束
    禁用约束 disable constraint 约束名字;
    删除约束 drop constraint 约束名字;
    唯一约束 与主键区别 唯一约束可以有多个,只能有一个null
    create table userinfo_u( id varchar2(20) primary key,username varchar2(20) unique,userpwd varchar2(20));
    
    创建表时添加约束
    constraint 约束名字 unique(列名);
    修改表时添加唯一约束 add constraint 约束名字 unique(列名);
    
    检查约束
    create table userinfo_c( id varchar2(20) primary key,username varchar2(20), salary number(5,0) check(salary>50));
    constraint ck_salary check(salary>50);
    
    /* 获取表:*/
    select table_name from user_tables; //当前用户的表
    select table_name from all_tables; //所有用户的表
    select table_name from dba_tables; //包括系统表
    select table_name from dba_tables where owner=’zfxfzb’
    
    /*

    2.修改表

    alter table test01 add constraint s_id primary key;
    
    alter table test01 add constraint CK_INFOS_GENDER check(gender=’男’ or gender=’女’)
    
    alter table test01 add constraint CK_INFOS_AGE(age>=0 and age<=50)
    
    alter table 表名 modify 字段名 default 默认值; //更改字段类型
    
    alter table 表名 add 列名 字段类型; //增加字段类型
    
    alter table 表名 drop column 字段名; //删除字段名
    
    alter table 表名 rename column 列名 to 列名 //修改字段名
    
    rename 表名 to 表名 //修改表名

    3.删除表格

    truncate table 表名 //删除表中的所有数据,速度比delete快很多,截断表
    
    delete from table 条件//
    
    drop table 表名 //删除表

    4.插入语句

    insert into 表名(值1,值2) values(值1,值2);

    5.修改语句

    update 表名 set 字段=[修改条件]
    
    update t_scrm_db_app_user set password = :pwd where login_name = :user

    6.查询语句

    带条件的查询
        where
    
    模糊查询
        like % _
    
    范围查询
        in
    
    对查询结果进行排序
        order by desc||asc

    7.case when

    select username,case username when ‘aaa’ then ‘计算机部门’ when ‘bbb’ then ‘市场部门’ else ‘其他部门’ end as 部门 from users;
    
    select username,case username=’aaa’ then ‘计算机部门’ when username=’bbb’ then ‘市场部门’ else ‘其他部门’ as 部门 from users;

    8.运算符和表达式

    算数运算符和比较运算符

      distinct 去除多余的行

      column 可以为字段设置别名 比如 column column_name heading new_name

      decode 函数的使用 类似于case…when

      select username,decode(username,’aaa’,’计算机部门’,’bbb’,’市场部门’,’其他’) as 部门 from users;

    9.复制表

    create table 表名 as 一个查询结果 //复制查询结果

    insert into 表名 值 一个查询结果 //添加时查询

    10.查看表空间

    desc test01;

     11.创建表空间

    永久表空间
    create tablespace test1_tablespace datafile ‘testfile.dbf’ size 10m;
    
    临时表空间
    create temporary tablespace temptest1_tablespace tempfile ‘tempfile.dbf’ size 10m;
    
    desc dba_data_files;
    
    select file_name from dba_data_files where tablespace_name=’TEST1_TABLESPACE’;
  • 相关阅读:
    windows中administrator 和 administrators两个账户的区别
    如何去掉打印网页时自带的网址以及页码等内容
    Oracle左连接,右连接
    oracle服务器本地能够登录但是局域网内其他机器不能访问的解决方法
    错误Name node is in safe mode的解决方法
    oracle数据库中对varchar类型求max的解决方法
    JBoss中配置数据源出现错误:“Failed to register driver for: com.mysql.jdbc.Driver”的解决方法
    学习junit和hamcrest的使用
    Ubuntu10.10如何给用户添加sudo权限
    ORACLE 9i卸载并重新安装
  • 原文地址:https://www.cnblogs.com/xuhai/p/9180661.html
Copyright © 2011-2022 走看看