Professional Documents
Culture Documents
DBMS First Exercise
DBMS First Exercise
AIM:
To execute the various Data Definition Language commands in RDBMS.
OBJECTIVE:
After completing the exercise the students can able to Understand how to create a table
with list of fields, Modify a row using where clause, Drop a table, Delete the unwanted rows in a
table.
DATA DEFINITION LANGUAGE
It is used to communicate with database.
DDL is used to:
Create an object
Alter the structure of an object
To drop the object created.
ALGORITHM:
Step 1: Start the program
Step 2: Go to SQL.
Step 3: Enter the user name and password.
Step 4: Connect to the database.
Step 5: Type the commands for creating tables and perform various operations on the tables.
Step 6: The output is displayed.
Step 7: Stop the program
DDL COMMAND:
CREATE
ALTER
DROP
TRUNCATE
RENAME
CREATE TABLE
QUERY: 01
Q1: Write a query to create a table employee with empno, ename, designation, and salary.
Syntax: It is used to create a table
SQL: CREATE TABLE <TABLE NAME>(COLUMN NAME.1 <DATATYPE> (SIZE),
COLUMN NAME.2 <DATATYPE> (SIZE) ………);
Command:
SQL>CREATE TABLE EMP (EMPNO INT,ENAME VARCHAR
(10),DESIGNATIN VARCHAR (10),SALARY INT);
Table created.
TABLE DESCRIPTION
It is used to view the table structure to confirm whether the table was createdcorrectly.
QUERY: 02
Q2: Write a query to display the column name and data type of the table employee.
Syntax: This is used to view the structure of the table.
SQL: DESC <TABLE NAME>;
Command:
SQL> DESC EMP;
Name Null? Type
--------------- ----------------
EMPNO INT
ENAME VARCHAR2(10)
DESIGNATINVARCHAR2(10)
SALARY INT
QUERY: 03
Q3: Write a query for create a from an existing table with all the fields
Syntax: syntax for create a table from an existing table with all fields.
SQL> CREATE TABLE <TRAGET TABLE NAME> AS SELECT * FROM<SOURCE
TABLE NAME>;
Command:
SQL> CREATE TABLE EMP1 AS SELECT * FROM EMP;
Table created.
Command:
QUERY: 04
Q4: Write a query for create a table from an existing table with selected fields
Syntax: Syntax for create a from an existing table with selected fields.
SQL> CREATE TABLE <TRAGET TABLE NAME> AS SELECT EMPNO, ENAMEFROM
<SOURCE TABLE NAME>;
Command:
SQL> CREATE TABLE EMP2 AS SELECT EMPNO, ENAME FROM EMP;
Table created.
Command:
SQL> DESC EMP2
Name Null? Type
----------------- --------------------- ----
EMPNO INT
VARCHAR2
ENAME (10)
QUERY: 05
Q5: Write a query for create a new table from an existing table without any record:
Syntax: The syntax for create a new table from an existing table without any record.
SQL> CREATE TABLE <TRAGET TABLE NAME> AS SELECT * FROM<SOURCE
TABLE NAME>
WHERE <FALSE CONDITION>;
Command:
SQL> CREATE TABLE EMP3 AS SELECT * FROM EMP WHERE1>2;
Table created.
Command:
QUERY:7
Q7. Write a Query to Alter the table employee with multiple columns (EMPNO,ENAME.)
Syntax: To alter table with multiple column.
SQL > ALTER <TABLE NAME> MODIFY <COLUMN NAME1><DATATYPE>(SIZE),
MODIFY <COLUMN NAME2><DATATYPE>(SIZE)………….;
Command:
SQL>ALTER TABLE EMP MODIFY (EMPNO NUMBER (7),
ENAMEVARCHAR2(12)); Table altered.
Command:
SQL> DESC EMP;
Name Null? Type
----------------- -------------------------------
EMPNO INT
ENAME VARCHAR2(12)
DESIGNATINVARCHAR2(10)
SALARY INT
QUERY: 08
Q8. Write a query to add a new column in to employee
Syntax: To add a new column.
SQL> ALTER TABLE <TABLE NAME> ADD (<COLUMN
NAME><DATATYPE><SIZE>);
Command:
SQL> ALTER TABLE EMP ADD QUALIFICATION (VARCHAR2(6));
Table altered.
SQL> DESC EMP;
Name Null? Type
---------------- ------------------------
EMPNO INT
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY INT
QUALIFICATION VARCHAR2(6)
QUERY: 09
Q9: Write a query to add multiple columns in to employee
Syntax: Syntax for add a new column.
SQL>ALTERTABLE<TABLENAME>ADD(<COLUMNNAME1><DATATYPE><SIZE>,
(<COLUMN NAME2><DATA TYPE><SIZE>…);
Command:
SQL>ALTER TABLE EMP ADD (DOB DATE, DOJ DATE);
Table altered.
SQL> DESC EMP;
Name Null? Type
----------------- --------------------------------
EMPNO INT
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY INT
QUALIFICATION VARCHAR2(6)
DOB DATE
DOJ DATE
REMOVE / DROP
It will delete the table structure provided the table should be empty.
QUERY: 10
RESULT