Day02 Lesson02 SQL Basics

43
1

Transcript of Day02 Lesson02 SQL Basics

Page 1: Day02 Lesson02 SQL Basics

1

Page 2: Day02 Lesson02 SQL Basics

2

Page 3: Day02 Lesson02 SQL Basics

3

Page 4: Day02 Lesson02 SQL Basics

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

Page 5: Day02 Lesson02 SQL Basics

5

Page 6: Day02 Lesson02 SQL Basics

6

Page 7: Day02 Lesson02 SQL Basics

7

Page 8: Day02 Lesson02 SQL Basics

8

Page 9: Day02 Lesson02 SQL Basics

9

Page 10: Day02 Lesson02 SQL Basics

create table Student

10

(

sid int,

sname varchar(20)

)

Drop table student;

Page 11: Day02 Lesson02 SQL Basics

11

Page 12: Day02 Lesson02 SQL Basics

12

Page 13: Day02 Lesson02 SQL Basics

13

Page 14: Day02 Lesson02 SQL Basics

14

Page 15: Day02 Lesson02 SQL Basics

15

Page 16: Day02 Lesson02 SQL Basics

16

Page 17: Day02 Lesson02 SQL Basics

17

Page 18: Day02 Lesson02 SQL Basics

18

Page 19: Day02 Lesson02 SQL Basics

19

Page 20: Day02 Lesson02 SQL Basics

20

Page 21: Day02 Lesson02 SQL Basics

21

Page 22: Day02 Lesson02 SQL Basics

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;

Page 23: Day02 Lesson02 SQL Basics

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.

Page 24: Day02 Lesson02 SQL Basics

24

Page 25: Day02 Lesson02 SQL Basics

25

Page 26: Day02 Lesson02 SQL Basics

26

Page 27: Day02 Lesson02 SQL Basics

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')

Page 28: Day02 Lesson02 SQL Basics

28

Page 29: Day02 Lesson02 SQL Basics

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

Page 30: Day02 Lesson02 SQL Basics

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

Page 31: Day02 Lesson02 SQL Basics

delete from student

31

where sid between 1 and 3

This will delete students with sid 1,2,3

Page 32: Day02 Lesson02 SQL Basics

To execute a statement in MS SQL, Select the statement and Click on the

32

Execute button in the query analyser or press F5

Page 33: Day02 Lesson02 SQL Basics

33

Page 34: Day02 Lesson02 SQL Basics

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.

Page 35: Day02 Lesson02 SQL Basics

35

Page 36: Day02 Lesson02 SQL Basics

36

Page 37: Day02 Lesson02 SQL Basics

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

Page 38: Day02 Lesson02 SQL Basics

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

Page 39: Day02 Lesson02 SQL Basics

39

Page 40: Day02 Lesson02 SQL Basics

40

Page 41: Day02 Lesson02 SQL Basics

41

Page 42: Day02 Lesson02 SQL Basics

42

Page 43: Day02 Lesson02 SQL Basics

43