Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

SQL Server

The document discusses SQL commands used for database management. It covers data definition language (DDL) commands like CREATE, ALTER, DROP for creating, modifying and deleting databases and tables. It also covers data manipulation language (DML) commands like INSERT, UPDATE, DELETE for managing data records and data query language (DQL) SELECT command for retrieving data.

Uploaded by

edwin mejia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

SQL Server

The document discusses SQL commands used for database management. It covers data definition language (DDL) commands like CREATE, ALTER, DROP for creating, modifying and deleting databases and tables. It also covers data manipulation language (DML) commands like INSERT, UPDATE, DELETE for managing data records and data query language (DQL) SELECT command for retrieving data.

Uploaded by

edwin mejia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

SQL SERVER

SQL COMMANDS

DDL Commands (Data Defintion Language )

1. Create - to create database and table


2. use - to select specific database
3. alter - to modify the structure of table
4. drop - to drop/delete database , table
5. truncate - to delete all records at a time from table

DML Commands (Data Manipulation Language)

1. insert - to insert records/data in a table


2. update - to update/modify inserted/saved data
3. delete - to delete specific record from table

DCL Commands (Data Control Language)

grant - to give access of database


revoke - to take back access of database

DQL Commands (Data Querry Language)

select - to show/fetch records

TCL Commands (Transaction Control Language)

commit
savepoint -
rollback

/* create database */

create database database_name;

create database batch10;

/* select database */

use database_name;

use batch10;

/* datatypes */

1) Numeric data types


1. int (-2,147,483,648 bits to 2,147,483,648) 1000 bits = 1byte
1000 byte = 1kb 1024kb = 1mb 6000000000
2. bigint (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,808 )
3. smallint (-32,768 to 32,768)
4. decimal
5. tinyint ( 0 - 255)
6. numeric

6999999999
7999999999

2) character String data types


1. char(8000 bits of characters) only alphabets
2. varchar (8000 characters) alphanumeric values (numbers+characters)
3. varchar(max) (231 characters)
4. nchar (4000 characters)
5. nvarchar (4000 characters)
6. text

char(15)
int

3)date and time datatype


1. date ('yyyy-mm-dd')
2. time ('hh:mm:ss')
3. datetime (jan-1-1753 to dec-31-9999) ('yyyy-mm-dd hh:mm:ss')
4. smalldatetime (jan-1-1900 to jun-6-2079)

4) binary datatypes
1. binary (fixed binary length 8000 bytes)
2. varbinary (variable length 8000 bytes)

3. image

/* create table */

create table table_name


(
column1 datatype(length),
column2 datatype(length),
.
.
columnN datatype(length)
);

create table tbl_student


(
stud_id int,
stud_name char(40),
stud_address varchar(60),
stud_contactno bigint,
stud_email varchar(30),
stud_DOB date,
stud_Fees decimal,
stud_course varchar(30)
);

use batch10;

/* drop table */

drop table table_name;

drop table tbl_student;

/* drop database */
use admin;
drop database database_name;

drop database batch10;

create database batch10;

use batch10;

/* create table */

create table tbl_employee


(
emp_id int,
emp_name varchar(40),
emp_address varchar(50),
emp_mail varchar(30),
emp_contactno bigint,
emp_DOB date,
emp_salary decimal,
emp_dept_id int,
emp_qualification varchar(30),
emp_adharno bigint
);

/* show table / fetch records */

select columnnames from tablename;


select column1, column2,.....columnN from tablename;
select emp_id, emp_name, emp_address, emp_mail, emp_contactno, emp_DOB, emp_salary,
emp_dept_id, emp_qualification, emp_adharno from tbl_employee;

select emp_id, emp_name, emp_adharno, emp_salary from tbl_employee;

select * from tbl_employee;

/* insert data or records */

address method
value method

/* address method */

insert into table_name (column1, column2,....columnN) values ('val1',


'val2',......'valN');

insert into tbl_employee ( emp_id, emp_name, emp_address, emp_mail, emp_contactno,


emp_DOB, emp_salary, emp_dept_id, emp_qualification, emp_adharno)
values (1, 'Sahil', 'Pune', 'sahil@yahoo.com',9089786756, '1999-10-04', 40000, 5,
'BE-IT', 123456789012);

insert into tbl_employee ( emp_id, emp_name, emp_address, emp_mail, emp_contactno,


emp_DOB, emp_salary, emp_dept_id, emp_qualification, emp_adharno)
values (3124567890, 'Sahil', 'Pune', 'sahil@yahoo.com',9089786756, '1999-10-04',
40000, 5, 'BE-IT', 123456789012);

insert into tbl_employee ( emp_id, emp_name, emp_address, emp_mail, emp_contactno,


emp_DOB, emp_salary, emp_dept_id, emp_qualification, emp_adharno)
values (4, 'Riya', 'Aundh', ' ',9089786756, '1999-10-04', 40000, 5, 'BE-IT',
123456789012);

select * from tbl_employee;

insert into tbl_employee ( emp_id, emp_name, emp_address, emp_contactno, emp_DOB,


emp_salary, emp_qualification, emp_adharno)
values (2, 'Kiran', 'Pune', 8009786756, '1995-03-24', 35000, 'BE-IT',
989856789012);

/* direct fetch records from form*/

insert into tbl_employee ( emp_id, emp_name, emp_address, emp_mail, emp_contactno,


emp_DOB, emp_salary, emp_dept_id, emp_qualification, emp_adharno)
values ( '@emp_id', '@emp_name', '@emp_address', '@emp_mail', '@emp_contactno',
'@emp_DOB', '@emp_salary', '@emp_dept_id', '@emp_qualification', '@emp_adharno');

/* value method */

insert into tablename values ('val1', 'val2',....,'valN');


insert into tbl_employee values (3, 'Pooja', 'Hadapsar', 'pooja@gmail.com',
8988999990, '2000-05-30', 40000, 2, 'BE-civil', 899898989898);

insert into tbl_employee values (3, 'Pooja', 'Hadapsar', 'pooja@gmail.com',


8988999990, 40000, 2, 'BE-civil', 899898989898); /* error */

use batch10;

select * from tbl_employee;

/* alter to add column */

alter table tablename add columnname datatype(length);

alter table tbl_employee add emp_bankdetails bigint;

/* alter to modify column */

alter table tablename alter column columnname datatype(length);

alter table tbl_employee alter column emp_bankdetails varchar(40);

/* alter to drop column */

alter table tablename drop column/index/constraint


columnname/indexname/constraintname;

alter table tbl_employee drop column emp_bankdetails;

select * from tbl_employee;

/* update */

update tablename set newvalue where condition ;

update tbl_employee set emp_mail = 'kiran@gmail.com' where emp_id = 2;

select * from tbl_employee;

update tbl_employee set emp_address = 'Poona' where emp_address = 'Pune';

update tbl_employee set emp_mail = 'riya@gmail.com' where emp_mail = ' ';

update tbl_employee set emp_dept_id = 10 where emp_dept_id is null;

insert into table_name (column1, column2,....columnN)


values ('val1', 'val2',......'valN'),
('val1', 'val2',......'valN'),
('val1', 'val2',......'valN');

insert into tbl_employee ( emp_id, emp_name, emp_address, emp_mail, emp_contactno,


emp_DOB, emp_salary, emp_dept_id, emp_qualification, emp_adharno)
values (5, 'Meera', 'Pune', 'meera@yahoo.com',7767786756, '1999-09-14', 45000, 3,
'BE-Mechanical', 167676789012),
(6, 'Rahul', 'Hadapsar', 'rahul@gmail.com',7897786756, '1998-01-18', 50000,
2, 'BE-Civil', 223456789012);

select * from tbl_employee;

/* delete */

delete from tablename where condition;

delete from tbl_employee where emp_qualification = 'BE-Mechanical';

select * from tbl_employee;

/* truncate */

truncate table table_name;

truncate table tbl_employee;

/* SQL Operators */

1) Arithmetic Operators

a = 10 b = 20
1. Addition (+) a + b
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Modulus (%)

a = 10 b = 3
a/b = 10/3 =

2) Comparison Operators
a = 10; b = 5;
1. = a = b false
2. != a != b
3. > a > b
4. < a < b
5. >= a >= b
6. <= a <= b
9. <> a<>b

3) Logical Operators
1. ANY select * from product any (select * from order where productid =
10);
2. AND select * from student where marks > 80 and age < 16;
3. OR select * from student where marks > 80 or age < 16;
4. EXISTS
5. LIKE
6. BETWEEN - between [lowerrange] and [higherrange]
7. IN - columnname in('val1',...., 'valN')
'val' in(col1,col2,.....)
8. NOT
9. ALL

use batch10;
select * from tbl_employee;
use customer;
select * from student1;

select marks, studid, marks + studid from student1;

select marks, studid, marks - studid from student1;

select marks, studid, marks * studid from student1;

select marks, studid, marks / studid from student1;

select marks, studid, marks % studid from student1;

/* cluases */

select
from
where
group by
having
order by

/* where */

select * from student1;

select columnnames from tablename where condition;

select * from student1 where marks = 92;

select * from student1 where marks != 92;


select * from student1 where marks > 90;

select * from student1 where marks >= 90;

select * from student1 where marks <= 80;

select * from student1 where marks < 90;

select * from student1 where marks <> 77;

select * from student1 where district like 'pune';

select * from student1 where district like 'pune' and marks < 80;

select * from student1 where district like 'pune' or marks < 80;

select * from student1 where marks between 80 and 90;

select studname from student1 where district in('pune', 'solapur', 'satara');

select * from student1 where district in('pune', 'solapur', 'satara');

select * from student1 where 'pune' in(address, district, city);

select * from student1 where 'wai' in(address, district, city);

select * from student1 where district not in('pune', 'solapur', 'satara');

select * from student1 where marks not between 80 and 90;

select * from customer;


select * from orders;

select * from customer where exists (select * from orders where customer.custid=
orders.custid and custid = 5);

select * from customer where custid = any (select custid from orders where
productname = 'clothes');

select * from customer where custid = all (select custid from orders where
productname = 'clothes');

/* aggregate functions */

count()
sum()
min()
max()
avg()
round()

select aggregate_function(columnname) from tablename;

select * from student1;

select count(studid) from student1;

select count(state) as no_of_states from student1;

select sum(marks) as total_marks from student1;

select max(marks) as Max_marks from student1;

select min(marks) as Min_marks from student1;

select avg(marks) as avg_marks from student1;

select round(marks, 2) as marks from student1;

/*scaler functions */

len()
upper()
lower()

select len(studname) as length, studname from student1;

select upper(studname)as studname, studname from student1;

select lower(studname)as studname, studname from student1;

/* group by clause */

select aggregate_function(columnname) from tablename group by columnname;


select * from student1;

select count(address)as no_of_students, address from student1 group by address;

select count(marks) as no_of_students, marks from student1 group by marks;

/* having clause */

select count(marks) as no_of_students, marks from student1 group by marks having


marks > 80;

select count(address)as no_of_students, address from student1 group by address


having marks > 80;
select count(address)as no_of_students, address from student1 where marks > 80
group by address ;

select count(address)as no_of_students, address from student1 group by address


having address in('aundh', 'pune', 'hadapsar');

/* order by clause */

select columnnames from tablename order by columnname;

select * from student1;

select * from student1 order by studname;

select * from student1 order by marks;

select * from student1 order by marks desc;

select * from tbl_employee;

select name, salary from tbl_employee where salary > 1250;

/* constraints */

not null
default
check

/* not null */

create table tblnotnull


(
id int not null,
name char(30),
address varchar(40)
);

select * from tblnotnull;

insert into tblnotnull (id, name, address) values (1, 'shreya', 'Wai');
insert into tblnotnull ( name, address) values ( 'shreya', 'Wai');
insert into tblnotnull (id, address) values (1, 'Wai');

/* default constraint */

create table center


(
id int,
name char(30),
center varchar(30) default 'Mumbai'
);

select * from center;

insert into center (id, name, center) values (1, 'sahil', 'pune');
insert into center (id, name) values (1, 'sahil');
insert into center (id, name, center) values (2, 'pooja', 'Thane');
insert into center (id, name) values (3, 'pratik');

/* check constraint */

create table reg


(
id int,
name char(20),
marks decimal,
check(marks > 60)
);

select * from reg;

insert into reg values (1, 'riya', 89);


insert into reg values (2, 'tejas', 61);
insert into reg values (2, 'tejas', 60);
insert into reg values (2, 'tejas', 79);
insert into reg values (2, 'tejas', 40);

/* sql keys */

primary key
unique key
alternate key
foreign key
composite key
super key
candidate key

/* primary key */

create table tblstudent


(
studid int primary key,
studname char(30),
address varchar(40)
);

select * from tblstudent;


insert into tblstudent (studid, studname, address) values (1, 'viaan', 'KP');
insert into tblstudent (studid, studname, address) values (2, 'viaan', 'KP');
insert into tblstudent ( studname, address) values ('viaan', 'KP');
insert into tblstudent (studid, studname, address) values (6, 'viaan', 'KP');
insert into tblstudent (studid, studname, address) values (3, 'viaan', 'KP');

insert into tblstudent (studid, studname, address) values (11, 'viaan', 'KP');
insert into tblstudent (studid, studname, address) values (7, 'viaan', 'KP');

/* unique key */

create table tblstudent1


(
studid int primary key,
studname char(30),
address varchar(40),
mobile bigint unique,
adharno bigint not null unique
);

select * from tblstudent1;

insert into tblstudent1 (studid, studname, address, mobile, adharno) values (1,
'viaan', 'KP', 9090909090, 454323456789);
insert into tblstudent1 (studid, studname, address, mobile, adharno) values (2,
'viaan', 'KP', 8880909090, 134623456789);
insert into tblstudent1 (studid, studname, address, mobile) values (3, 'viaan',
'KP', 9090909088);
insert into tblstudent1 (studid, studname, address, adharno) values (4, 'viaan',
'KP', 954323456789);
insert into tblstudent1 (studid, studname, address, adharno) values (5, 'viaan',
'KP', 994323456789);

/* foreign key */

columnname datatype foreign key references tablename(columnname)

create table customer


(
id int primary key,
name char(30),
address varchar(40)
);

create table tblorder


(
orderid int primary key,
product char(30),
custid int foreign key references customer(id)
);

select * from customer;


select * from tblorder;

insert into tblorder values (1, 'Colthes', 1);

insert into customer values (1, 'Ganesh', 'Pune');

/* composite key */

create table tblcourse


(
id int,
name char(30),
coursename varchar(30),
primary key(id, coursename)
);

select * from tblcourse;


insert into tblcourse values (1, 'raj', 'java');
insert into tblcourse values (2, 'Priya', 'java');
insert into tblcourse values (1, 'raj', 'python');

/* sql joins */

select * from customer;


select * from orders;
select * from product0;

1.INNER JOIN
2.OUTER JOIN
a.LEFT OUTER JOIN
b.RIGHT OUTER JOIN
c.FULL OUTER JOIN
3.CROSS JOIN
4.SELF JOIN

/* INNER JOIN */

select columnnames from table1 join table2 on table1.columnname =


table2.columnname;

select * from orders inner join customer on orders.custid = customer.custid;

/* left outer join */

select * from orders left outer join customer on orders.custid = customer.custid;


/* right outer join */

select * from orders right outer join customer on orders.custid = customer.custid;

/* full outer join */

select * from orders full outer join customer on orders.custid = customer.custid;

/* cross join */

select * from orders cross join customer;


select * from orders cross join customer where custname like 'a%';

select * from orders cross join customer where custname like '%a';

select * from orders cross join customer where custname like '%s%';

select * from customer where custname like 'a%';

select * from customer where custname like '%a';

select * from customer where custname like '%a%';

select * from customer where custname like 'a_______';


select * from customer where custname like 's____';

/* self join */

select * from customer;

select A.custname as CustomerA, B.custname as CustomerB, A.address, B.address


from customer A, customer B
where A.custname <> B.custname and A.address = B.address;

select A.empname , B.designation


from employee A, employee B
where A.exprience = '5years' ;

select * from student1;

select A.studname as StudentA, B.studname as StudentB, A.marks as MarksA, B.marks


as MarksB
from student1 A, student1 B
where A.marks > B.marks;

/* top limit */
select * from student1;

select top [limit] columnnames from tablename;

select top 1 * from student1;


select top 5 * from student1;

select top 3 * from student1 order by marks desc;

/* database refresh or random data */

select * from student1;

for mysql RAND() for oracle n sqlserver NEWID()

select * from student1 order by newid();

select top 5 * from student1 order by newid();

/* auto increment */

for mysql AUTO_INCREMENT() for sql server IDENTITY(1st value, incrementing value)

create table autoid


(
id int identity(1,1),
name char(30),
mobile bigint
);

select * from autoid;

insert into autoid values ( 'ram', 7878787878);

create table autoid1


(
id int identity(10,5),
name char(30),
mobile bigint
);

select * from autoid1;

insert into autoid1 values ( 'ram', 7878787878);

/* index */

create index index_name on tablename (columnnames);

create unique index index_name on tablename (columnnames);


create index idx_id on center(id, center);

create unique index idx_custid on cust(id);

select * from cust;

/* drop index */

drop index tablename.indexname;


drop index cust.idx_custid;

/* stored procedure */

create procedure procedurename


as sql_statement
go;

create procedure studentdata


as select * from tblstudent1
go
;

exec procedurename;

exec studentdata;

/* sql view */

create view view_name as


select columnnames
from tablename
where condition;

create view viewcustaddress as


select custid, custname, address, mobno, email
from customer
where address like 'pune';

select * from viewcustaddress;

/* drop view */

drop view viewname;

drop view viewcustaddress;

select * from customer;

You might also like