sELECT * FROM qj_test;
恢复数据
create table qj_test2
as
select * from qj_test as of TIMESTAMP to_timestamp('20120711 16','yyyymmdd hh24');
CREATE TABLE qj_test3(
autoid NUMBER(30) NOT NULL PRIMARY KEY,
NAME CHAR(30) NOT NULL,
ID NUMBER(10) NOT NULL,
address CHAR(50) NOT NULL
)
CREATE SEQUENCE temsequence
MINVALUE 1
MAXVALUE 99999999
START WITH 1
INCREMENT BY 3
NOCACHE;
DROP SEQUENCE temsequence
INSERT INTO qj_test3 VALUES('123','qijin',temseq.nextval,'123');
INSERT INTO qj_test3 VALUES(temseq.nextval,'qijin','123','123');//values这六个字母一定要打
正确否则会有不必要的麻烦
SELECT SYSDATE FROM dual
INT类型是NUMBER类型的子类型
commit 的使用
如何解决ORA-00054资源正忙,要求指定NOWAIT /////杀掉进程或者等待执行结束
CREATE SEQUENCE temyear
MINVALUE 1
MAXVALUE 999999
START WITH 2009
INCREMENT BY 1
NOCACHE;
commit 之前其他账号不能看到你更新的数据了
UPDATE qj_test3 SET birth=temyear.nextval WHERE autoid='25';
COMMIT;
CURRVAL 必须在取了NEXTVAL之后才有效。它只表示当前SESSION最后一次取值,不表示这个SEQUENCE(被多个SESSION使用)的当前值。
SELECT temyear.nextval FROM dual //查询到temyear的下一个数值 每查询一次值修改一次
不同的数据库中也成立可以说是同步的
SELECT temyear.nextval-10 FROM dual 可以进行计算
SELECT temseq.currval FROM dual 不能一开始就用需要初始化一下=====第一次执行的时候要next
初始化一下(oracle规定)
第一个循环体的使用
DECLARE
COUNT NUMBER:=1;
countid NUMBER:=16;
LOOP
COUNT:=COUNT+1;
EXIT WHEN COUNT>6;
UPDATE qj_test3 SET birth= temyear.nextval WHERE birth='null';
COMMIT;
END LOOP;
DECLARE
v_COUNT NUMBER:=1;
countid NUMBER:=16;
BEGIN
WHILE v_COUNT<=6
LOOP
UPDATE qj_test3 SET birth= temyear.nextval WHERE autoid=countid;
v_COUNT := v_COUNT + 1;
countid := countid + 3;
COMMIT;
END LOOP;
END; // 为什么用exit when 不行!(没有找到解决办法)
ALTER TABLE qj_test3
ADD SNO NUMBER(20) //给qj_test3这张表添加了一个属性
ALTER TABLE qj_test3
DROP COLUMN SNO //给qj_test3这张表删除了一个属性
CREATE OR REPLACE TRIGGER trg_test
BEFORE INSERT ON qj_test3
FOR EACH ROW
BEGIN
SELECT temseq.nextval INTO :new.autoid FROM dual; //new. 这个很关键!!
END trg_test
SELECT * FROM All_Triggers a WHERE a.table_name='QJ_TEST3';//查询表对应的触发器
也可在my objects 里查看triggers 就可以看到所有的触发器了!
每张表最多可建立12 种类型的触发器,它们是:
BEFORE INSERT
BEFORE INSERT FOR EACH ROW
AFTER INSERT
AFTER INSERT FOR EACH ROW
BEFORE UPDATE
BEFORE UPDATE FOR EACH ROW
AFTER UPDATE
AFTER UPDATE FOR EACH ROW
BEFORE DELETE
BEFORE DELETE FOR EACH ROW
AFTER DELETE
AFTER DELETE FOR EACH ROW
1. 执行 BEFORE语句级触发器;
2. 对与受语句影响的每一行:
l 执行 BEFORE行级触发器
l 执行 DML语句
l 执行 AFTER行级触发器
3. 执行 AFTER语句级
Compilation errors for TRIGGER AGENT_INTE_NEW.TRG_TEST
Error: PLS-00801: 内部错误 [ph2csql_strdef_to_diana:bind]
Line: 2
Text: FOR EACH ROW
Error: PL/SQL: ORA-06544: PL/SQL: 内部错误, 参数: [ph2csql_strdef_to_diana:bind], [], [], [], [], [], [], []
Line: 4
Text: SELECT temseq.nextval INTO: new.autoid FROM dual;
Error: PL/SQL: SQL Statement ignored
Line: 4
Text: SELECT temseq.nextval INTO: new.autoid FROM dual;
代码一:CREATE OR REPLACE TRIGGER trg_test BEFORE INSERT ON qj_test3
FOR EACH ROW
BEGIN
SELECT temseq.nextval INTO: (空格)new.autoid FROM dual;END;
代码二:CREATE OR REPLACE TRIGGER trg_test
BEFORE INSERT ON qj_test3
FOR EACH ROW
BEGIN
SELECT temseq.nextval INTO:new.autoid FROM dual;
END trg_test;
代码一爆出了三个错误,经过一晚上弱弱地查找,终于把三个错误找出,就是一个没有必要的空格打错了位置!!!!!!! 惨痛的教训啊!!
SQL INNER AND LEFT JOIN 关键字训练!!
ALTERTABLE qj_test4
DROPCOLUMN address
ALTERTABLE qj_test4
ADD qj_size NUMBER(20)
UPDATE qj_test4 SET qj_size=temyear.nextval WHERE autoid!=NULL;
COMMIT; ========有错误更新不了 找不到错误所在? 不知道是不是系统繁忙无法响应!
试验另外一种方法:
UPDATE qj_test4 SET qj_size=temyear.nextval WHERE autoid=37;
COMMIT;
和
UPDATE qj_test4 SET qj_size=temyear.nextval WHERE autoid=’37’;
COMMIT; 此方法是错误的! 因为数字不能用单引号~ 单引号好像是用来匹配字符串的~
UPDATE qj_test4 SET qj_size=temyear.nextval-1000WHERE autoid=16;
COMMIT; ================可以用来更新替换qj_size的原有值!!
按住F8 执行和按住绿色的按钮能commit!!! 才算提交成功!
SELECT *
FROM qj_test3
LEFTJOIN qj_test5
ON qj_test3.autoid=qj_test5.autoid
SELECTDistinct *
FROM qj_test3
LEFTJOIN qj_test5
ON qj_test3.autoid=qj_test5.autoid
DELETE !!!!!!!!!!! ORDER BY TOP == LIKE === IN
SELECT *
FROM qj_test3
ORDERBY birth
DELETEFROM
QJ_TEST3
WHERE birth=1990
SELECT * FROM qj_test
WHERERownum<5 ===========返回前4条记录!!!
SELECT * FROM qj_test3
WHERENAMEIN ('qijin','lby')
SELECT * FROM qj_test5
WHERE autoID BETWEEN16AND25 ====取在16到25之间的AUTOID的所有行
SELECT q.autoid
FROM qj_test3 AS q
WHERE q.autoid ISNOTNULL
=====报错 网上给出的解决办法是 不用AS 直接用 qj_test3
UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。
CREATETABLE qj_test6
AS
SELECT *
FROM
(SELECT * FROM qj_test3
UNIONALL
SELECT * FROM qj_test5)
W3cschool 没有标明 select into 是sql server的写法
SELECT * INTO qj_test7
FROM qj_test6
这是oracle的写法 前提是 qj_test7 存在且字段一致!!!
INSERT INTO qj_test7
SELECT *
FROM qj_test6
SQL PRIMARY KEY Constraint on ALTER TABLE
撤销 PRIMARY KEY 约束
除去表内的数据,但并不删除表本身 TRUNCATE TABLE 表名称
TRUNCATETABLE qj_test5
DROP DATABASE 语句用于删除数据库
DROPTABLE qj_test5