Vous êtes sur la page 1sur 7

* CONSTRAINTS: - Constraints are the rules which are applicable on table data to prevent wrong entry in any specific

attribute. We can apply data constraints to any specific column by using CREATE TABLE or ALTER TABLE command. TYPES OF DATA CONSTRAINTS:1. Input/Output Constraints 2. Business Rule Constraints 1. Input/Output Constraints:-the constraints that are general to any specific table for any organization /business are called Input/Output Constraints. Different types of these constraints are:1.) UNIQUE 2.) NOT NULL 3.) PRIMARY KEY 4.) FOREIGN KEY 1.) UNIQUE Constraints:- Using this rule we can restrict duplicate entries in the column of any specific table. QUERY 40: Create a table student having following Attributes-rollno, name & fees. Ensure that rollno is unique. SQL> create table student (rollno number(6) UNIQUE,name varchar2(15),fees number(3)); Table created. 2.) NOT NULL Constraints:- Using this constraint, we can restrict null values in any specific field of any specific table. QUERY 41: Create a table student- rollno(unique),name(not null)& fees. SQL> create table student(rollno number(6) UNIQUE,name varchar2(10) NOT NULL,fees number(3)); Table created. 3.) PRIMARY KEY: - An attribute in a table that uniquely identifies a row in a table is called primary key. i.e. an attribute having unique value & not null value is called primary key. QUERY 42: Create a table student rollno(primary key),name(not null)& fees. SQL> create table student(rollno number(6) PRIMARY KEY,name varchar2(15) NOT NULL,fees number(3)); Table created. 4.) FOREIGN KEY:-An attribute in a table which is primary key of some another table is called foreign key in the current table. A foreign key attribute establishes the relation between the 2 tables. Foreign key is represented by keyword REFRENCES.It is also called referential integrity constraint.

QUERY 43: Create a table exam brollno(primary key),examdate,subject,rollno(foreign key from the table student having rollno as an attribute). SQL> create table exam(brollno number(8) PRIMARY KEY,examdate DATE,subject varchar2(10),rollno number(6) REFERENCES student(rollno)); Table created. 5.) ON DELETE CASCADE CLAUSE: - Reference integrity constraint says that we cant delete parent record before deleting its child record. but oracle provides an option ON DELETE CASCADE using which we can delete parent & child record simultaneously. QUERY 44: Create a table exam(brollno-primary key,subject,examdate,rollno-.references from rollno of student) with on delete cascade option. SQL> create table exam(brollno number(8) PRIMARY KEY,subject varchar2(10),examdate DATE,rollno number(6) REFERENCES student(rollno)ON DELETE CASCADE); Table created. 2.) Business rule constraints:-The constraints which are specific to the table of any specific organization /business are called business rule constraints. Two types of these are:1.) CHECK 2.) DEFAULT 1.) CHECK Constraint:-Using this constraint, we can restrict table data from any specific value. A condition is specified with CHECK constraint if user enter data according to this condition than data is accepted else data is rejected. QUERY 45: Create a table employee (ecode-primary key,ename-not null,salary>=5000). SQL> create table employee(ecode varchar2(4) PRIMARY KEY,ename varchar2(15) NOT NULL,salary number(5) CHECK (SALARY>=5000)); Table created. 2.) DEFAULT Constraint:-Using this constraint, we can set default value to the attribute of any specific table. After setting default value if user enters null value then automatically default value will store. QUERY 46: Create a table employee (ecode-primary key,ename-not null,salarynot<2500,dept-default sales,doj).

SQL> create table employee(ecode varchar2(4) PRIMARY KEY,ename varchar2(15) NOT NULL,salary number(5) CHECK (Not salary<2500),dept varchar2(10) DEFAULT 'sales',doj DATE); Table created. QUERY 47: Create a table sales (prodcode,slno,quantity,saledate)where prodcode & slno are primary key. SQL> create table sales (prodcode varchar2(4),slno varchar2(4),quantity number(3),saledate date,primary key(prodcode,slno)); Table created. * NULL VALUES:- If a column in a row has no value hen the column is said to be null. Nulls can appear in column of any data type provided they are not restricted by NOT NULL constraints .we can use null values when actual value is not known. QUERY 48: Display detail for all the employees having dept is null. SQL> select * from emp where dept IS NULL; ECOD ENAME SALARY DEPT ---- ---------- --------- -------e101 suhang 25000 e103 shashank 30000 * SUBQUERY: - It is the method of including a query inside another query. It is also called nested query. With subquery, we can extract information from 2 or more tables. Normally subquery method is used to display information of one table & whose condition field is available in another table. QUERY 49: Display custname,custcity for all the customers having order an item of code 34. SQL> select custname,custcity from customer where custcode IN(select custcode from order1 where itcode='34'); CUSTNAME CUSTCITY ---------- ---------poonam sonipat nisha sonipat QUERY 50: Display orderno,orderdate & itcode for all the items which are ordered by the customer poonam. SQL> select orderno,orderdate,itcode from order where custcode IN (select custcode from customer where custname='poonam'); ORDERNO ORDERDATE ITCODE

--------- --------- --------3 05-JAN-07 34 QUERY 51: Display detail for all the customers having not placed any order. SQL> select * from customer where custcode NOT IN(select custcode from order); CUST CUSTNAME CUSTCITY ---- ---------- ---------c106 aman karnal c108 sohail new delhi QUERY 52: Display custname for the customer having placed an order of item with max amount. SQL> select custname from customer where custcode IN (select custcode from order where amount=(select max(amount) from order)); CUSTNAME ---------poonam nisha * JOIN: - Join is a query statement using which we can display information from two or more tables simultaneously. For extracting information from more than one table there must be an attribute common to all the tables. QUERY 53: Display employee name, dept name & salary for all the employees. SQL> select ename,dname,salary from emp,dept where emp.deptno=dept.deptno; ENAME DNAME SALARY ---------- -------------- --------dinesh ACCOUNTING 50000 sohail RESEARCH 45000 sumit SALES 50000 suhang RESEARCH 60000 rohit SALES 45000 rahul SALES 56000 vijay OPERATIONS 34000 7 rows selected. QUERY 54: Display employee no,employee name & dept name for all the employees having salary more than 40000. SQL> select ecode,ename,dname from emp,dept where emp.deptno=dept.deptno and salary>40000; ECODE ENAME DNAME ------ ---------- --------------

e101 e102 e103 e104 e105 e106

dinesh sohail sumit suhang rohit rahul

ACCOUNTING RESEARCH SALES RESEARCH SALES SALES

6 rows selected. QUERY 55: Display deptno,deptname,empno & location for all the dept having deptname production. SQL> select dept.deptno,dname,ecode,loc from emp,dept where emp.deptno=dept.deptno and dname='production'; no rows selected * EQUI JOIN: - The join in which columns of two tables are compared for equality are called equi join. QUERY 56: Display empno,ename,deptname & location for all the employees. SQL> select ecode,ename,dname,loc from emp,dept where emp.deptno=dept.deptno; ECODE ENAME DNAME LOC ------ ---------- -------------- ------------e101 dinesh ACCOUNTING NEW YORK e102 sohail RESEARCH DALLAS e103 sumit SALES CHICAGO e104 suhang RESEARCH DALLAS e105 rohit SALES CHICAGO e106 rahul SALES CHICAGO e107 vijay OPERATIONS BOSTON 7 rows selected. * NON-EQUI JOIN: - The join in which columns of two tables are compared with any relational operator other than equal is called non-equi join. QUERY 57: Display ename,salary & grade for all the employees having salary is in between losal to hisal. SQL> select ename,salary,grade from emp,salgrade where salary between losal and hisal; ENAME SALARY GRADE ---------- --------- --------dinesh 50000 1 sohail 45000 1

sumit suhang rohit rahul vijay dinesh sohail sumit suhang rohit rahul dinesh sohail sumit rohit vijay dinesh sohail sumit

50000 60000 45000 56000 34000 50000 45000 50000 60000 45000 56000 50000 45000 50000 45000 34000 50000 45000 50000

1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 4 4 4 GRADE

ENAME SALARY ---------- --------- --------rohit 45000 4 dinesh 50000 5 sohail 45000 5 sumit 50000 5 suhang 60000 5 rohit 45000 5 rahul 56000 5 vijay 34000 5 29 rows selected.

* SELF JOIN: - When a table is joined to itself then it is called self join. For creating a self join 2 copies of same table must be opened in the memory. For opening two copies of same table we need to specify alias name in the from clause. QUERY 58: Display employee name and manager name for all the emp. SQL> select e1.ename as empname,e2.ename as mgrname from emp e1,emp e2 where e1.ecode=e2.ecode; EMPNAME MGRNAME ---------- ---------dinesh dinesh sohail sohail sumit sumit

suhang suhang rohit rohit rahul rahul vijay vijay 7 rows selected. * OUTER JOIN: - The join that display all the matching values of both tables & rows of one table having no matching values in another table is called outer join. THE OUTER JOIN IS REPRESENTED BY (+) SYMBOL. QUERY 59: Display ename,salary & dname for all the employees.Dept name for all the dept should be displayed whether they have any employees or not. SQL> select ename,salary,dname from emp,dept where emp.deptno(+)=dept.deptno; ENAME SALARY DNAME ---------- --------- -------------dinesh 50000 ACCOUNTING sohail 45000 RESEARCH suhang 60000 RESEARCH sumit 50000 SALES rohit 45000 SALES rahul 56000 SALES vijay 34000 OPERATIONS 7 rows selected.