首页 > 代码库 > ORCEL基本语句(一)

ORCEL基本语句(一)

--创建学生信息表,约束放在所有列定义之后

 1 CREATE TABLE stu_info(
 2 stu_id INTEGER,
 3 stu_name NVARCHAR2(15) NOT NULL,
 4 stu_sex CHAR(4) NOT NULL,
 5 stu_age INTEGER NOT NULL,
 6 stu_address NVARCHAR2(30) NOT NULL,
 7 stu_email NVARCHAR2(30),
 8 stu_tele INTEGER NOT NULL,
 9 tch_id INTEGER,
10 CONSTRAINT stu_info_stu_id_pk PRIMARY KEY(stu_id),
11 CONSTRAINT stu_info_stu_sex_ck CHECK(stu_sex in (,)),
12 CONSTRAINT stu_info_stu_age_ck CHECK(stu_age between 12 and 18),
13 CONSTRAINT stu_info_stu_email_uk UNIQUE(stu_email),
14 CONSTRAINT stu_info_stu_tele_uk UNIQUE(stu_tele)
15 );

--删除数据表

DROP TABLE stu_info;

 


--创建教师信息表,约束放在列的定义后面

1 CREATE TABLE tch_info(
2 tch_id INTEGER PRIMARY KEY,
3 tch_name NVARCHAR2(15) NOT NULL,
4 tch_sub NVARCHAR2(15) NOT NULL,
5 tch_sex CHAR(4) CHECK(tch_sex in (,)) NOT NULL,
6 tch_email NVARCHAR2(30) NOT NULL,
7 tch_tele INTEGER NOT NULL UNIQUE
8 );

 

--添加外键

1 ALTER TABLE stu_info ADD CONSTRAINT stu_info_tch_info_tch_id_fk FOREIGN KEY(tch_id) REFERENCES tch_info(tch_id);

--删除外键约束

ALTER TABLE stu_info DROP CONSTRAINT stu_info_tch_info_tch_id_fk;

--添加可以级联操作的外键约束

1 ALTER TABLE stu_info ADD CONSTRAINT stu_info_tch_info_tch_id_fk FOREIGN KEY(tch_id) REFERENCES tch_info(tch_id) ON DELETE CASCADE;

--给学生信息表添加新的列

1 ALTER TABLE stu_info ADD (stu_subject NVARCHAR2(15) NOT NULL);

--修改学生信息表内某列的属性

1 ALTER TABLE stu_info MODIFY (stu_tele NUMBER(11));

--删除一个序列

DROP SEQUENCE stu_info_sequence;

--创建一个序列

CREATE SEQUENCE stu_info_sequence
INCREMENT BY 1
START WITH 1 NOMAXVALUE NOCYCLE NOCACHE;

--向stu_info表中添加内容

 1 INSERT INTO stu_info VALUES(stu_info_sequence.nextval,张晓风,,15,河南郑州,zhangxiaofeng@163.com,11235463120,null);
 2 INSERT INTO stu_info VALUES(stu_info_sequence.nextval,邓国庆,,15,河南郑州,dengguoqing@163.com,11235463121,null);
 3 INSERT INTO stu_info VALUES(stu_info_sequence.nextval,沙鹏飞,,15,河南郑州,shapengfei@163.com,11235463122,null);
 4 INSERT INTO stu_info VALUES(stu_info_sequence.nextval,臧豪,,15,河南郑州,zanghao@163.com,11235463123,null);
 5 INSERT INTO stu_info VALUES(stu_info_sequence.nextval,徐炜,,15,河南郑州,xuwei@163.com,11235463124,null);
 6 INSERT INTO stu_info VALUES(stu_info_sequence.nextval,侯志文,,15,河南郑州,houzhiwen@163.com,11235463125,null);
 7 INSERT INTO stu_info VALUES(stu_info_sequence.nextval,孔萌,,15,河南郑州,kongmegn@163.com,11235463126,null);
 8 INSERT INTO stu_info VALUES(stu_info_sequence.nextval,刘教练,,15,河南郑州,liujiaolian@163.com,11235463127,null);
 9 INSERT INTO stu_info VALUES(stu_info_sequence.nextval,帅炳超,,15,河南郑州,shuaibingchao@163.com,11235463128,null);
10 INSERT INTO stu_info VALUES(stu_info_sequence.nextval,任伟峰,,15,河南郑州,renweifeng@163.com,11235463129,null);
11 INSERT INTO stu_info VALUES(stu_info_sequence.nextval,李梦雅,,15,河南郑州,limengya@163.com,112354631210,null);

 


--查看表格内容

1 SELECT * FROM stu_info WHERE stu_name = 张晓风;
2 SELECT * FROM stu_info;

--更新指定行,指定列的内容

1 UPDATE stu_info set stu_address = 河南漯河 WHERE stu_name = 张晓风 ;

--删除表格内容

1 DELETE from stu_info [where stu_name = ‘邓国庆‘];

--查询用户内所有的表

1 SELECT * FROM tabs;
2 SELECT * FROM user_tables;

--若没有where语句则显示所有用户下的表

1 SELECT * FROM all_tables where owner = SCOTT;

 

ORCEL基本语句(一)