Sie sind auf Seite 1von 9

SUJIT KUMAR

2911190

EX.NO:-2 DATE:-

Data Definition Language

Aim: To Perform the DDL Commands Q1: Create a table called Employee with the following structure
Name Fname Minit Lname SSN Gender Salary SuperSSN Dno Allow Null for all columns except Fname Set primary constraint to SSN Type varchar2(10) varchar2(1) varchar2(10) number(9) varchar2(21) number(9,2) number(9) number(1)

SQL>create table employee(Fname varchar2(10) NOT NULL,Minit varchar2(1), Lname


varchar2(10),SSN number(9) Primary Key,Gender varchar2(1),Salary number(9,2), SuperSSN number(9),Dno number(1));

Table created. SQL> desc employee Name Null? ----------------------------------------- ---------------FNAME NOT NULL MINIT LNAME SSN NOT NULL GENDER SALARY SUPERSSN DNUMBER Type -------------------VARCHAR2(10) VARCHAR2(11) VARCHAR2(10) NUMBER(9) VARCHAR2(6) NUMBER(9,2) NUMBER(9) NUMBER(13)

SUJIT KUMAR

2911190

Q2: Create a table called Department with the following structure


Name Type Dname varchar2(6) Dnumber number(1) Mgrssn number(9) Mgrstartdate date Allow Null for all columns except Dname Set primary key constraint to Dnumber and foreign key constraint to mgrssn SQL>Create table Department(Dname VarChar2(6) NOT NULL,DNumber Number(1),
Primary Key,MgrSSN Number(9),MgrStartDate Date,Constraint fk Foreign Key(mgrSSN) references Employee(SSN));

Table created. SQL> desc Department; Name Null? ----------------------------------------- ---------------DNAME NOT NULL DNUMBER NOT NULL MGRSSN MGRSTARTDATE Type -------------------VARCHAR2(6) NUMBER(1) NUMBER(9) DATE

Q3: Create a table called Dept_locations with the following structure Name Type

Dnumber number(1) Dlocation varchar2(10) Allow Not Null for all columns Set primary key constraint to both Dnumber and Dlocation Set foriegn key constraint to Dnumber SQL>Create table Dept_Locations(Dnumber Number(1), DLocation VarChar2(10),
Primary Key (Dnumber, DLocation),Constraint fk2 Foreign Key (Dnumber) references Department(DNumber));

Table created.

SUJIT KUMAR

2911190

SQL> desc Dept_locations; Name Null? ----------------------------------------- ---------------DNUMBER NOT NULL DLOCATION NOT NULL Type -------------------NUMBER(1) VARCHAR2(10)

Q4: Create a table called project with the following structure


Name Pname Pnumber Dnum Set primary constraint to Pnumber Set foriegn key constraint to Dnum Type varchar2(15) number(2) number(1)

SQL>Create table Project(PName VarChar2(15), PNumber Number(2), DNum


Number(1),Primary Key(PNumber),Constraint fk5 Foreign Key (DNum) references Department(DNumber));

Table created. SQL> desc Project; Name Null? ----------------------------------------- ---------------PNAME PNUMBER NOT NULL DNUM Type -------------------VARCHAR2(15) NUMBER(2) NUMBER(1)

Q5: Create a table called Works_on with the following structure


Name Type ESSN number(9) Pno number(2) Hours number(2,1) Allow Not Null for all columns except hoursSet primary key constraint to both Pno and ESSN Set foriegn key constraint to Pno and ESSN

SUJIT KUMAR

2911190

SQL>Create table Works_on(ESSN Number(9),PNo Number(2),Hours Number(2,1),


Primary Key(PNo, ESSN),Constraint fk4 Foreign Key (PNo) references Project(PNUMBER),Constraint fk42 Foreign Key(ESSN) references Employee(SSN));

Table created. SQL> desc Works_on; Name Null? ----------------------------------------- ---------------ESSN NOT NULL PNO NOT NULL HOURS Type -------------------NUMBER(9) NUMBER(2) NUMBER(2,1)

Q6: Create a table called Dependent with the following structure


Name Type ESSN number(9) Dependent_Name varchar2(10) Gender varchar2(1) Bdate date Relationship varchar2(10) Allow Not Null for the columns ESSN and Dependent_Name Set primary key constraint to both ESSN and Dependent_Name Set foriegn key constraint to ESSN SQL>create table dependent(essn number(10),dependent_name varchar2(10),gender
varchar2(10),BDate date,Relationship varchar2(10), primar key(essn,dependent_name),constraints nk foreign key(essn) references employee(ssn));

Table created.

SUJIT KUMAR

2911190

SQL> desc dependent Name Null? ----------------------------------------- ----------------ESSN NOT NULL DEPENDENT_NAME NOT NULL GENDER BDATE RELATIONSHIP Type ------------------NUMBER(10) VARCHAR2(10) VARCHAR2(10) DATE VARCHAR2(10)

Q7: Add a column Bdate to the employee table of data type Date with null allowed
SQL>alter table employee add(bdate date); Table altered.

SQL> desc employee Name Null? ----------------------------------------- ----------------FNAME NOT NULL MINIT LNAME SSN NOT NULL GENDER SALARY SUPERSSN DNUMBER BDATE Type ------------------VARCHAR2(10) VARCHAR2(11) VARCHAR2(10) NUMBER(9) VARCHAR2(6) NUMBER(9,2) NUMBER(9) NUMBER(13) DATE

Q8: Modify the column width of the relationship field of Dependent table
SQL>alter table dependent modify(relationship varchar2(35)); Table altered.

SUJIT KUMAR

2911190

SQL> desc dependent Name Null? Type ----------------------------------------- ---------------- -------------------ESSN NOT NULL NUMBER(10) DEPENDENT_NAME NOT NULL VARCHAR2(10) GENDER VARCHAR2(10) BDATE DATE RELATIONSHIP VARCHAR2(35)

Q9: Add constraints to the employee table that Dno as the foreign key
SQL>alter table employee add(constraint f10 foreign key(dnumber) references
depatment(dnumber));

Table altered. SQL> desc employee Name Null? ----------------------------------------- ---------------FNAME NOT NULL MINIT LNAME SSN NOT NULL GENDER SALARY SUPERSSN DNUMBER BDATE Type -------------------VARCHAR2(10) VARCHAR2(11) VARCHAR2(10) NUMBER(9) VARCHAR2(6) NUMBER(9,2) NUMBER(9) NUMBER(13) DATE

Q10: Add constraints to the employee table that SuperSSN as the foreign key
SQL>alter table employee add(constraints jk foreign key(superssn) references
project(pnumber));

Table altered. SQL> desc employee

SUJIT KUMAR

2911190

Name Null? ----------------------------------------- ---------------FNAME NOT NULL MINIT LNAME SSN NOT NULL GENDER SALARY SUPERSSN DNUMBER BDATE

Type -------------------VARCHAR2(10) VARCHAR2(11) VARCHAR2(10) NUMBER(9) VARCHAR2(6) NUMBER(9,2) NUMBER(9) NUMBER(13) DATE

Q11: Add constraints to the employee table to check the SSN while entering (i.e)
empno>100 SQL> alter table employee add check(ssn>100); Table altered. SQL> desc employee Name Null? ----------------------------------------- ---------------FNAME NOT NULL MINIT LNAME SSN NOT NULL GENDER SALARY SUPERSSN DNUMBER BDATE Type -------------------VARCHAR2(10) VARCHAR2(11) VARCHAR2(10) NUMBER(9) VARCHAR2(6) NUMBER(9,2) NUMBER(9) NUMBER(13) DATE

Q12: Set salary by default as 5000


SQL>alter table employee modify(salary default 5000);

Table altered.

SUJIT KUMAR

2911190

SQL> desc employee Name Null? ----------------------------------------- ---------------FNAME NOT NULL MINIT LNAME SSN NOT NULL GENDER SALARY SUPERSSN DNUMBER BDATE Type -------------------VARCHAR2(10) VARCHAR2(11) VARCHAR2(10) NUMBER(9) VARCHAR2(6) NUMBER(9,2) NUMBER(9) NUMBER(13) DATE

Q13: Add the column Plocation to the project table


SQL> alter table project add(plocation varchar2(10)); Table altered. SQL> desc project Name Null? ----------------------------------------- ----------------PNAME PNUMBER NOT NULL DNUMBER PLOCATION Type ------------------VARCHAR2(10) NUMBER(10) NUMBER(10) VARCHAR2(10)

Q14: Create one table and delete it


SQL> create table bye(welcome varchar2(20),mdate date,bye varchar(10)); Table created. SQL> desc bye Name Null? ----------------------------------------- ----------------WELCOME MDATE BYE Type ------------------VARCHAR2(20) DATE VARCHAR2(10)

SUJIT KUMAR

2911190

SQL>drop table bye; Table dropped.

Result:
The queries in Data Definition Language that is DDL are done successfully and the output is verified.

Das könnte Ihnen auch gefallen