0% found this document useful (0 votes)
46 views6 pages

Dbms Lab6: Jenma Maria Binoy Rollno 34

This document describes using constraints in SQL queries. It provides examples of creating tables with various constraints like primary keys, foreign keys, check constraints, default values and not null constraints. The examples demonstrate creating tables for faculty, students, employees and departments with the appropriate constraints. Columns are added, modified and dropped from tables as needed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views6 pages

Dbms Lab6: Jenma Maria Binoy Rollno 34

This document describes using constraints in SQL queries. It provides examples of creating tables with various constraints like primary keys, foreign keys, check constraints, default values and not null constraints. The examples demonstrate creating tables for faculty, students, employees and departments with the appropriate constraints. Columns are added, modified and dropped from tables as needed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DBMS LAB6

Jenma Maria Binoy


ROLLNO 34  
Exercise: 6
AIM

 SQL queries using Constraints.

CONSTRAINTS:

 UNIQUE The UNIQUE constraint does not allow to insert a duplicate value in a column.
 NOT NULL NOT NULL constraint allows to specify that a column can not contain any NULL
value.
 CHECK  The CHECK constraint determines whether the value is valid or not from a logical
expression.
 DEFAULT While inserting data into a table, if no value is supplied to a column, then the
column gets the value set as DEFAULT
 KEY Constraints:
Primary key A PRIMARY KEY constraint for a table enforces the table to accept unique
data for a specific column and this constraint create a unique index for accessing the table
faster.
FOREIGN KEY A FOREIGN KEY creates a link between two tables by one specific column of
both table. The specified column in one table must be a PRIMARY KEY and referred by the
column of another table known as FOREIGN KEY.

Syntax:
Create table tablename (column_name1 data_ type constraints, column_name2 data_ type

constraints …)

1. Create table Faculty with the following fields:


a. FNo,FName, Job, DeptNo
b. FNo is the primary key
c. DeptNo will be the Foreign Key from DEPT Table;
d. All the fields must have values.
SQL> create table faculty(FNo number(2) primary key,Fname varchar(10),job varchar(15),
deptno references dept(deptno));

2. Create table student with the following fields. All the fields doesnot insert null values:
• Rollno,sname, ,dob;
• Set RollNo as the primary key of the table.
create table student(rollno number(3) primary key,sname varchar(15) not null,dob date);

3.Create table student with the following fields:


o regno number (6), mark number (3);
o the value of mark should be greater than or equal to 0 and less than or equal to 100;

o number of characters in the regno attribute would not be greater than 4.

SQL> create table student(regno number(6) primary key check(length(regno)<=4),mark number(3)


check(mark>=0 and mark<=100) not null);
4.Create a table called EMP with the following structure.

Name Type
---------- ----------------
EMPNO NUMBER(6)
ENAME VARCHAR2(2
0)
JOB VARCHAR2(1
0)
DEPTNO NUMBER(3)
SALARY NUMBER(7,2
)

 Allow NULL for all columns except ename and job.  Set default value of salary as ‘5000’;
SQL> create table emp(empno number(6) null ,ename varchar(20) not null,job varchar(10)
not null,deptno number(3) null,salary number(7,2) default 5000);

5: Add a column experience to the emp table.

 Experience(years)will be numeric attribute.

SQL> alter table emp add expirence number(2);


6: Modify the column width of the job field of emp table.

SQL> alter table emp modify job varchar(15);

7: Create DEPT table with the following structure.


Name Type
------------ ------------------
DEPTNO NUMBER(2)

DNAME VARCHAR2(10)

LOC VARCHAR2(10)

 Deptno as the primary key

SQL> create table dept(deptno number(2) primary key,dname varchar(10),loc varchar(10));


8: create the emp1 table with ename and empno, add constraints to check the empno value while

entering (i.e) empno > 100.

SQL> create table emp1(ename varchar(12),empno number(5) check(empno>100));

9: drop a column experience to the EMP table.


alter table emp drop column expirence;

RESULT:
 The DDL commands have been executed successfully and the output is verified.

You might also like