SQL
SQL
SQL is tied very closely with the relational model. It is a simple and powerful language used to create, manipulate and retrieve data and structures in the databases. SQL command are divided into three categories :1) DDL 2) DML 3) DCL 1) DDL :- DDL means Data Definition Language .These commands are responsible for creating, modifying or dropping database structure. DDL commands are :I) Create Table II) Alter Table III) Drop Table IV) Rename 2) DML :-DML stands for Data Manipulation Language. These statements retrieve, modify, insert or delete data into/from the database. DML commands are :I) Select II) Insert III) Update IV) Delete 3)DCL :- DCL stands for Data Control Language. DCL contains all the commands that are used to control the database. These commands are :I) Grant II) Revoke Create a Table The CREATE TABLE command is used to crate a table. In that table we we can insert our transactions. At the time of creation of table we have to specify the name of the table and there datatype and size. SQL> CREATE TABLE MINKU (ID NUMBER(4), NAME VARCHAR2(15), DOB DATE, ADDRESS VARCHAR2(50), PHONENO NUMBER(10) ) Table Created 1. Table name: This represents the name of the table to be created. 2. Column_name1: Through column_name represent the names of the columns to be created. 3. Data_type: Each column are of which datatype that will be shown here. 4. Column constraints: The constraints are the rules that must be satisfied for the value in the column to be valid. 5. Table constraints: The table level constrints are on multiple column rather than the single column. Use of Integrity constraints in create table Mainly the use of integrity constraints in oracle is to prevent the invalid data entry into the table of the database.The different kinds of integrity constraints are as follows: 1. NOT NULL Integrity constraints 2. Primary Key constraints 3. Unique Key constraints 4. Check Integrity constraints 1
A constraints can contain a single column or a group of columns in a table. The two ways to specify the constraints are as follows: A. Column Level Constraints B. Table Level Constraints Syntax of column level constraints Column[CONSTRAINTS constraints_name] constraints_type; Syntax of table level constraints Column, [CONSTRAINTS constraints_name] constraints_type (column,..) 1. NOT NULL INTEGRITY CONSTRAINTS Here in Oracle NOT NULL constraints ensure that a column of a table contain no null values. Note this this constraints can only apply at the column level and not at table level. SQL> create table student(id number(10) unique not null,name varchar2(10) not null); Table created. 2) Primary key :SQL> create table MINKU(id number(10) primary key,name varchar2(20)); Table created. SQL> insert into MINKU values(101,'Aman'); 1 row created. SQL> insert into MINKU values(101,'Mohan'); insert into MINKU values(101,'Mohan') * ERROR at line 1: ORA-00001: unique constraint (SCOTT.SYS_C00592) violated 3) Foreign key :SQL> create table fee(id number(10) references std2,fee number(5)); Table created. SQL> insert into fee values(101,2500); 1 row created. SQL> insert into fee values(102,4000); insert into fee values(102,4000) * ERROR at line 1: ORA-02291: integrity constraint (SCOTT.SYS_C00593) violated - parent key not found INSERT COMMAND INSERT command is used to insert a row into an existing table. INSERT command adds data row by row into existing Oracle table. Syntax :INSERT into <Table_name> values(val1,val2,val3,.); Eg. SQL> insert into minku values(101,'MINKU','MCA 2nd'); 1 row created. Another Example SQL> insert into minku values(&rno,&name,&class); Enter value for rno: 102 Enter value for name: 'Ram' Enter value for class: 'BCA 1st' old 1: insert into std values(&rno,&name,&class) new 1: insert into std values(102,'RAMAN','BCA 1st') 2
1 row created. SELECT COMMAND SQL> select * from emp; 14 rows selected. SQL>SELECT ENAME FROM EMP; SQL> SELECT ENAME FROM EMP WHERE SAL>1300 ; UPDATE COMMAND UPDATE command is used to change the values of existing tables. The update process can be done for a single row or a set of rows. Syntax :UPDATE <Table_name> SET <Column1>=val1,<Column2>=val2,. WHERE <Column_name>=<expression>; Eg :SQL> update std set name='MINKU DOOMRA',class='MCA 4th' where rno=101; 1 row updated. SQL> select * from std; RNO NAME CLASS --------- -------------------- ---------101 MINKU DOOMRA MCA 4th 102 RAMAN LAL BCA 1st DELETE COMMAND DELETE command is used to delete the rows from a table. The entire rows are deleted from table without WHERE clause .We can delete one or more specific rows using WHERE caluse. Syntax :DELETE from <table_name>; Eg :SQL> delete from MINKU; 10 rows deleted. SQL> delete from MINKU where id=101; 1 row deleted. ALTER TABLE This command is used to modify the structure of a table.we can add some new columns ,modify existing columns. To Add New Column SQL> alter table MINKU add(age number(3)); Table altered. To Modify SQL> alter table MINKU modify(class varchar2(20)); Table altered. To Drop Constraint SQL> alter table MINKU drop primary key; Table altered. RENAME COMMAND This command is used to change the name of a table. Syntax :RENAME <old_table_name> TO <new_table_name>; Eg :SQL> rename MINKU to MINKU1; 3
Table renamed. DROP TABLE This command is used to remove the definition of Oracle table. We can destroy the table and all the data stored in it by using drop table command. Syntax:DROP TABLE <table_name>; Eg :SQL> drop table MINKU1; Table dropped. OPERATORS Operator are special symbols that are used to manipulate data. The data items on which the operators acts upon are called operands. Some Oracle operators are shown below :1) IN operator :SQL> select * from emp where deptno in(10,20,30); 2) LIKE operator :SQL> select ename from emp where ename like 'M%'; ENAME ---------MARTIN MILLER 3) BETWEEN Operator :SOL> select ename from emp where sal between 2000 and 3000; ENAME ---------JONES BLAKE CLARK SCOTT FORD 4) IS NULL :SQL> select empno from emp where comm is null; EMPNO --------7369 7566 7698 7782 7788 7839 7876 7900 7902 7934 10 rows selected. 4
ORDER BY CLAUSE The ORDER BY clause is used to finally order the result in ascending order or descending order. 1) Ascending order SQL> select ename,sal from emp order by sal; ENAME SAL ---------- --------SMITH 800 JAMES 950 ADAMS 1100 WARD 1250 MARTIN 1250 MILLER 1300 TURNER 1500 ALLEN 1600 CLARK 2450 BLAKE 2850 JONES 2975 SCOTT 3000 FORD 3000 KING 5000 14 rows selected. SQL> 2) Descending order SQL>select ename,sal from emp order by sal desc; ENAME SAL ---------- --------KING 5000 SCOTT 3000 FORD 3000 JONES 2975 BLAKE 2850 CLARK 2450 ALLEN 1600 TURNER 1500 MILLER 1300 WARD 1250 MARTIN 1250 ADAMS 1100 JAMES 950 SMITH 800 14 rows selected.
GROUP BY CLAUSE This clause helps us in grouping the result of query. This clause is used to divide the rows in a table into similar statement group. SQL> select job from emp group by job having job!='CLERK'; JOB --------ANALYST MANAGER PRESIDENT SALESMAN SQL> Another eg. SQL> select job,max(sal) from emp group by job; JOB MAX(SAL) ----------------ANALYST 3000 CLERK 1300 MANAGER 2975 PRESIDENT 5000 SALESMAN 1600
GROUP FUNCTIONS These functions are used to summarized the result. These functions operate on set of rows to give one result. Group Functions are :1) Count() :SQL> select deptno,count(empno) from emp group by deptno; DEPTNO COUNT(EMPNO) 10 20 30 SQL> 2) Sum() :SOL>select deptno,sum(sal) from emp group by deptno; DEPTNO SUM(SAL) --------- --------6 3 5 6
10 20 30 SQL>
3) Max() :SQL>select deptno,max(sal) from emp group by deptno; DEPTNO MAX(SAL) --------- --------10 5000 20 3000 30 2850 SQL> 4) Min() :SQL>select deptno,min(sal) from emp group by deptno; DEPTNO MIN(SAL) --------- --------10 1300 20 800 30 950 SQL> 5) Avg() :SQL>select deptno,avg(sal) from emp group by deptno; DEPTNO AVG(SAL) --------- --------10 2916.6667 20 2175 30 1566.6667
JOINS Joins are used to query data from more than one tables. By using joins rows in one table can be joined rows in another table according to the values existing in corresponding columns. The connection between tables is established through where clause. Types of joins :7
1) Equi join :- Two tables are joined together using euality of values in one or more columns. SOL>select emp.ename,emp.empno,dept.loc from emp,dept where emp.deptno=dept.deptno; ENAME EMPNO LOC ---------- --------------------SMITH 7369 DALLAS ALLEN 7499 CHICAGO WARD 7521 CHICAGO JONES 7566 DALLAS MARTIN 7654 CHICAGO BLAKE 7698 CHICAGO CLARK 7782 NEW YORK SCOTT 7788 DALLAS KING 7839 NEW YORK TURNER 7844 CHICAGO ADAMS 7876 DALLAS JAMES 7900 CHICAGO FORD 7902 DALLAS MILLER 7934 NEW YORK 14 rows selected. 2) Non Equi Join :- A join is said to be non equi join where no column in one table corresponding directly to a column in another table . Eg :SQL> select emp.ename,salgrade.grade from emp,salgrade where sal between losal and hisal; ENAME GRADE ---------- --------SMITH 1 ADAMS 1 JAMES 1 WARD 2 MARTIN 2 MILLER 2 ALLEN 3 TURNER 3 JONES 4 BLAKE 4 CLARK 4 SCOTT 4 FORD 4 KING 5 14 rows selected. SQL> 8
3) Outer Join :- This type of join is used to see those rows that do not complete the join condition . such rows are forcefully selected by using the outer join operator (+). Eg :SQL> select emp.empno,emp.ename,dept.deptno from emp,dept where emp.deptno=dept.deptno(+); EMPNO ENAME DEPTNO --------- ---------- --------7369 SMITH 20 7499 ALLEN 30 7521 WARD 30 7566 JONES 20 7654 MARTIN 30 7698 BLAKE 30 7782 CLARK 10 7788 SCOTT 20 7839 KING 10 7844 TURNER 30 7876 ADAMS 20 7900 JAMES 30 7902 FORD 20 7934 MILLER 10 14 rows selected. SQL>
4) Self Join :- When we want to join a table to itself that is each row of the table is combined with it self and with every other row of the table. Self join can be seen as a join of 2 copies of same table. Eg :SQL> select e.ename,d.ename mgr from emp e,emp d where e.mgr=d.empno; ENAME MGR ---------- ---------SCOTT JONES FORD JONES ALLEN BLAKE WARD BLAKE JAMES BLAKE TURNER BLAKE MARTIN BLAKE MILLER CLARK ADAMS SCOTT JONES KING CLARK KING 9
BLAKE SMITH
KING FORD
13 rows selected. SQL> 5) Cartesian Join :- when no join condition is specified then join is called Cartesian join. While joining two tables each row of one table matched every row of other table. Eg :SQL> select emp.ename,worker.name from emp,worker; ENAME NAME ---------- -------------------SMITH Aman ALLEN Aman WARD Aman JONES Aman MARTIN Aman BLAKE Aman CLARK Aman SCOTT Aman KING Aman TURNER Aman ADAMS Aman JAMES Aman FORD Aman MILLER Aman SMITH Sham ALLEN Sham WARD Sham JONES Sham MARTIN Sham BLAKE Sham CLARK Sham
ENAME NAME ---------- -------------------SCOTT Sham KING Sham TURNER Sham ADAMS Sham JAMES Sham FORD Sham MILLER Sham 10
28 rows selected.
SET OPERATORS SET OPERATORS are used to combine the results of more than one SQL queries into a single result. The set operators are :1) Union :- This operator merged the output of two or more queries into a single set of rows and columns , here all the queries are executed independently and their output is merged. Eg :SQL> select ename,empno from emp where deptno=10 2 Union 3 select ename,empno from emp where deptno=20 order by 1; ENAME EMPNO ---------- --------ADAMS 7876 CLARK 7782 FORD 7902 JONES 7566 KING 7839 MILLER 7934 SCOTT 7788 SMITH 7369 8 rows selected. 2) Intersect :- This operator returns the rows that are common between two set of rows. Eg :SQL> select job from emp where deptno=20 2 intersect 3 select job from emp where deptno=30; JOB 11
3) Minus :- This operator return the rows unique to the first query. Eg :SQL> select ename from emp 2 minus 3 select dname from dept; ENAME -------------ADAMS ALLEN BLAKE CLARK FORD JAMES JONES KING MARTIN MILLER SCOTT SMITH TURNER WARD 14 rows selected.
NESTED QUEURIES SQL has the ability to nest query with in another. The result of inner query substituted in the condition of other query. We ensure that the sub query returns a single statement that is nested with in another statement and which returns intermediate results. Eg:SQL> select ename,sal from emp where sal>(select avg(sal) from emp where deptno=20); ENAME ---------SAL --------12
JONES BLAKE CLARK SCOTT KING FORD 6 rows selected. SQL> Another egs.
SQL>select ename,job,sal from emp where sal<(select sal from emp where empno=7698); ENAME JOB SAL ---------- ----------------SMITH CLERK 800 ALLEN SALESMAN 1600 WARD SALESMAN 1250 MARTIN SALESMAN 1250 CLARK MANAGER 2450 TURNER SALESMAN 1500 ADAMS CLERK 1100 JAMES CLERK 950 MILLER CLERK 1300 9 rows selected. SQL> SQL> select ename from emp where deptno=(select deptno from emp where ename='SCOTT'); ENAME ---------SMITH JONES SCOTT ADAMS FORD
13