- --部门:部门编号,部门名称,地址;
- --员工:员工编号,员工名字,职务,管理编号,入职日期,薪资,奖金,部门编号;
CREATE TABLE dept( deptno INT PRIMARY KEY, dname VARCHAR(20), loc VARCHAR(20) ); CREATE TABLE emp( empno INT PRIMARY KEY, ename VARCHAR(20) NOT NULL, job VARCHAR(20) CHECK (job IN('CLERK','SALESMAN','MANAGER','ANALYST')), mgp INT, hiredate DATE , sal DECIMAL(10,2), comm DECIMAL(10,2), deptno INT, CONSTRAINT pk_we FOREIGN KEY (deptno) REFERENCES dept (deptno) )
插入数据:
INSERT INTO dept VALUES (10,'ACCOUNTING','NEWTORK'); INSERT INTO dept VALUES (20,'RESEARCH','DALLAS'); INSERT INTO dept VALUES (30,'SALES','CHICAGO'); INSERT INTO dept VALUES (40,'OPERATIONS','BOSTON'); select * from dept insert into emp values(7369,'SMITH','CLERK',7902,'1980-12-17',1640,NULL,20); insert into emp values(7499,'ALLEN','SALESMAN',7698,'1981-2-20',11400,300,30); insert into empvalues(7521,'WARD','SALESMAN',7698,'1981-2-22',5200,500,30); insert into empvalues(7566,'JOENS','MANAGER',7839,'1981-4-2',7015,NULL,20); insert into emp values(7654,'MARTIN','SALESMAN',7698,'1981-9-28',5200,1400,30); insert into empvalues(7698,'BLAKE','MANAGER',7839,'1981-5-1',5900,NULL,30); insert into empvalues(7782,'CLARK','MANAGER',7839,'1981-6-9',2470,NULL,10); insert into emp values(7788,'SCOTT','ANALYST',7566,'1987-4-19',3040,NULL,20);
select a.ENAME from emp a
where exists(select * from dept b where b.deptno=a.deptno and b.deptno=10);