1.建表语句
CREATE TABLE test1 (cust_nbr number(5) NOT NULL, region_id Number(5) NOT NULL, salesperson_id number(5) NOT NULL, YEAR number(4) NOT NULL, MONTH number(2) NOT NULL, tot_orders number(7) NOT NULL, tot_sales number(11,2) NOT NULL )
--用select创建表 CREATE TABLE NEW_dept AS SELECT * FROM dept;--将已知表的结构和数据都复制到新表中 CREATE TABLE NEW dept AS SELECT * FROM dept WHERE 1=2;--只复制表的结构不复制数据
1.1插入
INSERT INTO SALGRADE VALUES (1,700,1200);
1.2创建新用户
CREATE USER 用户名 IDENTIFIED BY 密码;
1.3赋予权限
GRANT 权限,权限 TO 用户;
GRANT CREATE SESSION,CREATE TABLE,CREATE sequence,CREATE View,CREATE PROCEDURE TO 表名
CREATE SESSION(创建会话) CREATE TABLE (创建表) CREATE sequence(创建序列) CREATE VIEW (创建视图) CREATE procedure(创建存储过程)
CREATE ROLE manager;--创建角色 GRANT CREATE TABLE,CREATE VIEW TO manager;--为角色赋予权限 GRANT manager TO dehaan,kochhar--将角色赋予用户
GRANT UPDATE (department NAME,LOCATION ID ) ON departments TO scott,manager--分配表中各个列的更新权限 GRANT SELECT,INSERT ON dapartments TO scott WITH GRANT OPTION;--with grant option 使用户同样具有分配权限的权利 GRANT SELECT ON scott.departments TO icss;
收回权限
--收回权限 REVOKE SELECT,INSERT ON departments FROM scott;--使用revoke语句收回权限
1.4更改密码
ALTER USER 用户名 IDENTIFIED BY 密码;--更改用户密码
1.5 更改表名
RENAME 原表名 TO 新表名
1.6更新
update 表名 set 列名=新值 where条件; select语句 for update;
1.7添加备注
COMMENT ON TABLE es_invc_hdr_temp IS '发票信息临时表'; COMMENT ON COLUMN es_invc_hdr_temp.ID IS '主键(自动生成)'; COMMENT ON COLUMN es_invc_hdr_temp.ORDER_KEY IS '订单orderkey';
1.8查询备注
SELECT TABLE_NAME, COLUMN_NAME, COMMENTS FROM USER_COL_COMMENTS WHERE TABLE_NAME ='ES_INVC_HDR_TEMP';
2.基本语句
1.desc 表名--显示表的结构 2.drop table 表名 alter table 表名 drop column 列名称;--删除表中的一个字段 3.decode(value, if1, then1, if2,then2, if3,then3, . . . else);--与case...when ..类似,value表示要判断的值,if表示条件then表示结果;decode(instr(FACTOR,'NO'),'0','0','1')NO_COUNT表示在FACTOR中查找NO返回是0就给字段NO_COUNT设置为0否则设置为1. 4.substr(source,startIndex,length); 5.instr(source,taget,[startIndex],[nth_appearance]);--startIndex,nth_appearance这两个参数不是必须条件,默认返回从index=1开始taget第一次出现的位置,返回的结果值是表示taget字符串的首字母在source的位置,source的index的起始值为1,startIndex表示在source的开始搜索的开始位置,nth_appearance表示查找taget在source中第几次出现。
6.delete from 表名 where 条件--删除表中的所有数据 7.truncate table 表名;--一旦使用了truncate表中数据将不可恢复,删除速度快,当确定表中的数据确实没有用并且表很大的时候可以使用truncate删除(慎用) 8.ALTER TABLE 表名 DROP COLUMN 列名 9.create table student ( studentid int primary key not null, studentname varchar(8), age int); 10.alter table 表名 add (age varchar2(30) default 22 not null)--增加一个字段 11.alter table test1 add (name varchar2(30)default '无名氏' not null,age integer default 22 not null,has_money number(9,2) );--增加多个字段 12.alter table TABLE_NAME rename column FIELD_NAME TO NEW_FIELD_NAME;--修改一个字段 13.alter table test1 drop column name ;删除一个字段 14.SELECT * from user_cons_columns;--查看表的主键
15.创建主键约束
create table student ( studentid int primary key not null, studentname varchar(8), age int);--创建无命名的主键约束
create table students ( studentid int , studentname varchar(8), age int, constraint yy primary key(studentid));--创建有命名的主键约束
4、删除表中已有的主键约束
(1)无命名
SELECT * from user_cons_columns;--查询表中主键名称
alter table student drop constraint SYS_C002715;--删除表中已有的主键约束
(2)有命名
alter table students drop constraint yy;--删除表中已有的主键约束
3、向表中添加主键约束
alter table student add constraint pk_student primary key(studentid);
4。oracle小数点转化为百分数
(round(cpws_matchratio*100,2))||'%'
SELECT *,CASE WHEN cpws_matchratio IS NOT NULL then (round(cpws_matchratio*100,2))||'%' ELSE cpws_matchratio END cpws_matchratio1 FROM F_CIS_INDV_LAW_CPWS