You are on page 1of 43

1

2
3
Integer:
4
Bit - 1 bit
Tinyint - 1 byte
Smallint - 2 bytes
Int - 4 bytes
Bigint - 8 bytes
Float:
Float
Real
Text:
Non unicode string: A character occupies 1 byte
Char
Varchar
Text
Unicode string: A character occupies 2 bytes
Nchar
Nvarchar
5
6
7
8
9
create table Student
10
(
sid int,
sname varchar(20)
)
Drop table student;
11
12
13
14
15
16
17
18
19
20
21
Add new column:
22
Alter table test add grade char(1);
Modify a column data type:
Alter table test alter column grade varchar(10);
Delete a column:
Alter table test drop column grade;
A table can have only one clustered index and any number of non clustered
23
index (upto 249)
Unique index When a unique index exists, the Database Engine checks for
duplicate values each time data is added by a insert operations. Insert
operations that would generate duplicate key values are rolled back, and the
Database Engine displays an error message.
Clustered index - clustered index can be rebuilt or reorganized on demand to
control table fragmentation. A clustered index can also be created on a view.
This improves the performance.
Non clustered index - Creates an index that specifies the logical ordering of a
table. With a nonclustered index, the physical order of the data rows is
independent of their indexed order.
24
25
26
insert into Student values(1,'Ramu')
27
insert into Student(sid,sname) values(6,'Raj')
insert into Student(sid) values(2)
insert into Student(sname) values('Seetha')
28
update student
29
set sid=3
This will set sid =3 for all students
update student
set sid=1
where sname='Ramu
This will set sid as 1 only for Ramu
update student
30
set sid=3
This will set sid =3 for all students
update student
set sid=1
where sname='Ramu
This will set sid as 1 only for Ramu
delete from student
31
where sid between 1 and 3
This will delete students with sid 1,2,3
To execute a statement in MS SQL, Select the statement and Click on the
32
Execute button in the query analyser or press F5
33
The TOP clause can be very useful on large tables with thousands of records.
34
Returning a large number of records can impact on performance.
35
36
To select distinct rows, we need to use the distinct key word
37
Select distinct name from orders;
Orders
--------
Id Name
-- -------
1 Ram
2 Krish
3 Ram
4 Raj
Will fetch
Ram
Krish
Raj
Select count(name) from orders; will yield the result as 4
Related rows can be grouped together by GROUP BY clause by specifying a
38
column as a grouping column.
GROUP BY is associated with an aggregate function
Example: For each part supplied get the part number and the total shipment
quantity
Example:
SELECT PNO, SUM(QTY) FROM SP GROUP BY PNO
39
40
41
42
43

You might also like