DBMS 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Ex.

No : 1 Data Definition Commands, Data Manipulation Commands for inserting,


deleting, updating and retrieving Tables and Transaction Control statements

AIM:
To implement and execute a query for creating, manipulating & storing data items in a Oracle database
using Structured Query Language commands
DDL COMMANDS:
Data Definition Language (DDL) or Schema Definition Language, statements are used to define the
database structure or schema.
 CREATE - to create objects in the database
 ALTER - alters the structure of the database
 DROP - delete objects from the database
 TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed
 RENAME - rename an object
CREATE Command:
This command is used to create a table.
Syntax:
SQL> CREATE TABLE TABLENAME (COLUMN NAME1 <DATATYPE> (SIZE),
COLUMN NAME.2 <DATATYPE> (SIZE) ………);
Example:
SQL>CREATE TABLE EMP (EMPNO NUMBER (4),ENAME VARCHAR2 (10), DESIGNATION VARCHAR2
(10),SALARY NUMBER (8, 2));
Table created
CREATE Command:
Create a table from an existing table with all fields.
Syntax:
SQL> CREATE TABLE <TRAGET TABLE NAME> SELECT * FROM<SOURCE TABLE NAME>;
Example:
SQL> CREATE TABLE EMP1 AS SELECT * FROM EMP;
Table created.
CREATE Command:
Create a table from an existing table with selected fields
Syntax:
SQL> CREATE TABLE <TRAGET TABLE NAME> AS SELECT EMPNO, ENAME FROM
<SOURCE TABLE NAME>;
Example:
SQL> CREATE TABLE EMP2 AS SELECT EMPNO, ENAME FROM EMP;
Table created.
TABLE DESCRIPTION
It is used to view the table structure to confirm whether the table was created correctly.
Syntax:
SQL> DESC <TABLE NAME>;
Example:
SQL> DESC EMP;
Name Null? Type
--------------- ----------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
DESIGNATION VARCHAR2(10)
SALARY NUMBER(8,2)
ALTER Command:
This command is used to add, delete, and modify columns in an existing table.

ALTER_ADD Command
To add a new column in a table
Syntax:
SQL> ALTER TABLE <TABLE NAME> ADD (<COLUMN NAME><DATATYPE><SIZE>);
Example:
SQL> ALTER TABLE EMP ADD QUALIFICATION VARCHAR2(6);
Table altered.
SQL> DESC EMP;
Name Null? Type
---------------- ------------------------
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATION VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)

ALTER_Remove/Drop Command
It will delete the table structure provided the table should be empty.
Syntax:
SQL> ALTER TABLE <TABLE NAME> DROP COLUMN <COLUMN NAME>;
Example:
SQL> ALTER TABLE EMP DROP COLUMN DOJ;
Table altered.
SQL> DESC EMP;
Name Null? Type
----------------- ------------------------------
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATION VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)
DOB DATE
ALTER_Modify Command
To change the data type of a column in a table
Syntax:
SQL > ALTER TABLE <TABLE NAME> MODIFY <COLUMN NAME><DATATYPE> (SIZE);
Example:
SQL>ALTER TABLE EMP MODIFY EMPNO NUMBER (6);
Table altered.
SQL> DESC EMP;
Name Null? Type
----------------- ----------------------------------
EMPNO NUMBER(6)
ENAME VARCHAR2(10)
DESIGNATION VARCHAR2(10)
SALARY NUMBER(8,2)

RENAME Command
This command is used to rename a table.
Syntax:
SQL> ALTER TABLE RENAME <OLD NAME> TO <NEW NAME>
Example:
SQL> ALTER TABLE RENAME EMP TO EMPLOYEE;
SQL> DESC EMPLOYEE;
Name Null? Type
------------------- --------------------------
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATION VARCHAR2(10)
SALARY NUMBER(8,2)

TRUNCATE Command
If there is no further use of records stored in a table and the structure has to be retained then the records
alone can be deleted.
Syntax:
TRUNCATE TABLE <TABLE NAME>;
Example:
Truncate table EMP;
DROP Command
To remove a table along with its structure and data.
Syntax:
SQL> Drop table<table name>;
Example:
SQL> drop table employee;
DML (DATA MANIPULATION LANGUAGE) COMMAND
 Insert
 Select
 Update
 Delete
INSERT COMMAND:
The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.
Insert a record from an existing table:
Syntax
SQL :> INSERT INTO <TABLE NAME> VALUES< ‘VAL1’, ‘VAL2’,…..);
Example:
SQL>INSERT INTO EMP VALUES (101,'NAGARAJAN','LECTURER',15000);
1 row created.
Insert A Record Using Substitution Method:
Syntax
INSERT INTO <TABLE NAME> VALUES< ‘&column name’, ‘&column name 2’, …..);
Example:
SQL> INSERT INTO EMP VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY');
Enter value for empno: 102
Enter value for ename: SARAVANAN
Enter value for designatin: LECTURER
Enter value for salary: 15000
1 row created.
old 1: INSERT INTO EMP VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY')
new 1: INSERT INTO EMP VALUES(102,'SARAVANAN','LECTURER','15000')
SQL> /
Enter value for empno: 103
Enter value for ename: PANNERSELVAM
Enter value for designatin: ASST. PROF
Enter value for salary: 20000
1 row created.
old 1: INSERT INTO EMP VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY')

new 1: INSERT INTO EMP VALUES(103,'PANNERSELVAM','ASST.PROF','20000')


SQL> /
Enter value for empno: 104
Enter value for ename: CHINNI

Enter value for designatin: HOD,


PROF Enter value for salary: 45000
1 row created.
old 1: INSERT INTO EMP VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY')
new 1: INSERT INTO EMP VALUES(104,'CHINNI','HOD, PROF','45000')

SQL> SELECT * FROM EMP;


EMPNO ENAME DESIGNATIN SALARY
----------- ------------ ------------------- --------------
101 NAGARAJAN LECTURER 15000
102 SARAVANAN LECTURER 15000
103 PANNERSELVAM ASST. PROF 20000
104 CHINNI HOD, PROF 45000

SELECT COMMAND
SELECT Statement is used to fetch the data from a database table which returns data in the form
of result table. These result tables are called result-sets.
Syntax
SQL> SELECT * FROM <TABLE NAME>;
Example:
SQL> SELECT * FROM EMP;

EMPNO ENAME DESIGNATIN SALARY


---------- ------------ ---------- ----------
101 NAGARAJAN LECTURER 15000

UPDATE Command
The SQL UPDATE Query is used to modify the existing records in a table. You can use WHERE clause
with UPDATE query to update selected rows, otherwise all the rows would be affected.
Syntax
SQL> UPDATE <<TABLE NAME> SET <COLUMNANE>=<VALUE> WHERE <COLUMNNAME=<VALUE>;

Example

SQL> UPDATE EMP SET SALARY=16000 WHERE EMPNO=101;

1 row updated.

SQL> SELECT * FROM EMP;

EMPNO ENAME DESIGNATIN SALARY


---------- ------------ ------------------- ---------------
101 NAGARAJAN LECTURER 16000
102 SARAVANAN LECTURER 15000
103 PANNERSELVAM ASST. PROF 20000
104 CHINNI HOD,PROF 45000

Update Multiple Columns:

To update multiple records from the table.

Syntax
SQL> UPDATE <<TABLE NAME> SET <COLUMNANE>=<VALUE> WHERE <COLUMN NAME=<VALUE>;

Example

SQL>UPDATE EMP SET SALARY = 16000, DESIGNATIN='ASST. PROF' WHERE EMPNO=102;

1 row updated.

SQL> SELECT * FROM EMP;

EMPNO ENAME DESIGNATIN SALARY


---------- ------------ -------------------- --------------
101 NAGARAJAN LECTURER 16000
102 SARAVANAN ASST. PROF 16000
103 PANNERSELVAM ASST. PROF 20000
104 CHINNI HOD, PROF 45000

DELETE Command
The SQL DELETE Query is used to delete the existing records from a table. You can use WHERE clause
with DELETE query to delete selected rows, otherwise all the records would be deleted.
Syntax
SQL> DELETE <TABLE NAME> WHERE <COLUMN NAME>=<VALUE>;
Example
SQL> DELETE EMP WHERE EMPNO=103;
1 row deleted.
SQL> SELECT * FROM EMP;
EMPNO ENAME DESIGNATIN SALARY
---------- ------------ --------------------- --------------
101 NAGARAJAN LECTURER 16000
102 SARAVANAN ASST. PROF 16000
104 CHINNI HOD, PROF 45000

TCL Commands
Transaction Control Language (TCL) commands are used to manage transactions in the database. These
are used to manage the changes made to the data in a table by DML statements.
 COMMIT
 ROLLBACK
 SAVEPOINT
COMMIT Command
COMMIT command is used to permanently save any transaction into the database.
When we use any DML command like INSERT, UPDATE or DELETE, the changes made by these
commands are not permanent, until the current session is closed, the changes made by these commands
can be rolled back.
Syntax
SQL>COMMIT;
ROLLBACK command
This command restores the database to last commited state.It is also used with SAVEPOINT command to
jump to a savepoint in an ongoing transaction. If we have used the UPDATE command to make some
changes into the database, and realise that those changes were not required, then we can use
the ROLLBACK command to rollback those changes, if they were not commited using
the COMMIT command.
Syntax
Rollback to Savepoint_name;
SAVEPOINT command
SAVEPOINT command is used to temporarily save a transaction so that you can rollback to that point
whenever required.
Syntax
Savepoint Savepoint_name;

RESULT:
Thus, a query for creating, manipulating & storing data items in an Oracle database using Structured Query
Language commands were implemented and executed successfully.

You might also like