You are on page 1of 17

DDL AND DML

Statements
Ex. No : 04
Date :

SQL - DATA DEFINITION LANGUAGE AND DATA


MANIPULATION LANGUAGE STATEMENTS

Syntax for various QUERIES:

1. CREATE TABLE Statement:

To create a new table with the set of columns specified with respective types.

CREATE TABLE [schema.] table (column data type [DEFAULT expr];

Table – Name of new table to be created.


Schema – same as the owner’s name.
DEFAULT expr – specifies a default value if a value is omitted in the INSERT statement.
Column – Name of the column.
Datatype – The column’s datatype and length.

2. INSERT Statement:

Add new rows to a table by using the INSERT statement.

INSERT INTO table [(column [, column…])]


VALUES (value [, value…]);

Table – Name of the table.


Column x – The ‘x’- th column name of the table.
Value x – The value for the ‘x’- th column.

3. UPDATE Statement:

Modify existing rows with the UPDATE statement.

UPDATE table
SET column = value [, column = value]
[WHERE condition];

Table – Name of the table.


Column – Name of the column in the table to populate.
Value – The corresponding value or sub-query for the column.
Condition – identifies the rows to be updated and is composed of column names, expressions,
constants, sub-queries and comparison operators.

4. DELETE Statement:

Remove existing rows using the delete statement.

DELETE [FROM] table


[WHERE condition];

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


DDL AND DML
Statements

Table – Name of the table.


Condition – identifies the rows to be updated and is composed of column names, expressions,
constants, sub-queries and comparison operators.

5. Querying the Data Dictionary:

The data dictionary tables can be queried to view various database objects owned by
the user. Frequently used dictionary tables are:

USER_TABLES, USER_OBJECTS, USER_CATALOG, USER_CONSTRAINTS.

6. ALTER TABLE Statement:

ALTER TABLE table


ADD (column data type [DEFAULT expr] [, column data type]…);

Table – Name of the table.


Column – Name of the new column.
Data type – The data type and length of the new column.
DEFAULT expr - specifies the default value by using the ALTER TABLE statement with the
MODIFY clause.

7. RENAME Statement:

RENAME old_name TO new_name;

Old_name – Old name of the table, view, sequence, or synonym.


New_name – New name of the table, view, sequence, or synonym.

8. TRUNCATE Statement:

TRUNCATE TABLE table;

Table – Name of the table.

9. COMMENT Statement:

COMMENT ON TABLE table is ‘name’;

Comments stored in the data dictionary can be viewed in one of the following data dictionary
views in the COMMENTS column:

ALL_COL_COMMENTS, USER_COL_COMMENTS, ALL_TA_COMMENTS,


USER_TAB_COMMENTS.

Table – Name of the table.


Column – Name of the new column.
Text – The text of the comment.

10. DEFINING CONSTRAINTS:

CREATE TABLE [schema.]Table (column data type [DEFAULT expr]


[column_constraint] … [table_constraint]);
SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.
DDL AND DML
Statements

Table – Name of the table.


Column – Name of the new column.
Data type – The data type and length of the new column.
Schema – same as the owner’s name.
DEFAULT expr – specifies a default value if a value is omitted in the INSERT statement.
Column_constraint – An integrity constraint as part of the column definition.
Table_constraint - An integrity constraint as part of the table definition.

11. VIEW Statement:

To embed a sub-query within the CREATE VIEW Statement.

CREATE [OR REPLACE] [FORCE|INFORCE] VIEW view


[(alias [, alias] …)]
AS sub-query
[WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READY ONLY]

OR REPLACE – recreates the view if it already exists.


FORCE – creates the view regardless of whether or not the base tables exist.
NOFORCE – creates the view only if the base tables exist. This is the default.
View – the name of the view.
Alias – specifies names for the expressions selected by the view’s query. The number of
aliases must match the number of expressions selected by the view.
Sub-query – A complete SELECT statement. You can use aliases for the columns in the
SELECT list.
WITH CHECK OPTION – specified that only rows accessible to the view can be inserted or
updated.
Constraint – the name assigned to the CHECK OPTION constraint.
WITH READ ONLY – ensures that no DML operations can be performed on this view.

Data Manipulation Language - Statements:

Q: 1 – Inserting new rows into the department ‘DEPT‘ table.

SQL> INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES


(50,'DEVELOPMENT','DETROIT');

1 row created.
SQL> select * from dept;

DEPTNO DNAME LOC


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

Q: 2 – Inserting rows with NULL values.

Implicit method: Omit the column from the column list.

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


DDL AND DML
Statements
SQL> INSERT INTO DEPT (DEPTNO, DNAME) VALUES (60,’MIS’);

1 row created.

SQL>select * from dept;

DEPTNO DNAME LOC


---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 DEVELOPMENT DETROIT
60 MIS

6 rows selected.

Explicit method: Specify the NULL keyword.

SQL>INSERT INTO DEPT VALUES (70,’FINANCE’, NULL);

SQL> select * from dept;

DEPTNO DNAME LOC


---------- -------------------- --------------------
10 SALES ARIZONA
20 PRODUCTION CALIFORNIA
30 MANUFACTURE TEXAS
40 SUPPLY CHENNAI
50 DEVELOPMENT DETROIT
70 FINANCE
30 MANUFACTURE GEORGIA
80 EDUCATION ATLANTA

8 rows selected.

Q: 3 – Inserting special values.

Adding new employee record to the Employee ‘EMP’ table using SYSDATE and
USER.
SYSDATE and USER function records the current date and time.

SQL>INSERT INTO EMP


(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES
(7196,USER,’SALESMAN’,7782,SYSDATE,2000,NULL,10);

1 row created.

SQL> select * from emp;


EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- --------------- ---------- -------
7198 SCOTT SALESMAN 7782 21-APR-07 2000 20
2296 AROMANO CLERK 7782 03-FEB-97 1300 30
7196 SCOTT SALESMAN 7782 22-APR-07 2000 10

Q: 4 – Inserting Specific Date Values.

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


DDL AND DML
Statements
Adding a new employee record to the Employee ‘EMP’ table.

SQL>INSERT INTO EMP VALUES (2296,’AROMANO’,’SALESMAN’, 7782, TO_DATE


(‘FEB 3, 97’,’MON DD, YY’), 1300, NULL, 10);

1 row created.

SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


---------- -------------------- --------------- ---------- -------
7196 Smith WORKER 7799 02-FEB-93 1000 10
7198 SCOTT SALESMAN 7782 21-APR-07 2000 20

2 rows selected.

Q: 5 – Substitution variables (&) and (&&).

Create an interactive script by using SQL *Plus substituting parameters.

SQL>INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES


(‘&DEPARTMENT_ID’,’&DEPARTMENT_NAME’,’&LOCATION’);

Enter value for department_id: 80


Enter value for department_name: EDUCATION
Enter value for location: ATLANTA
old 1: INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES
('&DEPARTMENT_ID','&DEPARTMENT_NAME','&LOCATION')
new 1: INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES
('80','EDUCATION','ATLANTA')

1 row created.

SQL> select * from dept;


DEPTNO DNAME LOC
---------- -------------------- ------------
10 SALES ARIZONA
20 PRODUCTION CALIFORNIA
30 MANUFACTURE TEXAS
40 SUPPLY CHENNAI
50 DEVELOPMENT DETROIT

5 rows selected.

SQL> SELECT EMPNO, ENAME, JOB, &&COLUMN_NAME FROM EMP ORDER BY


&COLUMN_NAME;

Enter value for column_name: DEPTNO


old 1: SELECT EMPNO, ENAME, JOB, &&COLUMN_NAME FROM EMP ORDER BY
&COLUMN_NAME
new 1: SELECT EMPNO, ENAME, JOB, DEPTNO FROM EMP ORDER BY DEPTNO

EMPNO ENAME JOB DEPTNO


---------- ---------- -------- ----------
SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.
DDL AND DML
Statements
2296 AROMANO SALESMAN 10
7196 SCOTT SALESMAN 10
7198 Ada mech 20

3 rows selected.

Q: 6 – Copying from one table to another.

Using the UPDATE statement.

SQL> INSERT INTO MANAGERS (ID, NAME, SALARY, HIREDATE) SELECT


EMPNO, ENAME, SAL, HIREDATE FROM EMP WHERE JOB = ‘MANAGER’;

3 rows created.

SQL> SELECT * FROM MANAGERS;

ID NAME SALARY HIREDATE


---------- ----------- ---------- ---------
7799 James 8100 11-JAN-99
7783 Steve 8100 16-NOV-91
7787 Tommy 8100 12-FEB-80

3 rows selected.

Q: 7 – Updating rows in a table.

SQL> UPDATE EMP2 SET DEPTNO=20;

4 rows updated.

SQL> select * from emp2;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


---------- -------------------- --------------- ---------- --------
7196 Smith salesman 7799 02-FEB-93 1000 20
7799 James manager 11-JAN-99 8100 100 20
7184 Brooks worker 7787 19-AUG-97 3200 20
7196 SCOTT SALESMAN 7782 21-APR-07 2000 20

4 rows selected.

SQL> UPDATE EMP2 SET DEPTNO = 55 WHERE DEPTNO = 20;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


---------- -------------------- --------------- ---------- --------
7196 Smith salesman 7799 02-FEB-93 1000 55
7799 James manager 11-JAN-99 8100 100 55
7184 Brooks worker 7787 19-AUG-97 3200 55
7196 SCOTT SALESMAN 7782 21-APR-07 2000 55

4 rows selected.

Q: 8 – Deleting a set of rows from a table.

Specific row or rows are deleted when you specify the WHERE clause.

SQL> DELETE FROM DEPT2 WHERE DNAME = ‘DEVELOPMENT’;


SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.
DDL AND DML
Statements
1 row deleted.

SQL> select * from dept;


DEPTNO DNAME LOC
---------- -------------------- --------------------
10 SALES ARIZONA
20 PRODUCTION CALIFORNIA
30 MANUFACTURE TEXAS
40 SUPPLY CHENNAI

4 rows selected.
`
SQL> DELETE FROM DEPT2;

4 rows deleted.

DATABASE TRANSACTIONS:

A transaction begins when the first executable SQL statement is encountered


and terminates when one of the following occurs:

A COMMIT or ROLLBACK statement is issued.


A DDL statement, such as CREATE is issued.
A DCL statement is issued.
The user exits SQL Plus.
The machine fails or the system crashes.

After the end of a transaction, the next executable SQL statement will
automatically start the next transaction. A DDL statement or a DCL statement is
automatically committed and therefore implicitly ends a transaction.

Q: 9 - Committing data:

Make changes to the EMP table as follows:

SQL> UPDATE EMP SET DEPTNO = 10 WHERE EMPNO = 7782;

1 row updated.

Commit the changes made to the EMP table so that all changes are made permanent.

SQL> COMMIT;

Commit complete.

Q: 10 - Rolling back the State of data:

1. Delete all records from the EMP2 table.

SQL> DELETE FROM EMP2;

4 rows deleted.

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


DDL AND DML
Statements

SQL> select * from emp2;

No rows selected.

2. After using ‘rollback’…

SQL> ROLLBACK;

Rollback complete.

SQL> select * from emp2;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


---------- -------------------- --------------- ---------- --------
7196 Smith salesman 7799 02-FEB-93 1000 20
7799 James manager 11-JAN-99 8100 100 20
7184 Brooks worker 7787 19-AUG-97 3200 20
7196 SCOTT SALESMAN 7782 21-APR-07 2000 20

Q: 11 - Rolling back to a marker:

1. List all records of the DEPT table.

SQL> select * from dept;


DEPTNO DNAME LOC
---------- -------------------- -----------
10 SALES ARIZONA
20 PRODUCTION CALIFORNIA
30 MANUFACTURE TEXAS
40 SUPPLY CHENNAI

4 rows selected.

2. Make a save-point so that the state of data in the database can be restored to this point,
later.

SQL> SAVEPOINT update_done;

Savepoint created.

3. Make changes to the DEPT table as follows:

SQL> INSERT INTO DEPT (DEPTNO, DNAME) VALUES (60,’MIS’);

1 row created.

4. List all records of the DEPT table.

SQL> select * from dept;


DEPTNO DNAME LOC
---------- -------------------- -----------
10 SALES ARIZONA
20 PRODUCTION CALIFORNIA
30 MANUFACTURE TEXAS

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


DDL AND DML
Statements
40 SUPPLY CHENNAI
60 MIS

5 rows selected.

5. Use ROLLBACK to restore the database to state UPDATE_DONE..

SQL> ROLLBACK TO update_done;

Rollback complete.

Data Definition Language - Statements:

Q: 12 – Creating tables.

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

Table created.

SQL>DESC DEP;

Name Null? Type


------------------------------------------------------
DEPTNO NUMBER (2)
DNAME VARCHAR2 (14)
LOC VARCHAR2 (13)

Q: 13 – Querying the Data dictionary.

SQL>SELECT DISTINCT OBJECT_TYPE FROM USER_OBJECTS;

OBJECT_TYPE
------------
INDEX
TABLE

SQL>SELECT * FROM USER_CATALOG;

TABLE_NAME TABLE_TYPE
------------------------------ -----------
DEP TABLE
DEPT2 TABLE
DEPT3 TABLE
DEPT5 TABLE
EMP TABLE
EMP2 TABLE
EMP3 TABLE
EMP4 TABLE
MANAGERS TABLE

9 rows selected.

Q: 14 – Create table using sub-query.

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


DDL AND DML
Statements
SQL>CREATE TABLE DEPT30 AS SELECT EMPNO, ENAME, SAL*12 ANNSAL,
HIREDATE FROM EMP WHERE DEPTNO=30;

Table created.
SQL>DESC DEPT30;

Name Null? Type


-----------------------------------------
EMPNO NUMBER (4)
ENAME VARCHAR2 (20)
ANNSAL NUMBER
HIREDATE DATE

ALTERING A TABLE:

Q: 15 – Adding a column.

SQL>ALTER TABLE DEPT30 ADD (JOB VARCHAR2 (9));

Table altered.

SQL>DESC DEPT30;

Name Null? Type


-----------------------------------------
EMPNO NUMBER (4)
ENAME VARCHAR2 (20)
ANNSAL NUMBER
HIREDATE DATE
JOB VARCHAR2 (9)

Q: 16 – Modifying a column.

SQL> ALTER TABLE DEPT30 MODIFY (ENAME VARCHAR2 (15));

Table altered.

SQL>DESC DEPT30;

Name Null? Type


-----------------------------------------
EMPNO NUMBER (4)
ENAME VARCHAR2 (15)
ANNSAL NUMBER
HIREDATE DATE
JOB VARCHAR2 (9)
Q: 17 – Dropping a column.

SQL> ALTER TABLE DEPT30 DROP COLUMN ENAME;

Table altered.

SQL> desc dept30;

Name Null? Type


----------------------------- -------- --
EMPNO NUMBER (4)
ANNSAL NUMBER
HIREDATE DATE
SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.
DDL AND DML
Statements
JOB VARCHAR2 (9)

SQL>ALTER TABLE DEPT30 SET UNUSED COLUMN ENAME;

SQL> ALTER TABLE DEPT30 DROP UNUSED COLUMNS;

SQL> desc dept30;

Name Null? Type


----------------------------- -------- --
EMPNO NUMBER (4)
ANNSAL NUMBER
HIREDATE DATE
JOB VARCHAR2 (9)

Q: 18 – Dropping a Table.

SQL> DROP TABLE DEPT30;

Table dropped.

Q: 19 – Renaming an object.

SQL> RENAME DEPT2 TO DEPARTMENT;

Table renamed.

Q: 20 – Truncating a Table.

SQL> TRUNCATE TABLE DEPARTMENT;

Table truncated.

SQL> Select * from department;

No rows selected.

Q: 21 – Adding comments to a table.

SQL> COMMENT ON TABLE EMP IS ‘EMPLOYEE INFORMATION’;

Comment created.

CONSTRAINTS:

Q: 21 – Using the NOT NULL constraint.

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


DDL AND DML
Statements
SQL> CREATE TABLE EMP3(EMPNO NUMBER(4), ENAME VARCHAR2(10) NOT
NULL, JOB VARCHAR2(9), MGR NUMBER(4), HIREDATE DATE, SAL NUMBER(7,2),
COMM NUMBER(7,2), DEPTNO NUMBER(7,2) NOT NULL);

Table created.

SQL> desc emp3;

Name Null? Type


-----------------------------------------------
EMPNO NUMBER (4)
ENAME NOT NULL VARCHAR2 (10)
JOB VARCHAR2 (9)
MGR NUMBER (4)
HIREDATE DATE
SAL NUMBER (7, 2)
COMM NUMBER (7, 2)
DEPTNO NOT NULL NUMBER (7, 2)

Q: 22 – The UNIQUE key constraint.

SQL> CREATE TABLE DEPT3( DEPTNO NUMBER(2), DNAME VARCHAR2(14), LOC


VARCHAR2(13), CONSTRAINT DEPT_DNAME_UK UNIQUE(DNAME));

Table created.

SQL> desc dept30;


Name Null? Type
------------------------------------------------
EMPNO NUMBER(4)
ANNSAL NUMBER
HIREDATE DATE

Q: 23 – Using the PRIMARY KEY constraint.

SQL> CREATE TABLE DEPT6 ( DEPTNO NUMBER(2), DNAME VARCHAR2(14), LOC


VARCHAR2(13), CONSTRAINT DEPT_DNAME_KE UNIQUE (DNAME),
CONSTRAINT DEPT_DEPTNO_PKE PRIMARY KEY(DEPTNO));

Table created.

SQL> desc dept5;

Name Null? Type


------------------------------------------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)

Q: 24 – Using the FOREIGN KEY constraint.

SQL> CREATE TABLE EMP4( EMPNO NUMBER(4), ENAME VARCHAR2(10) NOT


NULL, JOB VARCHAR2(9), MGR NUMBER(4), HIREDATE DATE, SA NUMBER(7,2),
COMM NUMBER(7,2), DEPTNO NUMBER(7,2) NOT NULL, CONSTRAINT
EMP_DEPTNO_FK FOREIGN KEY (DEPTNO) REFERENCES DEPT5(DEPTNO));

Table created.

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


DDL AND DML
Statements
SQL> desc emp4;

Name Null? Type


------------------------------------------------
EMPNO NUMBER(4)
ENAME NOT NULL VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SA NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NOT NULL NUMBER(7,2)

Q: 25 – Adding a constraint.

SQL> ALTER TABLE EMP ADD CONSTRAINT EMP_EMPNO_PK PRIMARY


KEY(EMPNO);

Table altered.

SQL> SELECT CONSTRAINT_NAME ,CONSTRAINT_TYPE,


SEARCH_CONDITION FROM USER_CONSTRAINTS WHERE
TABLE_NAME='EMP';

CONSTRAINT_NAME C SEARCH_CONDITION
------------------------------ - -----------------------------------------------------------------------
EMP_EMPNO_PK P

SQL>ALTER TABLE EMP6 ADD CONSTRAINT EMP_MGR_FK FOREIGN


KEY (MGR) REFERENCES EMP6(EMPNO);

Table altered.

SQL> desc emp6;


Name Null? Type
--------------------------------------------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(20)
JOB VARCHAR2(15)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(5)
COMM NUMBER(4)
DEPTNO NUMBER(2)

Q: 26 – Dropping a constraint.

SQL>ALTER TABLE EMP6 DROP CONSTRAINT EMP_MGR_FK;


Table altered.

SQL> desc emp6;

Name Null? Type


--------------------------------------------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(20)
JOB VARCHAR2(15)
SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.
DDL AND DML
Statements
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(5)
COMM NUMBER(4)
DEPTNO NUMBER(2)

SQL> ALTER TABLE DEPT6 DROP PRIMARY KEY CASCADE;


Table altered.

SQL> desc dept6;

Name Null? Type


-------------------------------------------------
DEPTNO NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)

Q: 27 – Disabling a constraint.

SQL> ALTER TABLE EMP6 DISABLE CONSTRAINT EMP_EMPNO_PKE


CASCADE;

Constraint disabled.

Q: 28 – Enabling a constraint.

Constraint enabled.

SQL> ALTER TABLE EMP ENABLE CONSTRAINT EMP_EMPNO_PK;

Q: 29 – Viewing constraints.

SQL< SELECT CONSTRAINT_NAME ,CONSTRAINT_TYPE,


SEARCH_CONDITION FROM USER_CONSTRAINTS WHERE
TABLE_NAME=’EMP4’;

CONSTRAINT_NAME C SEARCH_CONDITION
------------------------------ - ----------------------
SYS_C003010 C "ENAME" IS NOT NULL
SYS_C003011 C "DEPTNO" IS NOT NULL
EMP_DEPTNO_FK R

Q: 11 – Specifying columns with constraints.

SQL>SELECT CONSTRAINT_NAME, COLUMN_NAME FROM


USER_CONS_COLUMNS WHERE TABLE_NAME=’EMP4’;

CONSTRAINT_NAME COLUMN_NAME
-------------------------------------------------------
EMP_DEPTNO_FK DEPTNO
SYS_C003010 ENAME
SYS_C003011 DEPTNO
SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.
DDL AND DML
Statements

VIEWS

Q: 30 – Creating a view.

SQL> CREATE VIEW EMPVU10 AS SELECT EMPNO, ENAME, JOB FROM


EMP WHERE DEPTNO=10;

View created.

Q: 31 – Describing the structure of a view.

SQL>DESC EMPVU10;

Name Null? Type


------------------------------------------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(20)
JOB VARCHAR2(15)

Q: 32 – Retrieving Data from a View.

SQL>SELECT * FROM EMPVU10;

EMPNO ENAME JOB


----- -------------------- -------------
7196 Smith salesman
7197 John rep
7799 James manager
7783 Steve manager
7196 SCOTT SALESMAN
2296 AROMANO SALESMAN
7196 SCOTT SALESMAN

Q: 33 – Modifying a view.

SQL> CREATE OR REPLACE VIEW EMPVU10(EMPLOYEE_NUMBER,


EMPLOYEE_NAME,JOB_TITLE) AS SELECT EMPNO,ENAME,JOB FROM
EMP4 WHERE EMPNO=10;

SQL>desc empvu10;
EMPLOYEE_NUMBER EMPLOYEE_NAME JOB_TITLE
---------------------------------------------------------
7196 Smith salesman
7197 John rep
7799 James manager

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


DDL AND DML
Statements
7783 Steve manager
7196 SCOTT SALESMAN
2296 AROMANO SALESMAN
7196 SCOTT SALESMAN

Q: 34 – Creating a complex view.

SQL>CREATE VIEW DEPT_SUM_VU(NAME,MINSAL,MAXSAL,AVGSAL) AS


SELECT D.DNAME,MIN(E.SAL),MAX(E.SAL),AVG(E.SAL) FROM EMP
E,DEPT3 D WHERE E.DEPTNO=D.DEPTNO GROUP BY D.DNAME;

View created.

SQL>desc dept_sum_vu;
Name Null? Type
-------------------------------------------------
VARCHAR2(14)
MINSAL NUMBER
MAXSAL NUMBER
AVGSAL NUMBER

Q: 35 - Using the ‘WITH CHECK OPTION’.

SQL>CREATE OR REPLACE VIEW EMPVU20 AS SELECT * FROM EMP WHERE


DEPTNO = 20 WITH CHECK OPTION CONSTRAINT EMPVU20_CK;

SQL>desc empvu20;
Name Null? Type
-------------------------------------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(20)
JOB VARCHAR2(15)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(5)
COMM NUMBER(4)
DEPTNO NUMBER(2)

Q: 36 – Denying DML operations.

SQL> CREATE OR REPLACE VIEW EMPVU10 (EMPLOYEE_NUMBER,


EMPLOYEE_NAME, JOB_TITLE) AS SELECT EMPNO, ENAME, JOB FROM EMP
WHERE DEPTNO = 10 WITH READ ONLY;

View created.

SQL>desc empvu10;

Name Null? Type


---------------------------------------------
EMPLOYEE_NUMBER NUMBER(4)
EMPLOYEE_NAME VARCHAR2(20)
JOB_TITLE VARCHAR2(15)

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.


DDL AND DML
Statements
Q: 37 – Removing a view.

SQL> DROP VIEW EMPVU10;

View dropped.

SQL>desc EMPVU10;

View or table does’nt exist.

SHASHIKIRAN V., ASSISTANT PROFESSOR (SENIOR), SITE, VIT VELLORE.

You might also like