动作 | 语句 |
---|---|
非空约束 | CREATE TABLE TEST_SCHEMA.NOT_Persons(ID int NOT NULL,Name varchar(255) NOT NULL); insert into TEST_SCHEMA.NOT_Persons values(1,‘古天乐’); insert into TEST_SCHEMA.NOT_Persons values(1,‘冯小刚’); commit |
唯一约束 | CREATE TABLE TEST_SCHEMA.PK_Persons (Id_P int,Name varchar(255),Address varchar(255),CONSTRAINT PK_ID_P UNIQUE (Id_P)); insert into TEST_SCHEMA.PK_Persons(1,‘徐帆’,‘黑龙江省佳木斯市’); insert into TEST_SCHEMA.PK_Persons(2,‘杨过’,‘辽宁省生沈阳市’); commit |
主键约束 | CREATE TABLE TEST_SCHEMA.TABLE_PRI(ID NUMBER ,USERNAME VARCHAR(16),LOCATION VARCHAR(30),CONSTRAINT PK_USERNAME PRIMARY KEY (“USERNAME”)); INSERT INTO TEST_SCHEMA.TABLE_PRI VALUES (1, ‘李亚鹏’,‘黑龙江省佳木斯市’); commit |
外键约束 | CREATE TABLE TEST_SCHEMA.TABLE_FOR(USERNAME VARCHAR(16),ID_CARD VARCHAR(25),CONSTRAINT FK_USERNAME FOREIGN KEY REFERENCES primary_key(USERNAME)); INSERT INTO TEST_SCHEMA.TABLE_FOR VALUES (‘李亚鹏’,‘142232199211182197’); commit |
check约束 | create table TEST_SCHEMA.CHECK_TABLE(ID number,NAME varchar2(20),SEX varchar2(4),CONSTRAINT check_sex CHECK (sex in (‘男’,‘女’))); insert into TEST_SCHEMA.CHECK_TABLE values (1,‘冯小芳’,‘女’); insert into TEST_SCHEMA.CHECK_TABLE values (2,‘徐帆’,‘男’); commit –验证 insert into TEST_SCHEMA.CHECK_TABLE values (2,‘徐帆’,‘男a’); |
CREATE TABLE "TEST_SCHEMA"."TEST_TRIGGER_YUAN"
(
ID int NOT NULL,
NAME VARCHAR(20) NOT NULL,
ID_CARD VARCHAR(20) NOT NULL,
PHONE VARCHAR(15) NOT NULL
);
INSERT INTO TEST_SCHEMA.TEST_TRIGGER_YUAN VALUES(1,'古天乐','23052219870618226X','13512983499');
CREATE TABLE "TEST_SCHEMA"."TEST_TRIGGER_MU"
(
ID int NOT NULL,
NAME VARCHAR(20) NOT NULL,
ID_CARD VARCHAR(20) NOT NULL,
PHONE VARCHAR(15) NOT NULL
);
--表级触发器,功能描述,在表TEST_TRIGGER_YUAN中添加一条数据后,表TEST_TRIGGER_MU会自动添加一条数据
CREATE or REPLACE TRIGGER TEST_SCHEMA.TEST_TRIGGER AFTER INSERT on TEST_SCHEMA.TEST_TRIGGER_YUAN FOR EACH ROW
BEGIN
INSERT INTO TEST_SCHEMA.TEST_TRIGGER_MU (ID,NAME,ID_CARD,PHONE) VALUES (:new.ID,:new.NAME,:new.ID_CARD,:new.phone);
END;
存储过程 --存储过程
CREATE OR REPLACE PROCEDURE INSERT_TABLE_DATA(i int)
AS
num1 int;
num2 int;
num3 int;
BEGIN
num3 = i;
num1 =(select count(*) from TEST_SCHEMA.SMOKE_TEST);
num2 = num1;
while i>0 LOOP
INSERT INTO TEST_SCHEMA.SMOKE_TEST VALUES (num2,'葛优','142232199211182197','15804661187','1992-11-18 23:53:17','黑龙江省哈尔滨市','2');
commit;
i:= i-1;
num2:=num2+1;
END LOOP;
print num3;
print num1;
num2 = (select count(*) from TEST_SCHEMA.SMOKE_TEST);
print num2;
END;
--验证,输出的结果,上面两个数加起来等于最后一个数即可
BEGIN
INSERT_TABLE_DATA(2);
END;
CREATE OR REPLACE FUNCTION "TEST_SCHEMA"."FUNC"(para1 int,para2 int)
return int
AS
BEGIN
IF para1 > para2 THEN
return para1;
ELSE
return para2;
END IF;
END;
--验证,输出较大的值
select "TEST_SCHEMA"."FUNC"(1,3);
包 CREATE TABLE TEST_SCHEMA.Person(
Id INT IDENTITY,
Name VARCHAR(100),
City VARCHAR(100));
INSERT INTO Person(Name, City) VALUES('冯小刚','武汉');
INSERT INTO Person(Name, City) VALUES('徐帆','北京');
INSERT INTO Person(Name, City) VALUES('舒淇','上海');
--第一步:创建包头
CREATE OR REPLACE PACKAGE PersonPackage AS
PROCEDURE AddPerson(Pname VARCHAR(100), Pcity varchar(100));
END PersonPackage
--第二步:创建包体
CREATE OR REPLACE PACKAGE BODY PersonPackage AS
PROCEDURE AddPerson(Pname VARCHAR(100), Pcity varchar(100) )AS
BEGIN
INSERT INTO Person(Name, City) VALUES(Pname, Pcity);
END AddPerson;
END PersonPackage;
--验证,给Person表添加一行数据
CALL PersonPackage.AddPerson ('张扬', '哈尔滨') ;
commit
create view "TEST_SCHEMA"."VIEW_SMOKE_TEST" AS select * from "TEST_SCHEMA"."SMOKE_TEST";
序列 create table TEST_SCHEMA.not_null_table(
ID int,
name varchar2(30) not NULL
);
CREATE SEQUENCE TEST_SCHEMA.SEQ_NOT_NULL_TABLE
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOCYCLE
NOCACHE;
insert into TEST_SCHEMA.not_null_table values(SEQ_NOT_NULL_TABLE.NEXTVAL,'古天乐');
insert into TEST_SCHEMA.not_null_table values(SEQ_NOT_NULL_TABLE.NEXTVAL,'冯小刚');
commit
普通索引 create table TEST_SCHEMA.NORMAL_INDEX_TABLE(
ID int,
PHONE varchar(20)
);
CREATE INDEX normal_index on TEST_SCHEMA.NORMAL_INDEX_TABLE(ID);
insert into TEST_SCHEMA.NORMAL_INDEX_TABLE values (1,'13512983499');
insert into TEST_SCHEMA.NORMAL_INDEX_TABLE values (2,'15804661187');
insert into TEST_SCHEMA.NORMAL_INDEX_TABLE values (2,'15804661186');
insert into TEST_SCHEMA.NORMAL_INDEX_TABLE values (3,'13945793607');
insert into TEST_SCHEMA.NORMAL_INDEX_TABLE values (4,'15522553661');
insert into TEST_SCHEMA.NORMAL_INDEX_TABLE values (5,'15522573661');
commit
create table TEST_SCHEMA.CONNECT_INDEX_TABLE(
ID int,
PHONE varchar(20),
NAME varchar(15)
);
CREATE INDEX connect_index on TEST_SCHEMA.CONNECT_INDEX_TABLE(ID,NAME);
insert into TEST_SCHEMA.NORMAL_INDEX_TABLE values (1,'13512983499','古天乐');
insert into TEST_SCHEMA.NORMAL_INDEX_TABLE values (2,'15804661187','古天乐');
insert into TEST_SCHEMA.NORMAL_INDEX_TABLE values (2,'15804661186','古天乐');
insert into TEST_SCHEMA.NORMAL_INDEX_TABLE values (3,'13945793607','古天乐');
insert into TEST_SCHEMA.NORMAL_INDEX_TABLE values (4,'15522553661','古天乐');
insert into TEST_SCHEMA.NORMAL_INDEX_TABLE values (5,'15522573661','古天乐');
commit
create table TEST_SCHEMA.unique_INDEX_TABLE(
ID int,
PHONE varchar(20)
);
CREATE UNIQUE INDEX unique_index on TEST_SCHEMA.unique_INDEX_TABLE(ID);
insert into TEST_SCHEMA.unique_INDEX_TABLE values (1,'13512983499');
insert into TEST_SCHEMA.unique_INDEX_TABLE values (2,'15804661187');
insert into TEST_SCHEMA.unique_INDEX_TABLE values (3,'13945793607');
insert into TEST_SCHEMA.unique_INDEX_TABLE values (4,'15522553661');
insert into TEST_SCHEMA.unique_INDEX_TABLE values (5,'15522573661');
commit
--验证,插入失败
insert into TEST_SCHEMA.unique_INDEX_TABLE values (2,'15804661186');