Вы находитесь на странице: 1из 15

desc dept

Name Null? Type


------------------------------- -------- ----
DEPTNO NOT NULL NUMBER(2)
DNAME NOT NULL VARCHAR2(15)
LOC VARCHAR2(15)

SQL> drop table dept purge;

Table dropped.

SQL> ed
Wrote file afiedt.buf

1* drop table emp purge


2 /

Table dropped.

SQL> clear screen


SQL> CREATE TABLE DEPT
2 (DEPTNO NUMBER(2),
3 DNAME VARCHAR2(14),
4 LOC VARCHAR2(13) );

Table created.

SQL>
SQL> INSERT INTO DEPT VALUES
2 (10,'ACCOUNTING','NEW YORK');

1 row created.

SQL> INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');

1 row created.

SQL> INSERT INTO DEPT VALUES


2 (30,'SALES','CHICAGO');

1 row created.

SQL> INSERT INTO DEPT VALUES


2 (40,'OPERATIONS','BOSTON');

1 row created.

SQL> CREATE TABLE EMP


2 (EMPNO NUMBER(4) ,
3 ENAME VARCHAR2(10),
4 JOB VARCHAR2(9),
5 MGR NUMBER(4),
6 HIREDATE DATE,
7 SAL NUMBER(7,2),
8 COMM NUMBER(7,2),
9 DEPTNO NUMBER(2));
Table created.

SQL>
SQL> INSERT INTO EMP VALUES
2 (7369,'SMITH','CLERK',7902,'17-DEC-1980',800,NULL,20);

1 row created.

SQL> INSERT INTO EMP VALUES


2 (7499,'ALLEN','SALESMAN',7698,'20-FEB-1981',1600,300,30);

1 row created.

SQL> INSERT INTO EMP VALUES


2 (7521,'WARD','SALESMAN',7698,'22-FEB-1981',1250,500,30);

1 row created.

SQL> INSERT INTO EMP VALUES


2 (7566,'JONES','MANAGER',7839,'2-APR-1981',2975,NULL,20);

1 row created.

SQL> INSERT INTO EMP VALUES


2 (7654,'MARTIN','SALESMAN',7698,'28-SEP-1981',1250,1400,30);

1 row created.

SQL> INSERT INTO EMP VALUES


2 (7698,'BLAKE','MANAGER',7839,'1-MAY-1981',2850,NULL,30);

1 row created.

SQL> INSERT INTO EMP VALUES


2 (7782,'CLARK','MANAGER',7839,'9-JUN-1981',2450,NULL,10);

1 row created.

SQL> INSERT INTO EMP VALUES


2 (7788,'SCOTT','ANALYST',7566,'09-DEC-1982',3000,NULL,20);

1 row created.

SQL> INSERT INTO EMP VALUES


2 (7839,'KING','PRESIDENT',NULL,'17-NOV-1981',5000,NULL,10);

1 row created.

SQL> INSERT INTO EMP VALUES


2 (7844,'TURNER','SALESMAN',7698,'8-SEP-1981',1500,0,30);

1 row created.

SQL> INSERT INTO EMP VALUES


2 (7876,'ADAMS','CLERK',7788,'12-JAN-1983',1100,NULL,20);

1 row created.
SQL> INSERT INTO EMP VALUES
2 (7900,'JAMES','CLERK',7698,'3-DEC-1981',950,NULL,30);

1 row created.

SQL> INSERT INTO EMP VALUES


2 (7902,'FORD','ANALYST',7566,'3-DEC-1981',3000,NULL,20);

1 row created.

SQL> INSERT INTO EMP VALUES


2 (7934,'MILLER','CLERK',7782,'23-JAN-1982',1300,NULL,10);

1 row created.

SQL> commit;

Commit complete.

SQL> clear screen


SQL> select *from dept;

DEPTNO DNAME LOC


--------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

SQL> select *from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
--------- ---------- --------- --------- --------- --------- --------- ------
---
7369 SMITH CLERK 7902 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7566 JONES MANAGER 7839 02-APR-81 2975
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7788 SCOTT ANALYST 7566 09-DEC-82 3000
20
7839 KING PRESIDENT 17-NOV-81 5000
10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 12-JAN-83 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
7902 FORD ANALYST 7566 03-DEC-81 3000
20
7934 MILLER CLERK 7782 23-JAN-82 1300
10

14 rows selected.

SQL> insert into dept values(10,'SW','HYD');

1 row created.

SQL> update emp set deptno=50


2 where empno=7902;

1 row updated.

SQL> ed
Wrote file afiedt.buf

1 update emp set mgr=7001


2* where empno=7369
3 /

1 row updated.

SQL> clear screen


SQL> select *from dept;

DEPTNO DNAME LOC


--------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
10 SW HYD

SQL> select *from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
--------- ---------- --------- --------- --------- --------- --------- ------
---
7369 SMITH CLERK 7001 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7566 JONES MANAGER 7839 02-APR-81 2975
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7788 SCOTT ANALYST 7566 09-DEC-82 3000
20
7839 KING PRESIDENT 17-NOV-81 5000
10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 12-JAN-83 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
7902 FORD ANALYST 7566 03-DEC-81 3000
50
7934 MILLER CLERK 7782 23-JAN-82 1300
10

14 rows selected.

SQL> delete from dept


2 where dname='SW';

1 row deleted.

SQL> select *from dept;

DEPTNO DNAME LOC


--------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

SQL> rollback;

Rollback complete.

SQL>
SQL> select *from dept;

DEPTNO DNAME LOC


--------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

SQL> alter table dept add constraint


2
SQL>
SQL> ed
Wrote file afiedt.buf

1* alter table dept add constraint pk_dno primary key(deptno)


2 /
Table altered.

SQL> ed
Wrote file afiedt.buf

1* alter table dept add constraint pk_dno primary key(deptno)


2
SQL>
SQL> clear screen
SQL> select *from dept;

DEPTNO DNAME LOC


--------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

SQL> select *from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
--------- ---------- --------- --------- --------- --------- --------- ------
---
7369 SMITH CLERK 7902 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7566 JONES MANAGER 7839 02-APR-81 2975
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7788 SCOTT ANALYST 7566 09-DEC-82 3000
20
7839 KING PRESIDENT 17-NOV-81 5000
10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 12-JAN-83 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
7902 FORD ANALYST 7566 03-DEC-81 3000
20
7934 MILLER CLERK 7782 23-JAN-82 1300
10

14 rows selected.

SQL> update emp set deptno=50


2 where empno=7902;

1 row updated.

SQL> ed
Wrote file afiedt.buf

1 update emp set mgr=7001


2* where empno=7369
SQL> /

1 row updated.

SQL> commit;

Commit complete.

SQL> clear screen


SQL> select *from dept;

DEPTNO DNAME LOC


--------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

SQL> select *from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
--------- ---------- --------- --------- --------- --------- --------- ------
---
7369 SMITH CLERK 7001 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7566 JONES MANAGER 7839 02-APR-81 2975
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7788 SCOTT ANALYST 7566 09-DEC-82 3000
20
7839 KING PRESIDENT 17-NOV-81 5000
10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 12-JAN-83 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
7902 FORD ANALYST 7566 03-DEC-81 3000
50
7934 MILLER CLERK 7782 23-JAN-82 1300
10

14 rows selected.

SQL> edx
unknown command "edx" - rest of line ignored.
SQL> ed
Wrote file afiedt.buf

1 ALTER TABLE Emp ADD Constraint Emp_Mgr_FK


2* FOREIGN KEY(Mgr) REFERENCES Emp(empno)
SQL> /
FOREIGN KEY(Mgr) REFERENCES Emp(empno)
*
ERROR at line 2:
ORA-02270: no matching unique or primary key for this column-list

SQL> ed
Wrote file afiedt.buf

line 1 truncated.
1* alter table emp add constraint pk_eno primary key(empno
SQL>
SQL>
SQL> ed
Wrote file afiedt.buf

1* alter table emp add constraint pk_eno primary key(empno)


SQL> /

Table altered.

SQL> ALTER TABLE Emp ADD Constraint Emp_Mgr_FK


2 FOREIGN KEY(Mgr) REFERENCES Emp(empno)
3
SQL> ed
Wrote file afiedt.buf

1 ALTER TABLE Emp ADD Constraint Emp_Mgr_FK


2* FOREIGN KEY(Mgr) REFERENCES Emp(empno)
3 /
ALTER TABLE Emp ADD Constraint Emp_Mgr_FK
*
ERROR at line 1:
ORA-02298: cannot validate (JONES.EMP_MGR_FK) - parent keys not found

SQL> ed
Wrote file afiedt.buf

1 select mgr from emp


2* where mgr not in(select empno from emp)
SQL> /
MGR
---------
7001

SQL> ed
Wrote file afiedt.buf

1 update emp set mgr=7902


2* where empno=7369
SQL> /

1 row updated.

SQL> ed
Wrote file afiedt.buf

1 update emp set mgr=7902


2* where empno=7369
3
SQL>
SQL> ed
Wrote file afiedt.buf

line 2 truncated.
1 ALTER TABLE Emp ADD Constraint Emp_Mgr_FK
2* FOREIGN KEY(Mgr) REFERENCES Emp(empno
SQL> /
FOREIGN KEY(Mgr) REFERENCES Emp(empno
*
ERROR at line 2:
ORA-00907: missing right parenthesis

SQL> ed
Wrote file afiedt.buf

1 ALTER TABLE Emp ADD Constraint Emp_Mgr_FK


2* FOREIGN KEY(Mgr) REFERENCES Emp(empno)
SQL> /

Table altered.

SQL> ed
Wrote file afiedt.buf

1 ALTER TABLE Emp ADD Constraint Emp_Mgr_FK


2* FOREIGN KEY(Mgr) REFERENCES Emp(empno)
SQL>
SQL>
SQL> ed
Wrote file afiedt.buf

1 ALTER TABLE Emp ADD Constraint Emp_Mgr_FK


2* FOREIGN KEY(Mgr) REFERENCES Emp(empno)
SQL> clear screen
SQL> select *from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM
DEPTNO
--------- ---------- --------- --------- --------- --------- --------- ------
---
7369 SMITH CLERK 7902 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7566 JONES MANAGER 7839 02-APR-81 2975
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7788 SCOTT ANALYST 7566 09-DEC-82 3000
20
7839 KING PRESIDENT 17-NOV-81 5000
10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 12-JAN-83 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
7902 FORD ANALYST 7566 03-DEC-81 3000
50
7934 MILLER CLERK 7782 23-JAN-82 1300
10

14 rows selected.

SQL> update emp set deptno=null


2 where empno=7369;

1 row updated.

SQL> ed
Wrote file afiedt.buf

1 update emp set deptno=null


2* where empno=7369
3 /

1 row updated.

SQL> ed
Wrote file afiedt.buf

1 update emp set deptno=null


2* where empno=7369
3
SQL>
SQL>
SQL> select *from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
--------- ---------- --------- --------- --------- --------- --------- ------
---
7369 SMITH CLERK 7902 17-DEC-80 800
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7566 JONES MANAGER 7839 02-APR-81 2975
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7788 SCOTT ANALYST 7566 09-DEC-82 3000
20
7839 KING PRESIDENT 17-NOV-81 5000
10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 12-JAN-83 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
7902 FORD ANALYST 7566 03-DEC-81 3000
50
7934 MILLER CLERK 7782 23-JAN-82 1300
10

14 rows selected.

SQL>
SQL> ed
Wrote file afiedt.buf

1 ALTER TABLE Emp ADD CONSTRAINT Dept_Dno_FK


2 FOREIGN KEY(Deptno)
3* REFERENCES Dept MODIFY(Deptno NOT NULL)
SQL> /
ALTER TABLE Emp ADD CONSTRAINT Dept_Dno_FK
*
ERROR at line 1:
ORA-02296: cannot enable (JONES.) - null values found

SQL> update emp set deptno=20


2 where deptno is null;

1 row updated.

SQL> ed
Wrote file afiedt.buf

line 3 truncated.
1 ALTER TABLE Emp ADD CONSTRAINT Dept_Dno_FK
2 FOREIGN KEY(Deptno)
3* REFERENCES Dept MODIFY(Deptno NOT NULL
SQL> /
REFERENCES Dept MODIFY(Deptno NOT NULL
*
ERROR at line 3:
ORA-00907: missing right parenthesis

SQL> ed
Wrote file afiedt.buf

1 ALTER TABLE Emp ADD CONSTRAINT Dept_Dno_FK


2 FOREIGN KEY(Deptno)
3* REFERENCES Dept MODIFY(Deptno NOT NULL)
SQL> /
ALTER TABLE Emp ADD CONSTRAINT Dept_Dno_FK
*
ERROR at line 1:
ORA-02298: cannot validate (JONES.DEPT_DNO_FK) - parent keys not found

SQL> ed
Wrote file afiedt.buf

1 ALTER TABLE Emp ADD CONSTRAINT Dept_Dno_FK


2 FOREIGN KEY(Deptno)
3* REFERENCES Dept MODIFY(Deptno NOT NULL)
SQL>
SQL>
SQL> select
2
SQL>
SQL> ed
Wrote file afiedt.buf

1 select deptno from emp


2* where deptno not in(select deptno from dept)
3 /

DEPTNO
---------
50

SQL> ed
Wrote file afiedt.buf

1 select deptno from emp


2* where deptno not in(select deptno from dept)
3
SQL> /

DEPTNO
---------
50

SQL> uupdate emp set


unknown command beginning "uupdate em..." - rest of line ignored.
SQL>
SQL>
SQL> update emp set
2
SQL>
SQL> ed
Wrote file afiedt.buf

1 update emp set deptno=20


2* where empno=7902
3
SQL>
SQL> ed
Wrote file afiedt.buf

1 update emp set deptno=20


2* where empno=7902
SQL> /

1 row updated.

SQL> select *from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
--------- ---------- --------- --------- --------- --------- --------- ------
---
7369 SMITH CLERK 7902 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7566 JONES MANAGER 7839 02-APR-81 2975
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7788 SCOTT ANALYST 7566 09-DEC-82 3000
20
7839 KING PRESIDENT 17-NOV-81 5000
10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 12-JAN-83 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
7902 FORD ANALYST 7566 03-DEC-81 3000
20
7934 MILLER CLERK 7782 23-JAN-82 1300
10

14 rows selected.

SQL> ALTER TABLE Emp ADD CONSTRAINT Dept_Dno_FK


2 FOREIGN KEY(Deptno)
3 REFERENCES Dept MODIFY(Deptno NOT NULL)
4 /

Table altered.

SQL> ed
Wrote file afiedt.buf

1 ALTER TABLE Emp ADD CONSTRAINT Dept_Dno_FK


2 FOREIGN KEY(Deptno)
3* REFERENCES Dept MODIFY(Deptno NOT NULL)
SQL>
SQL>
SQL>
SQL> select *from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
--------- ---------- --------- --------- --------- --------- --------- ------
---
7369 SMITH CLERK 7902 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7566 JONES MANAGER 7839 02-APR-81 2975
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7788 SCOTT ANALYST 7566 09-DEC-82 3000
20
7839 KING PRESIDENT 17-NOV-81 5000
10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 12-JAN-83 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
7902 FORD ANALYST 7566 03-DEC-81 3000
20
7934 MILLER CLERK 7782 23-JAN-82 1300
10
14 rows selected.

SQL> update emp set deptno=60;


update emp set deptno=60
*
ERROR at line 1:
ORA-02291: integrity constraint (JONES.DEPT_DNO_FK) violated - parent key not
found

SQL> ed
Wrote file afiedt.buf

1* update emp set deptno=null


2 /
update emp set deptno=null
*
ERROR at line 1:
ORA-01407: cannot update ("JONES"."EMP"."DEPTNO") to NULL

SQL>

Вам также может понравиться