Professional Documents
Culture Documents
--display all
select ename from emp;
--display jos
select distinct job from emp;
--select date
select getdate()+5;
--like operator
select * from emp where ename like '_A%';
select * from emp where ename like '%s';
select * from emp where ename like '____';
--length operator
select * from emp where len(ename)=4;
--display all the employees in ascending order of department number and descending
order of jobs
select * from emp
order by deptno asc, job desc;
--displaying constraint
alter table deptno nocheck constraint pk_deptno_age_619B8048
--enabling constraint
alter table deptno
check constraint ck_altertest_age_619B8048
sp_help altertest
alter table altertest
drop constraint df_alter_doj
--coalesce contains any number of argumnets but it will returns non nullable
values
select COALESCE ("expression 1", "expressions 2", ...);
--numeric functions
select round(12345.53556,2);
select isnumeric(5);
select isnumeric('k');
select abs(-10);
select CEILING(14.3);
select FLOOR(234.2);
select SQUARE(16);
select sqrt(25);
select rand();
--date functions
select getdate();
select day('11/14/1989'); --fromat (mm/dd/yyy)
select month('11/14/1989');
select year('11/14/1989');
select datename(month,getdate());
select datepart(wk,getdate());
select convert(varchar (10),getdate(),101) as [dd-mm-yyyy];--101 is standard for us
select datediff(day,'2018-01-01','2018-10-05');
select datediff(wk,'2018-01-01','2018-10-10');
select datediff(month,'2018-01-01','2018-05-10');
--ansi syntax
select e.empno,e.ename,e.sal,d.deptno,d.dname from emp e join dept d on
e.deptno=d.deptno
--natural inner join if all the column name of the both tables has same columns
select * from emp natural join dept
--self join
select e1.empno,e1.ename as Employee,e2.ename as Manager from emp e1,emp e2
where e1.empno=e2.mgr
7593
T-sql(sql server):
================
transaction sql
procedure:
==========
1.no return value
2.create even errors
functions:
==========
1.alway return value
2.does not exceed if errors
sp_addmessage;
===============
stores user defined error messages in sql server catalog 'sys.messages'.
3 types of views
1.normal or standard view.
2.indexed or permanent
3.partition view
advantages of view:
==================
1.query simplicity
2.structural simplicity
disadvantage of views:
=====================
1.time consuming
2.updation problem incase of complex use
indexes:
=========
indexes are used to improve the performance of the database they can be created on
single or multiple columns
a table can have only one clustered index but multiple non clustured indexes.we cal
crete unique indexes for both clustered
and non clustered indexes
3 types of indexes
1.clustered index
2.Non clustered index
3.Filtered index
clustered index:
=================
1.have intermediate level
2.clustered index determined the logical order in whic table data is stored .the
leaf node contains the actual database o f the table .it is created an clustered
index key. the clustered index key must be unique
clustered index:
=================
1.not have intermediate level
2.non clustered index they are stored seperately from the table outside of the
datbasethey can be either
unique or nonunique.the leaf node has data pages which are not similar to the
pages
partitions:
===========
sql server 2017 supports upto 15000 partitions outside of db they can be either
unique or non unique the leaf node has datapages which are
not similar to the table.
advantages:
==========
1.maintance
2.efficient access to data
3.data integrity maintained
cursor operations:
==================
declare, open, fetch, and deallocate
types of cursor:
===============
1.static cursor--read only and rollable ie., move forward and move backward
2.dynamic cursor--
3.forward only cursor
3.1 forward_only_static_cursor
3.2 fast forward cursor.
4.keyset driven cursor
disdvantages: performance
============
syntax:
=======
1.@@fetch status
2.@@cursor_rows
3.@@cursor_status
exec p5 @deptno=30,@job='clerk'
exec p5 @deptno=default,@job='manager'
exec p5 @job='clerk'
select * from emp
select dbo.area(10.0,20.0)
--encryption
select dbo.area2(10.0,20.0)
sp_helptext area2
--views
create view viewname(alias name1,name2)--syntax starts
with encryption
with schemebinding
as
select statement[with check option]--syntax
select * from v1
insert into v1 values (8,'karthik','clerk',7992,1992-11-17,900,null,20)--will
insert into dept view,..though here it allows insertion
select * from v1
select * from emp
alter view v1
as select * from emp where deptno=20 with check option --now u cant insert values
with depid other than 1
select * from v1
insert into v1 values (8,'karthik','clerk',7992,1992-11-17,900,null,20)--will
insert into dept view,..though here it allows insertion
--encryption option
alter view v1 with encryption as select * from emp where deptno=20 with check
option
select text from syscomments where id= OBJECT_ID('v1')
create view v2
as
select * from emp,dept where emp.empno=dept.deptno
select * from v2
insert into v2 values(3,'ggg',3,'111')--cant be update
use master
create database dbp2
on primary
(
name='pfg',
filename='c:\pg1.mdf',
size=10MB,
maxsize=100MB,
filegrowth=10MB
),
filegroup fg1(
name='fg_1',
filename='c:\fg1.ndf',
size=10MB,
maxsize=100MB,
filegrowth=10MB
),
filegroup fg2(
name='fg_2',
filename='c:\fg2.ndf',
size=10MB,
maxsize=100MB,
filegrowth=10MB
) // error is user access is denied