SQL
SQL
===
---> it is a DataBase Language, which is used to communicate with Database
Systems
(SQL server,MS-ACCESS,ORACLE,MySQL,DB2,SYBASE) for the follwing purpose
*) Inserting Data
*) Deleting the Data
*) Updating the Data
*) Retrieving the Data
--> it is not a case-sensitive Language
SQL TOPICS
==========
*) SQL statements
*) SQL OPerators
*) SQL set Operators
*) SQL joins
*) SQL stored Procedures
*) SQL Triggers
*) T-SQL (Transact SQL)
*) SQL Cursors
*) SQL Views
*) SQL Constraints
*) SQL Functions
*) SQL RULES
SQL STATEMENTS
==============
1. DDL statements
============
*) create
=====
--> it is used to create the Database,Table,views, triggers, stored
Procedures and so on
---> When Executing this Query, the two files are Created
use DataBase_Name
Ex : use EmployeeDB
SQL DataType
==========
int --> specifies the integer values to Store in a Column
datetime --> specifies the Datetime value to Store in a Column
varchar ---> specifies the Variable Length characters to Store in a
Column
char ---> specifies the Fixed length of character String to
store in a Column
money ---> specifies the Float values to store in a Column
varbinary --> specifies the Binary values to store in a Column
2) alter
====
--> used to change or modify the structure of Existing table
-------------------------------------------------------------------------
3. drop
===
---> used to remove the table from the DB system
4. truncate
======
--> used to remove all the rows from the table.
DML statements
===========
1. select
====
--> it is used to retrieve all rows or specific rows from the Table
2. Insert
====
--> used to insert or add new records to table
3) delete
====
--> used to delete all rows or specific rows from the table
Ex : delete from emp where eid=1001 -> deletes the specific row
SQL operators
===========
*) <, >,<=,>=,<>,=
*) AND
*) OR
*) IN
*) NOT IN
*) BETWEEN
*) LIKE
Ex : AND
===
Ex : OR
Ex : BETWEEN
Ex : IN
Ex : NOT IN
WILDCARD SYMBOLS
================
Ex : %
=====
*) UNION
--> used to combine the rows from two or more tables without
including the duplicates records..
syntax
=====
select * from T1 UNION select * from T2
*) UNION ALL
syntax
---------
select * from T1 UNION ALL select * from T2
*) INTERSECT
syntax
--------
select * from T1 INTERSECT select * from T2
*) EXCEPT [MINUS-ORACLE]
---> returns only the records that appears in first set but not in
the Second set..
syntax
--------
select * from T1 EXCEPT select * from T2
Ex :
===
select * from emp UNION select * from emp1
select * from emp UNION ALL select * from emp1
select * from emp INTERSECT select * from emp1
select * from emp1 EXCEPT select * from emp
SQL Constraints
=============
Data Integrity
---------------
--> it ensures the correctness of the Data stored in the Database
Tables
Constraints
--------------
--> constraints are set of rules, which are applied to table columns
Types of Constraints
================
syntax
=====
Ex :
==
create table student
(
sid int constraint sid_key primary key,
sname varchar(20)
)
Ex :
create table student1
(
sid int constraint sid_key1 unique,
sname varchar(20)
)
syntax
======
create table tablename
(
col1 datatype,
col2 datatype default value,
-----
)
Ex :
==
create table emp2
(
eid int,
ename varchar(20),
deptid int,
doj datetime default getdate(),
salary int default 6000
)
-> The Enforcement of NOT NULL constraint on a Table Column, makes the user
must enter the value in a Column which has NOT NULL constraint..
syntax
=====
create table <tablename>
(
col1 datatype not null,
col2 datatype,
----
)
v) check Constraint
-----------------------
--> it allows only a specific range values to Enter in a Column
syntax
=====
Ex:
==
create table emp
(
eid int,
ename varchar(20),
gender varchar(10) constraint gen_check check (gender
in('Male','female')),
age int constraint age_check check(age between 20 and 40)
)
syntax
=====
alter table tablename nocheck constraint [ALL|Constraint_name]
syntax
=====
alter table tablename check constraint [ALL|Constraint_name]
Ex :
===
create table student_personal
(
sid int constraint p_id primary key,
sname varchar(10),
address varchar(20),
gender varchar(10)
)
Ex : declare @a int
declare @name varchar(20)
set @varname=value
(or)
select @varname=value from <tablename> where <condition>
Ex :
===
set @a=100
or
select @a=100
Ex :
==
declare @a int
declare @salary int
declare @ename varchar(20)
set @a=1
select @salary=salary,@ename=ename from emp where eid=1001
print 'The value of a is : '+convert(varchar,@a)
print 'EName : '+@ename
print 'ESalary : '+cast(@salary as varchar)
Ex : if...else statement
==
declare @a int,@b int
set @a=10
set @b=20
if @a>@b
print 'a is greatest'
else
print 'b is greatest'
begin....end block
===============
syntax
=====
begin
//stmt1
//stmt2
-----
end
Ex :
==
declare @a int,@i int
set @a=10
set @i=1
while @i<=@a
begin
print 'i = '+convert(varchar,@i)
print 'i * a = '+convert(varchar,(@i*@a))
set @i=@i+1
end
Stored Procedures
==============
--> it is a procedure, which is used to execute the set of statements as a
Single Transaction
--> it is createad and stored in server
syntax
===
create table student (sid int,sname varchar(20),M1 int,M2 int,M3 int,tot int,aveg
int,result varchar(10))
SQL Functions
============
*) Numeric Functions
*) string functions
*) Date functions
*) conversion functions(convert,cast)
*) System functions
*) min()
*) max()
*) count()
*) sum()
*) avg()
i) Numeric Functions
===============
select sqrt(64),abs(-
50),sin(45),cos(45),tan(45),power(2,3),floor(23.67),ceiling(34.78),round(123.54676,
2),pi(),log(100),exp(5),rand()
System functions
=============
--> used to query general information about the DB system
select
host_id(),host_name(),suser_id(),suser_name(),object_id('emp'),object_name(20730584
21)
DCL statements
=============
syntax
======
syntax
======
TCL statements
============
---> commit
syntax : commit
---> rollback
syntax
======
rollback
Cursors
=======
1) declaring the Cursor
==============
syntax :
----
Ex:
===
syntax :
=======
open cursor_name
Ex : open mycursor
syntax:
======
close cursor_name
syntax
======
deallocate cursor_name
Ex:
==
deallocate mycursor
declare mycursor cursor scroll
for select * from emp
open mycursor
SQL VIews
===========
--> A view is a virtual table, which holds the Output of the Query..
syntax
======
create view view_name
as
select Query
--> joins are used to combine rows from two or more tables based on the
Condition..
types of joins
===========
*) inner join
*) outer join
--> left join
--> right join
--> full join
*) cross join
INNER JOIN:
===========
Returns all rows when there is at least one match in
BOTH tables
LEFT JOIN:
========
Return all rows from the left table, and the matched
rows from the right table
RIGHT JOIN:
=========
Return all rows from the right table, and the matched rows
from the left table
FULL JOIN:
===========
Return all rows when there is a match in ONE of the tables
*) inner join
========
syntax :
-------
or
select * from T1 inner join T2 on T.c1=T2.c1
*) outer join
==========
--> similar to inner join but it also includes the unmatched values in the
Resultset
syntax
select * from T1 [LEFT|RIGHT|FULL] OUTER JOIN T2 ON T1.C1=T2.C1
*) cross Join
==========
---> it gives the cortesian product of two tables
syntax
======
SQL Rules
======
-->a rule specifies the acceptable values that can be inserted into that column.
syntax
======
create rule rule_name as <conditional_Expression>
Ex:
===
create rule myrule as @salary>=3000
syntax
======
Ex:
---
syntax:
=======
execute sp_unbindrule 'obj.colname'
Ex:
===
execute sp_unbindrule 'emp_salary.salary'
Sql Identity property
===============
--> it is applied to the table column when creating the table,so we dont need to
enter the values in the column with idenity property..
syntax
======
Ex:
==
@@idenity
=========
--> it is a Global variable, which is used to find the most recently generated
identity value
Ex:
==
select @@identity
select ident_seed('emp')
select ident_incr('emp')
Triggers
======
syntax:
======