DROP TABLE STUDY;
DROP TABLE STUDENT;
DROP TABLE COURSE;
/
CREATE TABLE STUDENT
(
SID VARCHAR(10),
SName varchar(30),
primary key (SID)
);
/
CREATE TABLE COURSE
(
CID VARCHAR(10),
CName varchar(50),
primary key (CID)
);
/
CREATE TABLE STUDY
(
SID VARCHAR(10),
CID VARCHAR(10),
SCORE INTEGER DEFAULT 0 CHECK (SCORE between 0 and 100),
constraint PK_STUDY PRIMARY KEY (SID) --注意此处设置主键约束名,便于后面修改表的主键
);
/
COMMIT;
/
select * from student;
select * from study;
/
--以表 student,study,score 为例
--1.增加列
alter table student add (sex varchar2(2) DEFAULT '女',age integer,address varchar(100)) ;
/
--2.修改列定义
ALTER TABLE student MODIFY sex VARCHAR2(4) DEFAULT '男';
/
--3.删除列
ALTER TABLE student DROP COLUMN address;
/
--4.修改列名
ALTER TABLE student RENAME COLUMN sid to sno;
/
ALTER TABLE student RENAME COLUMN sno to sid;
/
--5.修改表名
RENAME study TO Learn;
/
RENAME Learn TO study;
/
--6.增加注释
-- 给表添加释
COMMENT ON TABLE STUDY IS '学习信息表';
/
--给列添加注释
COMMENT ON COLUMN STUDY.SID IS '学员编号';
/
--7.增加外键关联
ALTER TABLE study ADD CONSTRAINT FK_study_R_student_S FOREIGN KEY (SID) REFERENCES student(SID);
/
--8.修改主键
--查找主键约束名
select T.constraint_name from USER_CONSTRAINTS T where table_name='STUDY' and constraint_type='P' AND ROWNUM<2;
/
--删除主键约束
ALTER TABLE study DROP CONSTRAINT PK_STUDY
/
--添加主键
alter table study add constraint PK_STUDY primary key (cid,sid);
/
commit;
/
select * from study;
select * from COURSE;