Oracle下SQL基本操作(三)
----------------------------建表,与插入数据
--创建学位表
CREATE TABLE QUAFICATION (
QUALID CHAR(1),
QUALNAME VARCHAR(10),
---不要忘记括号
CONSTRAINTS QUA_ID_PK PRIMARY KEY (QUALID)
);
--创建工资级别表
CREATE TABLE EMPLEVEL(
LEVELNO VARCHAR2(2),
LOWSALARY NUMBER(7,2),
HIGHSALARY NUMBER(7,2),
CONSTRAINTS EMP_NO_PK PRIMARY KEY (LEVELNO)
);
---创建职务表
CREATE TABLE POSITION(
POSITIONID VARCHAR(2),
POSITIONNAME VARCHAR(20),
CONSTRAINTS POS_ID_PK PRIMARY KEY (POSITIONID)
);
---创建部门表
CREATE TABLE DEPARTMENT(
DEPTID VARCHAR2(2),
DEPTNAME VARCHAR2(30),
LOCATION VARCHAR2(30),
MANAGERID VARCHAR2(4),
CONSTRAINTS DEP_ID_PK PRIMARY KEY (DEPTID)
);
---创建员工表
CREATE TABLE EMPLOYEE(
EMPLOYEEID VARCHAR2(4),
NAME VARCHAR2(40) NOT NULL,
HIREDATE DATE,
SALARY NUMBER(7,2),
COMMISSION NUMBER(7,2),
MANAGERID VARCHAR2(4),
DEPTID VARCHAR2(2),
POSITIONID VARCHAR2(2),
QUALID CHAR(1),
CONSTRAINTS EMP_ID_PK PRIMARY KEY (EMPLOYEEID),
CONSTRAINTS EMP_DEP_FK FOREIGN KEY(DEPTID) REFERENCES DEPARTMENT(DEPTID),
CONSTRAINTS EMP_POS_FK FOREIGN KEY(POSITIONID) REFERENCES POSITION(POSITIONID),
CONSTRAINTS EMP_QUA_FK FOREIGN KEY(QUALID) REFERENCES QUAFICATION(QUALID)
);
--DROP TABLE EMPLOYEE;
----添加 员工表EMPLOYEE 外键,参照原表
ALTER TABLE EMPLOYEE
ADD CONSTRAINTS EMP_MAN_FK FOREIGN KEY(MANAGERID) REFERENCES EMPLOYEE(EMPLOYEEID);
----------------添加 部门表DEPARTMENT 外键,参照 EMPLOYEE 表
ALTER TABLE DEPARTMENT
ADD CONSTRAINTS DEP_MAN_FK FOREIGN KEY(MANAGERID) REFERENCES EMPLOYEE(EMPLOYEEID);
---SELECT * FROM USER_CONSTRAINTS;查看约束
SELECT * FROM EMPLOYEE FOR UPDATE;
----INSERT POSITION
INSERT INTO POSITION(POSITIONID,POSITIONNAME) VALUES('01','程序员');----指定列
INSERT INTO POSITION VALUES('02','组长');---不指定列,则要给所有的列指定值
INSERT INTO POSITION VALUES('03','经理');
INSERT INTO POSITION VALUES('04','总裁');
----SELECT *FROM POSITION;
----INSERT EMPLEVEL
INSERT INTO EMPLEVEL VALUES('01',1000,2000);
INSERT INTO EMPLEVEL VALUES('02',2001,3000);
INSERT INTO EMPLEVEL VALUES('03',3001,4000);
INSERT INTO EMPLEVEL VALUES('04',401,5000);
----SELECT *FROM EMPLEVEL;
---INSERT QUAFICATION
INSERT INTO QUAFICATION VALUES('1','学士');
INSERT INTO QUAFICATION VALUES('2','博士');
相关新闻>>
- 发表评论
-
- 最新评论 更多>>