3) ansi sql (american national standard institute ... · sql server 2005 2 12) t-sql is one of the...

90
SQL Server 2005 1 1) SQL server is Database package. Package has limitations. 2) SQL( Structured query language) is a language through which we can communicate with database. 3) ANSI SQL (American national standard institute Structured query language) is a basic standards for RDBMS( Relational database management system). 4) If you want to develop RDBMS , it has to follow ANSI SQL standards. 5) Oracle consists of SQL-Plus and SQL-Plus consists of SQL and SQL and PL/SQL. 6) Microsoft’s SQL server has T-SQL (transact sql) . T-SQL consists of SQL and batches ( like PL/SQL in oracle). 7) T-SQL is a Database language through which interact with SQL Server. 8) Batches means some programs like functions, procedures and triggers. 9) SQL implemented using E.F. codd rules in 1969. 10)SEQUEL (structured English query language) is a original name of sql server. 10) SQL server is a popular RDBMS package implemented by Microsoft corporation. 11) SQL is a D.B. language by which we can communicate with specified D.B. and this is the standard SQL for all RDBMS packages.

Upload: others

Post on 21-Sep-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 1

1) SQL server is Database package. Package has limitations. 2) SQL( Structured query language) is a language through which we can

communicate with database.

3) ANSI SQL (American national standard institute Structured query language) is a basic standards for RDBMS( Relational database management system).

4) If you want to develop RDBMS , it has to follow ANSI SQL

standards.

5) Oracle consists of SQL-Plus and SQL-Plus consists of SQL and SQL and PL/SQL.

6) Microsoft’s SQL server has T-SQL (transact sql) . T-SQL consists of

SQL and batches ( like PL/SQL in oracle).

7) T-SQL is a Database language through which interact with SQL Server.

8) Batches means some programs like functions, procedures and triggers.

9) SQL implemented using E.F. codd rules in 1969.

10)SEQUEL (structured English query language) is a original name of sql server.

10) SQL server is a popular RDBMS package implemented by

Microsoft corporation. 11) SQL is a D.B. language by which we can communicate with

specified D.B. and this is the standard SQL for all RDBMS packages.

Page 2: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 2

12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases.

13) DBMS is a collection of data and set of software programs for

storing , managing, and accessing the data.

14) RDBMS is a DBMS with strong relationship concept , that relation is called parent and child relation.

15) There are three types of DBMS models

i) HDBMS (ex:- IMS) ii) NDBMS (ex:- dbase, foxpro) iii) RDBMS (ex:- oracle, sql server, DBL)

Features of SQL Server 2005:- 1) .NET integration :- It is one of the main feature in 2005 version. Here

we can develop procedures and functions in .NET languages (VB.NET,C#) and we can call them directly from SQL server by enabling SQL server.

2) Structured exception handling

3) DDL triggers

4) New sample databases ( Adventure works, Adventureworks DW)

5) New objects [SYNONYMS]

Page 3: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 3

6) New data types [XML,NVARCHAR(MAX),VARCHAR(MAX),VARBINARY(MAX),VARBINARY (MAX)]

7) XML integration

8) Ranking functions

9) New tools

i) SQL Server management studio ii) Integration services iii) Business intelligence studio (BI studio) Version Version number 2005 9.0 2000 8.0

10) Sql server versions:- SQL server 4.0/4.2 (Similar to MS-access) SQL server 6.0/6.5 (for only small size applications with LAN support) SQL server 7.0 ( for small and medium size applications with LAN and WAN support) SQL server 8.0 ( For all type of applications) SQL server 9.0 ( .NET integration)

Page 4: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 4

Steps to open SQL server management studio:- SQL server management studio is a powerful tool in 2005 for creating and managing database objects. Start>Programs>Microsoft SQLServer2005> SQL server management studio For windows authentication for sql server , username and password is not required. For SQL server 2000 i) Query analyzer: - for queries ii) Enterprise :- for managing database objects

The above two is included in one SQLServer studio management in 2005.

Data maintenance:- i) File management:- .txt /.dat / .xml ii) Database management system:- data maintained in the

form of table

Limitations of file management system:- iii) data redundancy :-

When number of users want or need to access same file you should maintain different copies

. iv) No concurrency control:- simultaneous transactions are not possible.

v) Data inconsistency:-

Page 5: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 5

When user modifies the master copy those are not reflected in the details copy. Manually delete and update each and every copy. vi) Lack of security:-

There is no procedure for hiding data from unauthorized users.

vii) No data integrity:- There is no type checking. To overcome all these drawbacks DBMS is introduced.

DBMS:- It consisting set of inter related data items and as well as set of software programs for accessing those data. Limitations or drawbacks:-

i) It supports to store limited data only ii) It will not support any E.F.Codd rules iii) It is only for single user purpose iv) It is only suitable for small size organizations v) It supports less number of relations

To overcome above limitations data model / database model is introduced. Database model / data model:-

i) HDBMS (Hierarchial DBMS):-

Page 6: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 6

Here data can be arranged in the heirarchial format ie we are creating relations between data items ie parent and child relationship.

A---- Parent dataitem B,C------- This will act as parent or child data item depending on the situation D,E,F,G-------- purely child data item.These are also called as leaf nodes. Above diagram may be B-tree or B+ tree .

A

C

E F G

B

D

Page 7: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 7

Characteristics of HDBMS:- It is a simple data structure. Here data nodes or data items arranged in the hierarchical format with parent and child relation. Limitations of HDBMS:- i) Data redundancy ii) Forward accessing mechanism only Ex:- IMS (Information management system) NDBMS:- i) Here data items are arranged in network format. Ie arbitrary

format. ii) Using NDBMS we can provide bidirectional accessing

mechanism iii) It avoid data redundancy Ex:- Foxpro, Dbase Limitations:- i) Data rearrangement is complicated task ie insertions and

deletions are very complicated ii) There is no query facility

Page 8: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 8

RDBMS (Relational database management system):- It is one type of database model purely developed based on relationship concept. The relation may be a i) one to one relation ii) one to many relation iii) many to one relation iv) many to many relation Ex:- SQL Server, Oracle RDBMS packages Vendor SQL server Microsoft Oracle Oracle corporation DB2 IBM Database engine boreland Sybase sql Sybase corporation Informix Informix software Mysql T.C.X. INGRES computer associates Difference between DBMS and RDBMS:- i) DBMS supports less number of relations only, where as

RDBMS was developed based on the relationship concept ii) DBMS is only for single user purpose where RDBMS is

multiuser purpose iii) In the DBMS data can be stored in the files format where asd in

RDBMS data is stored in files format iv) Data processing in DBMS is slow and it requires low software

and hardware configuration, where as in RDBMS high data processing and it requires high configuration

Page 9: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 9

v) Internally DBMS use 3GL (3 rd generation language) where as RDBMS use 4 GL.

vi) DBMS will not support EFCODD rules . RDBMS will support EFCODD rules. Any RDBMS should support at least 6 CODD rules out of 12 CODD rules. Examples for DBMS:- Dbase, Foxpro Examples for RDBMS:- SQL server , Oracle

E-R models (Entity – relationship models): Entity:- means object Object:- set of properties and behaviours D.B. object � means table E-R model will displays relation between tables or entities. Generally this E-R model having three components. i) Entity sets symbol:-

iii) Attribute sets:-

Symbol:-

Page 10: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 10

iv) Relationship sets

Symbol:- There are two types of attributes i) Simple attribute ii) Composite attribute

Customer has two attributes cname , caddress and cname has attributes first name , middle name, last name and caddress has pin, city street attributes. Ie Cname and caddress are composite attributes First name, middle name, last name, pin, city, street are simple attributes.

Page 11: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 11

Fig. one to one relation

customer Borrrower

loan

cname SSN city

Loan number Amt

Page 12: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 12

Fig. one to many relation

customer Borrrower

loan

cname SSN city

Loan number Amt

Page 13: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 13

Borrrow Fig. many to one relation

customer borrower

loan

cname SSN city

Loan number Amt

Page 14: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 14

Borrrow Fig. many to many relation

customer borrower

loan

cname SSN city

Loan number Amt

Page 15: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 15

SQL SERVER:- It is one of the most popular RDBMS package. It was developed and marketed by the Microsoft corporation and it is most important part of the .NET technologies. SQL Server exclusively runs under window platform such as win2000,2003,XP,NT It is closely associated with .NET and windows platform. Note:- Basically it is the product of Sybase corporation and it was licensed by the Microsoft in the year of 1988. Features of SQL Server:-

i) It provides user friendly interface ii) It supports data integrity at various levels iii) It supports backup and recovery concepts iv) It provides security and authentication control v) It provides some data ware housing features.

Page 16: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 16

Database:- Database is a container which contains database object. Here database is classified into three categories. 1) System databases:- For system purpose.

Master, model, MS DB, TempDB system databases. 2) Sample databases:-

These are for sample tables for learning purpose Adventure works, adventureworks DW (DW means dataware housing) sample databases

3) User databases:- User created database. It is better to create database in user data bases. Datatypes:-

i) Numerical datatypes:- Data type memory size data range tiny 1B 0-255 smallint 2B -32768 to 32767 int 4B -2147483648 to 2147483647 bigint 8B -2 pow 63 to 2 pow 63 -1

decimal [p,s] 5-17B -------------------- p�precision – max. digits allowed

Page 17: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 17

s� scale --- decimal digits after decimal point numeric[p,s] 5-17B -------------------- float 8B real 4B bit 1 bit

ii) String datatype:-

Data type memory size data range char[n] 1B 8000 characters varchar[n] 1B 8000 characters nchar[n] 2B 4000 characters nvarchar[n] 2B 4000 characters text 1B upto 2GB ntext 2B 2 pow 31 -1 varchar(max) 1B 2 pow 31 -1 nvarchar(max) 2B 2 pow 31 – 1 nchar, nvarchar � are Unicode or universal datatype

iii) currency datatypes:- money 8 Bytes same as bigint smallmoney 4 Bytes same as int

iv) Date datatypes and time datatypes:-

Datetime 8B Jan-1-1753 to dec-31-9999 Smalldatetime 4B jan-1-1900 to jun-6-2079

v) miscellanious datatypes:- CURSOR , XML , TIMESTAMP etc.

Page 18: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 18

Sublanguages in Transact Structured query language:- 1) DDL (Data definition language)

CREATE, ALTER , DROP 2) DML (Data manipulation language)

INSERT, UPDATE , DELETE 3) DCL (Data control language)

GRANT , REVOKE 4) TCL (Transaction control language) ROLLBACK, COMMIT 5) DATA QUERY LANGUAGE:-

SELECT statement As per ANSI , DDL, DML, DCL are sublanguages of Transact SQL. SELECT statement comes under Data Query language. DATA DEFINITION LANGUAGE: CREATE , ALTER, DROP statements will come under data definition language.

i) CREATE :- This is for creating database and database objects

Page 19: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 19

Syntax for creating database:- CREATE DATABASE database name Ex:- CREATE DATABASE STORAGE_SPACE This will create storage_space database. Syntax for creating table:- CREATE TABLE table name (column1 datatype, column2 Datatype, ……………..)

Ex:- CREATE TABLE employee( empno int, ename varchar(20),salary money)

ii) ALTER:- This is for modifying database and data base objects.

Syntax for adding columns to the specified table:- ALTER TABLE table name ADD col1 data type, col2 data type etc. Ex:- ALTER TABLE employee ADD doj datetime, addr varchar(50), Photo image) Syntax to alter a column data type:- ALTER TABLE table name ALTER COLUMN column name new data type Ex:- ALTER TABLE EMPLOYEE ALTER COLUMN ename VARCHAR(50) Syntax for dropping column:-

Page 20: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 20

ALTER TABLE table name DROP COLUMN col1, col2 Ex:- ALTER TABLE employee DROP COLUMN photo , doj DROP:- This is for dropping databases and database objects. This statement works only for user and sample databases. Syntax to drop database:- DROP DATABASE SQL-2-DAYS , SEP_DB If any problems are there goto “master” database and execute query. You can not drop system databases.

List of database objects:-

viii) Tables ix) Views x) Indexes xi) Synonyms xii) Stored procedures xiii) Function xiv) Triggers xv) Constraints xvi) Defaults xvii) Rules

• One sequel server can manage 32,767 databases. • One database can manage 2 billion of database objects. • One table can have maximum of 1024 columns • Number of rows are unlimited. It depends on database size. • In each database should contain atleast two files. One is primary file

(log file) and data file with extension .mdf (master data file). • Master data file is used for storing data

Page 21: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 21

• Log file is used for storing transaction information • F5 key is used for displaying of object explorer. Using this you can

create database, views etc. DATA MANIPULATION LANGUAGE:- i) INSERT:- This language supports INSERT, UPDATE , DELETE statements. Syntax for inserting values :- INSERT [INTO] TABLE / VIEW / SYNONYM (COL1 , COL2,….) VALUES(VAL1,VAL2,………….) Ex:- INSERT INTO EMP VALUES(11,’RAJ’,5000) INSERT INTO EMP(EMPNO,ENAME,SAL) VALUES(33,’amar’,9000) NULL value:- NULL is garbage value or unknown value when user not supplying sufficient values to the table. In that case , system will assign null values. We can not use null values for any calculation. Ie NULL +100 =NULL 1000+ NULL=NULL

Inserting null values explicitly:- INSERT INTO EMP VALUES(NULL,NULL,NULL) Some shortcut keys:-

Page 22: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 22

F5------- Query execution key Ctrl + E ------------ Query execution Ctrl + R ---------To show / hide result window II) UPDATE:- This statement is for modifying data based on the given conditions. Syntax for update statement:- UPDATE TABLE / VIEW / SYNONYM SET COL1=VAL1 , COL2=VAL2,……….. WHERE EXPRESSION Ex:- UPDATE emp SET sal=sal+1000 Increases employee salaries by 1000 rupees Case Expression:- This is for validating multiple conditions. Syntax:- case when cond. Then result . . . Else Result n End Ex:- Calculating bonus for employee table:- Step1:- Add bonus column to emp table Step2:- Update bonus column based on the given condition

Page 23: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 23

Salary bonus <=5000 1000 >5000 and <=10000 2000 >10000 5000 Step1:-

ALTER TABLE emp ADD bonus money After adding column bonus , values in that column will be null values. Ie Empno ename sal money 11 raj 400 NULL 12 ram 900 NULL Step2:- UPDATE emp set bonus=CASE WHEN sal<=5000 THEN 1000 WHEN sal>5000 AND sal<=1000 WHEN sal>1000 THEN 5000 END

iii) DELETE:- This statement is for deleting rows from the table. Syntax:- DELETE [FROM] TABLE / VIEW / SYNONYM WHERE EXPRESSION Ex:- DELETE from emp DELETE emp DELETE from emp where sal>5000

Page 24: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 24

iii) DATA QUERY LANGUAGE:-

It supports SELECT statement. It is one basic statement for selecting data from the tables by specifying table name , view, function, synonym. Syntax:- Select * | columns from table / view /function/ synonym Select columns----, group functions from <tablename | viewname> Where <condition> group by <exp.> having <exp.> order by <columnlist> [asc|desc] Ex:-

1) Write a query to select all rows and columns from emp table.

Select *from emp 2) write a query to select employee names and their

salaries? Select ename, sal from emp 3) Write a query to select first record fro emp table? Select top 1 * from emp Select top 1 eno, ename from emp

Page 25: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 25

Select top 2 ename from emp Some queries:- Ex:- INSERT INTO EMP SELECT 67,’secno’ 5000 SELECT 67,’raj’,5000 ----� this statement is like print statement SELECT 10+20 SELECT ‘logic’

Where clause in select statement:- This is for validating conditions. It is associated with select, update, delete statements. SELECT QUERY WHERE CONDITION EX:- Write a query to select rows from emp table whose salary greater than 5000? SELECT *FROM EMP WHERE salary >5000 Ex:- Write a query for displaying total salary based on deptno and total salary should be greater than 20,000. Select deptno, sum(salary) from emp group by deptno having Sum(salary) > 20000

Page 26: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 26

IDENTITY:- It provides uniqueness property to the specified column. It will generate the values based on the given initial and increment values. User need not to supply values for this column. Limitations:- Only identity column allowed per table. Only few datatypes support Identity property. Ie tinyint, smallint, int, bigint, decimal (all integer and decimal datatype) Ex:- CREATE TABLE product( pid int identity(100,1), Pname varchar(20), Qty int) INSERT INTO product values(‘soap’,700) • Identity(100,-1) means start from 100 to 99 , 99 to 98 etc. • Identity(100,1) means start form 1 to 2 , 2 to 3 etc. upto 100.

COMPUTED OR VIRTUAL COLUMNS:- Computed columns are also called virtual columns which are computed automatically based on given formula. Ex:- CREATE TABLE STUDENT( rollno int, sname varchar(20), m int, p int,

Page 27: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 27

C int , total_marks as m+p+c) In above query, total_marks column is a virtual or computed column. OPERATORS:-

i) arithmetic operators:- + - * % / =

ii) relational operators < > <= >= != or <> !< !> =

iii) logical operators AND, OR , NOT

ALIAS NAMES:- This is the alternative name for the database objects and its attributes. This is for hiding original names and also providing names for the derived or calculated values. Ex:- select empno as employee_no , ename as employee_name from emp TRUNCATE STATEMENT:- It is also similar to delete statement. To delete rows from the table and it makes execution faster compare to the delete statement because it can delete data in page by page format, where as delete can Delete row by row format. It does not support where clause . That is why it removes all records from table.

Page 28: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 28

Ex:- TRUNCATE TABLE emp Data stored in page format. P1

P2 P1 , P2 pages. We can rollback truncate. We can rollback DDL commands.

DATA INTEGRITY:- It is a data validity or data checking while insertion. Here using data integrity concept, we can avoid unwanted or invalid data. Majorly it can be achieved in three ways

vi) data types vii) constraints viii) triggers

Page 29: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 29

CONSTRAINTS:- Constraint is the rule or condition can be defined on the specified column inorder to prevent invalid data. OR It is a mechanism. It is automatically activated when user Performs DML operations on the table or view. This concept is used for achieving data integrity. We can enforce data integrity by defining constraints. Constraints are classified into three categories:-

2) Domain integrity constraints ( NOT NULL , CHECK) 3) Entity integrity constraints (UNIQUE, PRIMARY KEY)

4) Referential integrity constraints [FOREIGN KEY]

5) DEFAULT is a additional constraint.

i) NOT NULL :- It does not allow null values ii) CHECK :- This is for validating user condition iii) UNIQUE :- It does not allow duplicate values. This unique property

is applicable to null values also. No two null values are allowed in unique constraint columns.

Page 30: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 30

iv) PRIMARY KEY:- It does not allow duplicate and null values. v) FOREIGN KEY:- This is for establishing relation between two tables. RDBMS use relational concept using foreign key

i) should maintain at least one common column in the given tables.

ii) Foreign key allows duplicate and null values. iii) Foreign key allowed only parent table key

column values and null values iv) Foreign key associated with either primary

key or unique column v) Foreign key table is called child table and

associate with primary key column table called parent table

vi) If any changes are made in parent table key column, those changes are automatically affected child table foreign key column. This is possible with the help of ‘on update cascade’

vii) When user try to remove parent table records, before that system will removes all corresponding rows from that child table. This is possible with the help of ‘on delete cascade’.

viii) First insert records into parent table, then only insert records is child tables.

ix) First drop child table then only parent table can be dropped

x) Only one primary key constraint allowed for table

xi) Number of foreign keys are upto 53. Employee table:- Primary key emp table [Parent table] Foreign key

Page 31: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 31

Empno Ename Sal Dept no 11 RAJ 5000 10 22 MANI 2000 20 33 AMAR 9000 20 44 ANIL 10000 NULL Primary key Dept table (Child table) Deptno Dname loc 10 Accounts NY 20 Research Boston 30 Sales Hyd Deptno column of dept child table may be unique or primary key. Ie child table may have unique or primary key.

• Database design is complicated task, based on normal forms applying repeatedly.

• One table can have 253 foreign keys Query:- delete from dept where deptno=15 Removes records from child table and then removes parent table.

Page 32: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 32

Levels of constraints:- 1) Column level constraints:- Here constraints are defined for each and

every column separately.

CREATE TABLE tablename (col1 datatype constraint1 constraint2……, Col2 datatype constraint1 constriant2……………., ------------------------)

� Create department table based on following columns and constraints? Columns constraints deptno int primary key dname varchar(20) unique not null loc varchar(20) default ‘hyd’ CREATE TABLE dept( deptno int primary key, deptname varchar(20) unique NOT NULL, loc varchar(20) default ‘hyd) DEFAULT:- It is additional constraint to define user default values, instead of using system default value null.

Page 33: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 33

INSERT INTO dept VALUES(10,’ACC’,’NEW YORK’) INSERT INTO dept VALUES(30,’SALES’,DEFAULT) INSERT INTO dept(deptno,dname) VALUES (40 ,’HR’) Ex:- Create emp table based on the follwing columns and constraints? Column constraints empno int primary key ename varchar(20) NOT NULL sal money check( sal >=5000) deptno int foreign key references dept(deptno) CREATE TABLE emp ( empno int primary key, ename varchar(20) not null, sal money check(sal >= 5000), deptno int foreign key references dept (deptno) ON DELETE CASCADE ON UPDATE CASCADE)

Page 34: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 34

CREATE TABLE dept1 ( deptno int primary key, dname varchar(20) unique not null, loc varchar(20) default ‘hyd’) CREATE TABLE dept2 (deptno int primary key, dname varchar(20) unique not null, loc varchar(20) default ‘hyd’) CREATE TABLE emp ( empno int primary key, ename varchar(20) not null, sal money check (sal > 500) deptno int foreign key (deptno) references dept1(deptno), foreign key (deptno) references dept2 (deptno) ON DELETE CASCADE ON UPDATE CASCADE) Emp is a parent table and dept1 and dept2 are child tables. If you modify any value in parent table , then it can not be updated in dept1, dept2 .There is a error. 2) table level constraints:- here constraints are defined at end of table structure and we can create a constraint on multiple columns called composite constraints. Note:- In table level constraints syntax, we can not define not null and default constraint. CREATE TABLE tablename( col1 datatype, col2 datatype,………….

Constraint1 (columns) , constraint2 (columns)……………..)

Page 35: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 35

� Create joint accounts table based on the following constraints?

Accno int Ahn varchar(20) Balance money constraints,\ Primary key (accno,ahn) Check(salary >= 5000)

Answer:- For joint account , you can create composite primary key. Create table accounts( accno int, Ahn varchar(20), Balance money, Primary key (acno,ahn),

Check (balance >= 5000)) • Advantage of table level constraints is composite primary key can be

used. • Drawback is not null , default is not supported. Syntax for dropping constraints:- ALTER TABLE tablename DROP CONSTRAINT constraint name 1, -------------------

� Write a query to drop unique constraint from department table? ALTER TABLE dept DROP CONSTRAINT DF_DEPT_LOC_014935CB

Page 36: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 36

This (DF_DEPT_LOC_014935CB) constraint value is different in different servers.

Adding constraints to existing tables:- ALTER TABLE tablename ADD constraint1 column1, constraint2 column2,……………… Ex:- CREATE TABLE dept ( deptno int not null, dname varchar(20), loc varchar(20) )

� ALTER TABLE dept ADD PRIMARY KEY (deptno), UNIQUE (dname).

JOIN:- Join is a select query to select data from more than one table. Types:- i) inner join ii) outer join iii) cross join iv) self join

Page 37: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 37

I) inner join:- inner join will select only matched records from the given table based on the given equality condition. That is why , it is also called equi join. Select [table1] col1, [table2] col2,…………….. from table1 INNER JOIN table2 on table1.col1=table2.col INNER JOIN table3 ON table2.col=table3.col

Employee table:- Primary key emp table [Parent table] Foreign key Empno Ename Sal Dept no 11 RAJ 5000 10 22 MANI 2000 20 33 AMAR 9000 20 44 ANIL 10000 NULL Primary key Dept table (Child table) deptno Dname loc 10 Accounts NY 20 Research Boston 30 Sales Hyd Write a query to select emp name, sal, dname, loc columns? SELECT emp.ename , emp.sal FROM emp inner join dept ON emp.deptno=dept.deptno

Page 38: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 38

o/p:- RAJ 5000 ACCOUNTS NY MANI 7000 RESEARCH BOSTON AMAR 9000 RESEARCH BOSTON

Note:- Without specifying INNER JOIN keywords default is inner join. SELECT emp.ename, dep.dname from dept, emp where Emp.deptno=dept.deptno II) OUTER JOIN:- It is simply extension version of inner join. It is 3 types

i) Left outer join:- This join will select matched records from the given tables and selects unmatched records from left most table.

Ansi syntax:- SELECT [TABLE1.] Col1, [table2.]col2, ….. …….. from table1 left outer join table2 ON table1.col1 =table2.col2 T-sql syntax:- Select column list from table1 left outer join table2 on table1.column=table2.column

Page 39: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 39

Ex:- SELECT emp.ename, emp.sal, dept.dname, dept.loc from emp left outer join dept on emp.deptno=dept.deptno RAJ 5000 ACCOUNTS NY MANI 7000 RESEARCH BOSTON AMAR 9000 RESEARCH BOSTON

KUMAR 15000 NULL NULL In above first three are matched records and last one is unmatched records.

ii) right outer join:- itdisplays matched records from the given set of tables and unmatched records from the right side tables. Ansi syntax:- Select columnlist from table1 right outer join table2 on table1.column=table2.column

T-sql syntax:- Select columnlist from table1, table2…….. where Table1.column =* table2.column Ex:- Select empno, ename,dname, loc from emp,dept where emp.deptno=*dept.deptno

Page 40: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 40

iii) Full outer join:- It displays matched records from given set of tables and as well as unmatched records from the given set of tables. Ansi syntax:- Select columnlist from table1 full outer join table2 on table1.column=table2.column Ex:- Select empno,ename,dname,loc from emp full outer join dept on emp.deptno=dept.deptno

iii) Self join:- Joining a table by itself ie self join. It is used for retrieving data from single column and placed in the different columns. Emp table

Empno ename mgr 101 A null 102 B 101 103 C 102 104 D 101 105 E 102 Subordinates superiors --------------- ------------ B A C B D A E B

Page 41: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 41

Syntax:- Select e1.ename as subordinate, e2.ename as superior from emp e1, emp e2 where e1.mgr=e2.empno E1 table Eno ename mgr 101 A null

102 B 101 103 C 102 104 D 101 105 E 102

E2 table Eno ename mgr 101 A null

102 B 101 103 C 102 104 D 101 105 E 102 Emp_log table ___________________________________________ Empno log_time status 101 8 AM 0 102 11 AM 0

Page 42: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 42

101 5 PM 1 102 3PM 1 Empno in_time out_time 101 8 AM 5 PM 102 11 AM 3 PM

� select e1.logtime as in_time, e2.logtime as out_time from emp_log e1, emp_log e2 where e1.empno=e2.empno and e1.status < e2.status

In self join conditions we must use alias names for joining itself. FUNCTIONS:- Function is a readymade formula. It is defined for achieving particular task. Or Function is a sub program or module or group of instructions which are placed in a specified order for solving given problem. There are two types of functions:-

i) Predefined functions:- These are the functions which can develop by software professional and all these functions are available in given software. Here user simply uses all these functions without writing any code. These are also called built-in functions. There are two types of predefined functions:-

Page 43: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 43

i) scalar functions or single row functions:- Numeric functions like ceiling(), floor() Date functions like power(), abs(), sqrt() String functions like getdate(),datediff(), dataepart() String functions like char(),convert(), upper(), Ascii(), lower()

ii) Aggreate functions or group functions:- Sum(),avg(), max(), min(), count() � Write a query for displaying employee’s total salary, average salary and max. and min. salary. Select sum(salary) as total_salary , avg(salary) as avg_salary, Max(salary) as max_salary , min(salary) as min_salary from EMP � Write a query for displaying employees count Select count(*) as no_employees from emp For above query null will be counted Select count(deptno) as no_employees from emp

For above query null will not be counted Select count(distinct deptno) from emp For above query null, duplicate will not counted GROUP BY CLAUSE:- This is used for dividing entire table into number of subgroups based on the given condition. Syntax:- Select columns…………, group functions from <table name> Where <condition> Group by <emp>

Page 44: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 44

Ex:- write a query displaying highest salary based on the dept no. Select deptno, max(salary) as max_salary form emp group by deptno

� Write a query for displaying lowest and highest salaries based on the job?

Select job , max(salary),min(salary) from emp group by job � Write a query for displaying sum, avg. salary

based on dept. no. Select deptno, sum(salary),avg(salary) form emp group by deptno � Write a query for displaying max, min salary based on deptno and the dept should have more than two employees? Select deptno, max(salary), min (salary) from emp Where count(deptno) > 2 group by deptno //�error – where clause does not support group functions. Select deptno, max(salary), min(salary) from emp group by deptno having count(*) >2

Select deptno, max(salary) , min (salary) form emp Group by deptno having count(deptno)>2

Page 45: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 45

Special operators:- IN, NOT IN, BETWEEN, NOT BETWEEN, LIKE, NOT LIKE, IS NULL, IS NOT NULL IN:- It will accept value from the given list.

� Write a query for displaying clerk, analyst and accounts of employee details?

Select *from emp where job=’clerk’ or job=’analyst’ or job=’accountant’ Select *from emp where job IN(‘clerk’,’analyst’,’accountant’) Select *from emp where job!=’clerk’ and Job!=’analyst’ and Job!=’accountant’

BETWEEN:- This is for evaluating range conditions.

� write a query print all employees information for salary>5000 and salary=< 20000

select *from emp where salary>7500 and salary<=20000 select *from emp where salary between 5000 and 10000 reverse of above Query:- select *from emp where salary<5000 or salary>20000

Page 46: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 46

select *from emp where salary not between 5000 and 20000

LIKE:- It is used for comparing character patterns. It is generally associated with two symbols % _ . -> write a query for displaying employee records whose employee name starts with ‘c’.

select *from emp where ename like ‘c%’ � Write a query for displaying employee records

whose employee name having ‘n’ as second letter? Select *from emp where ename like ‘_n%’ � write a query for displaying employee records

whose employee name containing only 5 characters?

Select *from emp where ename like ‘_ _ _ _ _’ IS NULL:- This is used for comparing with null values.

� write a query for displaying all employee records whose deptno is null?

Select *from emp where deptno=NULL // error Select *from emp where deptno IS NULL

Note:- Null values are do not compare with assignment operator. Compare with only isnull and not is null operator.

Page 47: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 47

Select *from emp where deptno!=null Select *from emp where deptno is not null SUB QUERIES:- Sub query is a statement which is under another query. Sub query will execute before executing . It’s main query

1) Non co-related sub query i) single row sub query:- performed by relational operator ii) Multi row sub query:- performed by IN,ALL,ANY operators

2) co-related subquery emp table empno ename salary deptno mgr 11 RAJ 25000 10 NULL

� Write a query for displaying employee records who are belong to ‘RAJ’ ‘s department?

Select deptno from emp where ename like ‘RAJ’ Select *from emp where deptno=(select deptno from emp where ename like ‘RAJ’) In the above query server will executes sub query based on the sub query value server will executes main query when sub query returns single value then it is called single row sub query. � Write a query to display all employee records

whose salary should be greater than maxium salary of ‘kumar’ department?

Page 48: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 48

Select * from emp where salary > (select max(salary) from emp where deptno=(select deptno from emp where ename like ‘kumar’)) � write a query for displaying employee records

whose salary should be greater than total salary of tenth department?

Select *from emp where salary > (select sum(salary) from emp where deptno=10)

� write a query to print manager details? Select *from emp where empno IN( select distinct mgr from emp) When a sub-query return more than one value as an output then that is called multi-row sub-query . In this case we need to use special operators IN,ALL,ANY

� Write a query for displaying employee records whose salary greater than maximum salary of tenth department?

Select *from emp where salary>( select max(salary) from emp where deptno=10) Or Select *from emp where salary > ALL (select salary from emp where deptno=10) In the above examples first query is single row subquery, here we are using aggreagate functions, maximum functions for getting highest salary from tenth department , where as in the second query is a

Page 49: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 49

multi row sub query it returns moe than one value as an output, to its nearest outer query. In this case we are using special operator ‘ALL’.

>ALL :- It will accept highest value from given set of values <ALL:- It will accept only min. value from the given set of values

Write a query for displaying all employee records whose salary less than min. salary of tenth department? Select *from emp Where salary<ALL (select salary from emp where deptno=10) Or Select *from emp where salary < (select min(salary) from emp where deptno=10) >ANY: It will accept min value from given list of values.

� Write a query for displaying all employee records whose salary less than minimum salary of tenth department? Select *from emp >ANY (select salary from emp where deptno=10)

<ANY:

Page 50: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 50

It will accept highest value from the given list of values.

� Write a query displaying employee records whose employee salary is less than maximum salary of tenth department?

Select *from emp salary < ANY (select salary from emp where deptno=10)

1) >ALL : Employee records whose salary > max. salary of tenth

department 2) <ALL: employee records whose salary< min. salary of tenth

department 3) >ANY : employee records whose salary> min. salary of tenth

department 4) <ANY: employee records whose salary< max salary of tenth

department Co-related sub queries:- A sub-query which retrieves value from outer query and it will executes for each record of its outer query ie when outer query selects ‘n’ records then sub query will executes ‘n’ times. Sub query will execute based on outer query value and it provides resultant value as an input to its nearest outer query. Based on this value outer query will executes Ie One relation existed between the given two queries and which are executed in circular path. This type of query is used for achieving some complicated problems.

Page 51: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 51

� Write a query for displaying employee records whose employee salary is greater than average salary of same department? Select *from emp e where salary > (select avg(salary) from emp whre emp.deptno=e.deptno)

� Write a query for displaying highest salary employee details?

Select *from emp where salary=(select max(salary) from emp) Select *from emp where 0= (select count(*) from emp e where emp.salary < e.salary) Select *from emp where 1=(select count(*) from emp e where emp.salary <e.salary) Select *from emp where 3=(select count(*) from emp e where emp.salary < e.salary)

In above example sub query will retrieve value from its outer query ie Salary from ‘emp’ table , same value compare with each and every value

of ‘e’ table, if count is equivalent to the given value then that is the required highest value.

Views:- View is a window through which we can allow part of the

database for updating and displaying purpose. Basically it is a virtual table does not consist any data.

Page 52: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 52

View is a name it can hold only select statement.

View is a precompiled select statement.

Advantage:- By creating views we can hide original table names and original columns of the table. View is used for simplifying a long based queries. This is used for summarizing data and also can filter the data from given database. Ie we can retrieve data from the database based on the given expression. Here view is similar to pointer concept, directly it can hold any data. It can hold only precompiled select statement and if DML operations performed on the view those modifications are automatically effected on the table ie without using table name we can modify or delete some part of the table. Used for simplifying long based on queries. VIEWS:-

There are two types of views.

i) simple view: - created on single table ii) complex view:- created on more than one table. iii) Indexed view Syntax:-

Page 53: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 53

Create view <viewname> [with encryption] as <select statement> [with check option] Ex:-

� create view v20 with encryption as select *from emp where deptno=20 with check option

� select *from v20

� update v20 set salary=salary+1000

� delete from v20

� select *from v20

� insert into v20 values(77,’k’,7000,30)

department number is 30 but view is for 20 dept number But it is valid ie no restrictions on ‘insert’ statement on views. Here by using view we can insert the record into any part of the table, basically no restrictions on insert statement but we can update and delete. Delete from v20 where deptno=30 Update v20 set salary=salary+(salary *10/100)

where deptno=10

With check option clause:- Generally no restriction on the insertion statement

on view ie by using view we can insert the data into any location of

Page 54: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 54

the table but some time we restrict insert option ie it will allow only insert into specified location, in this case we need to use ‘with check option’ while creation of view.

i) create view v10 as select empno, salary, deptno from

emp where deptno=10 with check option ii) insert into v10 values(99,7000,20) //error: because ‘with

check option’ allows for deptno=10

iii) select *from v10

With encryption clause:- This is for providing security for the view contents. It simply encrypted view contents ie converting view contents into secured format and no body can see the contents of view. ALTER VIEW:-

This is for modifying existing view contents. Syntax:- Alter view <view name> with encryption as <select statement> with check option

� alter view v10 with encryption as select *from emp where deptno=10

COMPLEX VIEWS:-

Syntax:- Create view <view name> [with encryption] as <select stmt. Which uses more than one table> where <condition> [with check option]

Page 55: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 55

Create view complex_views as select empno, ename, salary , dept.deptno,dname from emp,dept where emp.deptno=dept.deptno � insert into complex_view(empno,ename,salary) Values(115,’F’,15000)

� insert into complex_view(deptno,dname) values(50, ‘marketing’)

� insert into complex_view

values(120,’Z’,70000,60,’purchase’)

// error: because we can not insert into more than one table. It is possible through trigger. Here using single insert statement, we can not insert data at time more than one table, but it is possible through combination of views and triggers.

Note:- Complex views are not updatable views. This means using complex

views we can update only one table at time.

Creating a view based on another view:-

� create view v1 as select *from emp where deptno=10

� create view v2 as select empno, salary from v1

Page 56: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 56

� select *from v1 � select *from v2 � delete from emp � select *from v1 � select *from v2 � drop table emp � select *from v1 //error

Note:- we can create a view based on another view up to 32 levels. Syntax for dropping view:- Drop view <view name1>[,viewname2,……………] Ex:- Drop view v10, v20

iv) indexed views:- Generally we can cerate indexes on table , but some time we can create index on views also. If that view is created with schemabinding option. Syntax:- Create view v1 with schemabinding as Select empno,ename,sal from dbo.emp � create clustered index cidx3 on v1(empno) Here indexed views also called materialized views because this type of views can hold data directly. Here without dropping view , we can not drop parent table. Here ‘dbo’ is default schema in sql server

Page 57: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 57

SCHEMA:- Schema is a concept for holding or grouping objects, to avoid naming conflications. Ex:- create schema s1 Create schema s2 S1,s2 are schemas ,just like folders

� create table s1.emp(empno int , ename varchar(20)) � create table s2.emp(empno int, ename varchar(20))

syntax for dropping schemas:- DROP SCHEMA s1,s2 Before dropping schema, objects of schema should be dropped. Ie Drop table s1.emp Drop table s2.emp Drop table s1,s2 SYNONYMS:- It is new topic in 2005 version for creating permanent alias names for the specified object. Syntax for creating synonym:- CREATE SYNONYM synonym name for object name Ex:- CREATE SYNONYM E1 FOR EMP1 CREATE SYNONYM E2 FOR EMP2 CREATE SYNONYM E3 FOR E1 // ERROR:this type of chaining is not allowed.

Page 58: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 58

Dropping synonyms:- DROP SYNONYM synonym name,---------------- DROP SYNONYM e1, e2 INDEX:- Here sql-server index similar to textbook index. Internally it will arrange data in the specified order for improving performance of the execution. It is purely internal task done by the server. Here we can not see what is happening internally. This is the only performance job. For example When table having thousands of rows whenever we retrieving data from this table it take some more time. Inorder to reduce execution time, we can use index concept. Primary key=unique key + not null +clustered index Types of index:-

i) Clustered index:- It can change physical order of the table and will arrange data in the ascending order based on the specified column and we can create only one clustered index for a table. It closely associated with primary key.

Syntax:- Create <index type> <index name> on table1(col.s) Ex:-

� create clustered index clx_idx on emp(emp(empno)

� create clustered index clx_idx1 on

Page 59: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 59

iemp(ename,salary)

note:- per table only one clustered index is allowed.

ii) non-clustered index:-

249 non clustered indexes on single table is allowed.

It will no change order of the table but we can create ‘n’ number of non-clustered indexes on a single table.

Ex:- � create [non clustered] index non_idx on emp(salary) � create index non_idx on emp(salary) Here non-clustered index is a default index, when user not specifying any

index type, then server will consider as non-clustered index type. iii) Unique index:-

This index generally created unique constraint columns . Here It will not allow any duplicate values.

� create unique index uq_idx on dept(dname) iv) composite index:- It is also similar to unique index . when unique index created on more than one column then that is called composite index. Create unique index uq_idx on dept(dname,loc) Note:- using unique indexes we can get higher performance other indexes. Because only one reason, it will not allow any duplicate values. Syntax for dropping index:-

� drop index tablename.indexname

Page 60: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 60

ex:- drop index dept.uq_idx

SET OPERATORS:- These are used for combining more than one query . Simply displays output of first query and output of second query one by one. When you are using set operators for combine the queries , the given query should have same number of columns and same datatype or compatible datatype. i) union all:- it will displays all data items from query1 and query2 and

so on… syntax:- <select stmt1> <set operator> <select stmt2> <set operator> | | | <select stmt n> Ex:- Select empno from emp union all select deptno from dept In above query , it will displays all employees numbers from emp table and deptno from dept table. Select empno from emp union all select dname from dept.//error: because datatypes of empno and dname is not matched. � select empno, ename from emp union all select deptno,dname from dept � create table number(num int) Insert numbers values(10)

Page 61: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 61

Insert numbers values(-20) Select num from numbers where num < 0 Union all Select num from numbers where num>0

------� outputs duplicate values. Union:- It displays only unique value from the given queries and it will change the output order ie it displays all values ascending order by default and outputs distinct values.

� select num from numbers where num<0 union

select num from numbers where num >0 CURSORS:- It is a temporary memory location for storing transactional data and for the purpose of displaying and updating etc. Generally from the physical area user unable to retrieve the specified record directly, in this case create temporary memory and populating entire data from physical area memory to temporary memory from the temporary area user can retrieve selected record directly. Select top 2 * from dept

Page 62: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 62

Here cursor will occupy same memory area ie logical memory .User can create logical memory with help of cursor. From that cursor we can retirieve record by record into batch. Syntax to declare cursor:- Declare <cursorname> cursor <cursor type> For <select stmt.> Syntax for open a cursor:- Open <cursor name> Syntax for fetch record from the cursor:- Fetch <keyword> from <cursor name> Syntax for closing a cursor:- Close <cursor name> Syntax for removing cursor:- Deallocate <cursor name> Keywords in fetch statements:-

i) first:- this is for retrieving first record from the cursor ii) last:- this is for retrieving record from the cursor iii) next:- for retrieving next record from the cursor iv) prior:- for retrieving previous record from the cursor v) absolute n :- for retrieving ‘n’ th record from the cursor from

top or bottom vi) relative n:- for retrieving n th record from the cursor ie it moves

from the current cursor position

Page 63: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 63

There are four types of cursors:-

i) Forward cursor:- This cursor does support only one way accessing mechanism ie forward direction and it supports only ‘next’ keyword. It is also default cursor ie if user not specifying any cursor type server will consider as forward type. Ex:- Declare fcur cursor forward_only For Select *from dept Open fcur Fetch next from fcur While(@@fetch_status=0) Begin Fetch next from fcur End

ii) static cursor:- if any modifications are done in the table after opening the cursor, those modifications are not effected in cursor area and once you close the cursor and reopen the same cursor, those modifications are effected automatically in cursor area. � declare sta_cur cursor static For Select *from dept Open sta_cur

Page 64: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 64

Update dept set loc=’delhi’ where deptno=10 Fetch first from sta_cur Close sta_cur Open sta_cur fetch first from sta_cur Close sta_cur Open sta_cur Fetch last from sta_cur Fetch prior from sta_cur Fetch absolute 2 from sta_cur Close sta_cur If absolute value is positive , it displays ‘n’th record from top of the cursor otherwise if absolute value is negative value , it displays n th record in backward direction for bottom of the cursor.

Fetch absolute -2 from sta_cur

� backward direction from the bottom of the cursor fetch relative 1 from sta_cur

� forward direction from current position upto one record fetch relative -1 from sta_cur � backward direction from current position upto one record

Page 65: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 65

iii)dynamic cursor:- After opening dynamic cursor if any updations are occurred in the table, those are automatically reflected in the cursor are also. This type of cursor , not required to close and reopen the cursor � declare dy_cur cursor dynamic For Select *from dept Open dy_cur Update dept set dname=’admin’ where deptno=40 Fetch last from dy_cur

iii) Keyset cursor:- This cursor can hold only key information, if any modifications are done non-key columns, then also we can retrieve updated information from the cursor, otherwise if any updating are occurred on the key column, then we can not retrieve those records form cursor. Before declaring keyset cursor, it is better to create unique index on the suitable column. Create unique index uq_idx on dept(deptno) Declare ks_cur cursor keyset For Select *from dept

Page 66: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 66

Ks_cur _________ 10 20 30 40 Dept _______________________________________ 10 accounts delhi 20 research boston 30 sales hyd 40 admin hyd Open ks_cur Update dept Set loc=’new york’ Where deptno=10 Fetch first from ks_cur Update dept Set deptno=50 Where dname=’admin’ Fetch last from ks_cur Close ks_cur Deallocate ks_cur

Page 67: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 67

BATCHES:- Here writing ‘n’ number of statements and execute as a single unit or single batch called are batch statement There are two types of batches:- i) Anonymous batch ii) Named batch (stored procedures, functions , triggers) Anonymous batch:- Without specifying any name, we can create ‘n’ number of statements and execute as single batch called anonymous batch statement and all these statements we can save under one file name. Variables:- Variable is a memory element for storing specified data item. Here variables are two types. One is global variables and another one is local variables. Global variables:- These are built in or existing variables which are provided by software and these variables can be shared by all databases and users and which should be preceded by ‘@@’ symbol. Ex:- Select @@version -� displays current version of the software Select @@language --� displays current usage language Select @@textsize Select @@connections Select @@ servername

Page 68: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 68

Local variables:- These are variables which can be created by the user for storing data items and which can shared by only specified block. Syntax for declaring the variables:- Declare @<variable> datatype, @<variable2> datatype Ex:- Declare @A int , @b int ,@c int Syntax for assigning values to the variables:- Set @<variablename>=<value> Set @A=10 Set @B=20 Set @c=@A+@B ‘Print’ statement:- This is for printing values from the variables and as well as specified messages. Ex:- Print ‘hai’ Print @A Comment operators:- � double hypen (single line comment separator) � /* � */ � Multiline comment separator

Page 69: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 69

-> write a batch for performing addition operation on given to values? -- batch for addition operation Declare @a int, @b int ,@c innt Set @a=10 Set @b=20 Set @c =@a+@b Print ‘addition of a and b’+ convert(varchar(20),@c) Control statements:- These are used for controlling the executable statements, basically these are two types. i) selection control statements (if ) ii) iterative control statements(loop--while ) If statement:- This is used for evaluating the given expression, based on the expression it will execute the statements. i) syntax:- if(exp) begin - - End

Page 70: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 70

ii) syntax:- if(exp) begin end else

begin end

iii) syntax:- if(exp1) begin -- End Else if (exp2) Begin -- End Else Begin -- End

Page 71: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 71

iv) syntax:-

if(exp1) begin -- If(exp2)

Begin -- If(exp3) Begin -- End End end here 32 nested levels are supported. Ex:- Declare @a int ,@b int , @c int Set @a=10 Set @b=20 Set @c=30 If(@a >@b) Begin If(@a >@c) Print ‘a is big’ Else Print ‘c is big’ End Else if (@b >@c) Print ‘b is big’ Else Print ‘c is big’

Page 72: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 72

Iterative control structure:-

i) while statements:- It is one of the looping structure for executing specified block of statements repeatedly without writing again and again. Syntax:- While(<exp>) Begin -- End � Write a query for repeating your name for ten times Declare @i int Set @i=1 While (i<=10) Begin Print ‘prashant’ Set @i=@i+1 End User defined stored procedures:- These are the procedures developed by the user based on user requirements. Syntax:- Create proc[edure] <procedure name> <parameters list> [output] [with encryption] As Begin End

Page 73: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 73

Ex:- -> procedure without any parameters? Create proc dept_show As Select *from dept Syntax for executing procedure Exec[ute]<procedure nam> [input values] [out parameters output] Ex:- Exec dept_show Create procedure show @eno int As Begin Select *from emp where empno=@eno End

� execute show 11 --� Write a procedure for getting employee salary by supplying employee number Create procedure show_sal @eno int, @sal money output As Begin Select @sal=salary from emp where empno=@eno End Executing:- Declare @esal money Exec show_sal 22, @esal output Print ‘salary’+ convert(varchar(20),@esal)

Page 74: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 74

-�write a procedure with default values Create procedure show1 @eno int =11 As Begin Select *from emp where empno=@eno End

� execute show1 22 � execute show1 syntax for alter the procedure alter proc[edure] <procedure name> <parameters list [default value] [output][with encryption] Example procedure with encryption clause:-

Create proc show_emp with encryption As Begin Select *from emp End Alter proc show_emp As Select *from emp1

Page 75: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 75

User defined functions:- A function is a sub program or module or it is a ready made formula defined for achieving particular task. Sometimes built-in functions are not suitable for current scenario, in this case sql-server provides a feature for creating new functions based on the user requirement. These are called user-defined functions. Features of functions:-

1) reusability 2) decompose large task into number of smaller tasks inorder to

avoid a complexity of the problem 3) it improves performance of the execution

syntax:- create function <function name> [(@par1 type[=default value],[…n]] Returns <return type> [with recompile] As Begin Return(<exp>) End

Page 76: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 76

� create function addition(@a int, @b int) returns int As Begin Declare @C int Set @C =@A+@B Return (@C) End Syntax for executing function:- Select <function name ([values])> � select dbo.addition(3,2) [dbo—database object---called as schema] Print dbo.addition(10,20)

Page 77: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 77

� create function fact(@n int) Returns int As Begin Declare @f int Set @f=1 While(@n!=0) Begin Set @f=@f*@n Set @n=@n-1 End Return (@f) End Print dbo.fact(3) � create function reverse(@n int) returns int As begin declare @rem int, @rev int set @rev=0 while(@n!=0) begin set @rem =@n%10 set @rev=@rev *10+@rem set @n=@n/10 end return (@rev) end

Page 78: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 78

� write a function which returns value Create function showemp() Returns table As Return ( select *from emp) Syntax for modifying function:- Alter function <function name> [@par1 type=[default value], [----n]] Returns <return type> [with recompile] As Begin Return (<exp>) End � create function add_default(@a int =10,@b int =20) returns int As Begin Declare @C int Set @C=@a+@b Return (@c) End � select dbo.add_default(default,default) �select dbo.add_default(100,default)

Page 79: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 79

Syntax for removing function:- Drop function <function name> � drop function add_default TRIGGERS:- Trigger is a mechanism , by which it is invoked by the server automatically when specified action occurred on the specified object. This is automatic process done by server internally , sometimes user want to perform some internal task, when user applying dml transactions on the data. Ex:- when we deleting data from table at the same time that deleted data we can store into some other table. Ex:- when you modifying existing data of the table and at the same time we need to store old data and new data different tables. Different types of trigger:-

i) insert trigger:- This trigger simply activate when user performs insert operation on the specified table.

Page 80: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 80

Syntax:- Create trigger <trigger name> On <table name> For { insert | delte | update} As Begin End � select * into dept_new from dept where 1=2 Above query used for copying the structure of specified or existing table.

� > create trigger ns_trg on dept For insert As Insert into dept_new select *from inserted � insert into dept values(32,’sales’,’hyd’) � select *from dept � select *from dept_new Here insert trigger create new table called inserted. This table holding always newly inserted record. This is a temporary table automatically created and deleted by trigger Delete trigger:- It is invoked by the server when user performs delete operation on the table

Page 81: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 81

Ex:- Create a delete trigger for storing deleted records � select * into dept_delete from dept where 1=2 � create trigger delete_trg on dept for delete As Insert into dept_delete select *from deleted � delete from dept where deptno=40 � delete form dept where deptno=30 � select *from dept_delete Update trigger:- This trigger invoked by the server, when user performs update operation on the specified table. � select *into dept_old from dept where 0=2 � select *into dept_new from dept where 1=2 � create trigger update_trg on dept For update As Begin Insert into dept_old select *from deleted Insert into dept_new select * from inserted End

Page 82: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 82

� update dept set loc=’delhi’ where loc=’new york’ � update dept set dname=’hr’ where dname=’sales’ Instead of trigger:- This trigger invoked by the server, instead of specified DML operation(insert, delete, update) Syntax:- Create trigger <trigger name> On <table name> | <view name> Instead of {insert | update | delete} As Begin End � create trigger insteadof_trg on Dept Instead of insert As Delete from emp � insert into emp values(99,’mahesh’,6777,89) � select *from emp generally we can not insert data into more than one table at a time but it is possible with help of views and instead of trigger.

Page 83: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 83

Ex:- � create view emp_dept As Select empno,ename,dept.deptno,dname,loc From emp,dept Where Emp.deptno=dept.deptno Insert into emp_dept values(2,’yogi’,45,’admin’,’chennai’) //error In the above Query we are passing data items empno, ename into emp table and supplying deptno,dname,loc into dept table but this type of insertion is not possible directly. To achieve this type of insertion use ‘instead of trigger’ concept Create trigger insteadof_trigger1 On emp_dept Instead of insert As Begin Insert into emp(empno,ename) select empn,ename from inserted Insert into dept(deptno,dname,loc) select deptno,dname,loc from Inserted End Syntax dropping trigger:- Drop trigger <trigger name> Ex:- Drop trigger ins_trg

Page 84: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 84

DATA CONTROL LANGUAGE:-

i) grant ii) revoke grant� this is for granting permissions on the specified object for the specified user accounts grant all | permission list on object name to user1, user2,………….. ex:- create two new logins login1, login2 and create employee table on login1 and insert some records into the table. One login can not access another login tables .To allow this ,give permissions to that login. Login2: Select *from emp -�//error Login1: grant select on emp to login2 Only select permission is granted �GRANT INSERT , UPDATE ,DELETE ON emp to login2 �Grant all on emp to login2 Revoke:- This is to get back the permission from the specified user account. Syntax:- REVOKE ALL | ON object name FROM user1, user2…….. Login1:- REVOKE SELECT ON emp from login2

Page 85: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 85

Login2:- SELECT *FROM EMP //ERROR Login1:- REVOKE ALL ON emp from login2 TRANSACTION CONTROL LANGUAGE:- i) ROLLBACK:- This is for rolling transaction back. Just like undo

operation to cancel the specified transaction. ii) COMMIT:- to make a transaction permanent. Once a transaction

commited , we can not rollback the transactions AUTO COMMIT TRANSACTIONS:- � select *from emp Five row(s) selected � delete from emp Five row(s) affected � select *From emp O row(s) selected � rollback This error is because it is already auto commited.

Page 86: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 86

CHANGING AUTOCOMMIT MODE:- Implicit transactions:- SET IMPLICIT_TRANSACTIONS ON SELECT *FROM DEPT 3 row(s) effected Delete from dept 3 row(s) affected Select *from dept 0 row(s) affected Rollback

� rollbacks delete query In this one problem is there, that is it can rollback one transaction only. Explicit transaction:- Syntax:- Begin tran| transaction Begin transaction Delete from student Delete from customer Delete from emp Delete from emp Rollback --- rollbacks all previous transactions Problem lies here is , it rollbacks all transactions but I want to rollback last two transaction then go for

Page 87: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 87

� save transaction using this user can rollback to specified transaction. BEGIN TRANSACTION DELETE FROM STUDENT SAVE TRAN T1 DELETE FROM CUSTOMER SAVE TRAN T2 DELETE FROM EMP ROLLBACK TRAN T2 ROLLBACK TRAN T1 SOME USEFUL BUILT-IN STORED PROCEDURE:- 1)sp_help ‘table1’ Displays schema of table 2)sp_rename ‘emp.empno’ ‘eno’

column name is changed to eno

3) sp_helpconstraint dept describes constraints

Page 88: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 88

4) sp_helptext <view name | stored procedure | trigger> sp_helptext v10 displays contents of view

5) sp_helpindex tablename sp_helpindex dept

displays indexes

NORMALIZATION:- It is the process of deleting or avoiding data redundancy of the existing table based on theory of functional dependency. Normalization process finds dependency among the columns in the existing table, if it finds any dependency simply applies normalization procedure for avoiding redundancy. After performing this task again if it finds any dependency among columns, must to repeat normalization process until all dependencies are removed Empno project no ename salary prj. Name budget dept no dname loc 11 (p1,p2) raj 1000 (n1,n2) (b1,b2) 34 admin hyd Normal forms:- A rule in normalization process is called as normal form. Generally normalization process done based on three normal forms. 1NF:-

Page 89: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 89

Multivalue attribute should be avoided or removed ie an attribute shoul represent only one data item. Ie 11 p1 raj 1000 n1 b1 34 admin hyd 11 p2 raj 1000 n1 b1 34 admin hyd Empno, project no are key columns. In the above table , we should use only composite primary key concept for empno and project no. columns, for unique format. 2NF:- Table should be in 1NF and partial functional dependency should be removed. In the above table ename, salary non key columns are depending on emp no, column but it is only part of key column. Project name, budget non key columns arev depending on project number column. But this is also part of key column. This type of dependency is called partial dependency. So split into multiple tables. Project table:- Projectno projectname budget P1 raj b1 P2 raj b2 Employee table:- Empno ename salary deptno dname loc 11 raj 1000 n1 admin hyd

Page 90: 3) ANSI SQL (American national standard institute ... · SQL Server 2005 2 12) T-SQL is one of the D.B. language by which we can communicate with sequel server databases. 13) DBMS

SQL Server 2005 90

3NF:- Table should be 2NF an transitive functional dependency should be removed. Above deptno, dname,loc there three are nonkey columns is employee table, generally all non-key cols should depend on key-column but in this case dname, loc are depending on deptno column ie some nonkey columns depending on another non key column this type of dependencey is called transitive functional dependency. To bring the table to type of dependency by splitting table into multiple tables. Deptno dname loc N1 admin hyd