sql success ch02

142
1 SQL Success Chapter 2 slides Stéphane Faroult

Upload: roughsea

Post on 20-May-2015

202 views

Category:

Education


0 download

DESCRIPTION

SQL course

TRANSCRIPT

Page 1: Sql success ch02

1

SQL Success

Chapter 2 slides

Stéphane Faroult

Page 2: Sql success ch02

2

ALPHA

Page 3: Sql success ch02

3

Don Chamberlin with Ray Boyce (+ 1974)

3

Page 4: Sql success ch02

4

select ...

Page 5: Sql success ch02

5

select ... from ...

Page 6: Sql success ch02

6

select ... from ... where ...

Page 7: Sql success ch02

7

Michael Stonebraker

QUEL

Page 8: Sql success ch02

8

Michael Stonebraker

QUEL

range of e is employee retrieve (comp = e.salary / (e.age - 18)) where e.name = "Jones"

Page 9: Sql success ch02

9

Movies | Movieid | Title | Country | Year_Released -------------------------------------------------------------------- | | | | P. IN > 1985

Moshe Zloof

QBE

Page 10: Sql success ch02

10

Oates Ellison

Miner

Page 11: Sql success ch02

11

components

Page 12: Sql success ch02

12

CREATE

Data Definition Language

Page 13: Sql success ch02

13

ALTER

Data Definition Language

Page 14: Sql success ch02

14

DROP

Data Definition Language

Page 15: Sql success ch02

15

id title country year_released

Data Manipulation Language

INSERT id title country year_released 1 Casablanca us 1942 2 Blade Runner us 1982 3 On The Waterfront us 1954 4 Lawrence Of Arabia uk 1962 5 Annie Hall us 1977 6 Goodfellas us 1990

Page 16: Sql success ch02

16

id title country year_released

Data Manipulation Language

INSERT id title country year_released 1 Casablanca us 1942 2 Blade Runner us 1982 3 On The Waterfront us 1954 4 Lawrence Of Arabia uk 1962 5 Annie Hall us 1977 6 Goodfellas us 1990 7 The Third Man uk 1949 8 Citizen Kane us 1941 9 Bicycle Thieves it 1948 10 The Battleship Potemkin ru 1925 11 Sholay in 1975 12 A Better Tomorrow hk 1986

Page 17: Sql success ch02

17

id title country year_released 1 Casablanca us 1942 2 Blade Runner us 1982 3 On The Waterfront us 1954 4 Lawrence Of Arabia uk 1962 5 Annie Hall us 1977 6 Goodfellas us 1990 7 The Third Man uk 1949 8 Citizen Kane us 1941 9 Bicycle Thieves it 1948 10 The Battleship Potemkin ru 1925 11 Sholay in 1975 12 A Better Tomorrow hk 1986

Data Manipulation Language

UPDATE id title country year_released 1 Casablanca us 1942 2 Blade Runner us 1982 3 On The Waterfront us 1954 4 Lawrence Of Arabia gb 1962 5 Annie Hall us 1977 6 Goodfellas us 1990 7 The Third Man gb 1949 8 Citizen Kane us 1941 9 Bicycle Thieves it 1948 10 The Battleship Potemkin ru 1925 11 Sholay in 1975 12 A Better Tomorrow hk 1986

Page 18: Sql success ch02

18

DELETE id title country year_released 1 Casablanca us 1942 2 Blade Runner us 1982 3 On The Waterfront us 1954 4 Lawrence Of Arabia gb 1962 5 Annie Hall us 1977 6 Goodfellas us 1990 7 The Third Man gb 1949 8 Citizen Kane us 1941 9 Bicycle Thieves it 1948 10 The Battleship Potemkin ru 1925 11 Sholay in 1975 12 A Better Tomorrow hk 1986

Data Manipulation Language

id title country year_released 1 Casablanca us 1942 2 Blade Runner us 1982 3 On The Waterfront us 1954 4 Lawrence Of Arabia gb 1962 6 Goodfellas us 1990 7 The Third Man gb 1949 8 Citizen Kane us 1941 9 Bicycle Thieves it 1948 10 The Battleship Potemkin ru 1925 11 Sholay in 1975 12 A Better Tomorrow hk 1986

Page 19: Sql success ch02

19

id title country year_released 1 Casablanca us 1942 2 Blade Runner us 1982 3 On The Waterfront us 1954 4 Lawrence Of Arabia gb 1962 6 Goodfellas us 1990 7 The Third Man gb 1949 8 Citizen Kane us 1941 9 Bicycle Thieves it 1948 10 The Battleship Potemkin ru 1925 11 Sholay in 1975 12 A Better Tomorrow hk 1986

SELECT

Page 20: Sql success ch02

20

Page 21: Sql success ch02

21

EMEA Survey, Evans Data, 2011

Page 22: Sql success ch02

22

EMEA Survey, Evans Data, 2011

Page 23: Sql success ch02

23

EMEA Survey, Evans Data, 2011

Page 24: Sql success ch02

24

Page 25: Sql success ch02

25

Page 26: Sql success ch02

26

Page 27: Sql success ch02

27

Page 28: Sql success ch02

28

Page 29: Sql success ch02

29

Flickr: Jeff Kramer

Page 30: Sql success ch02

30

DBMS

Client

Page 31: Sql success ch02

31

DBMS

Client

Page 32: Sql success ch02

32

DBMS

Page 33: Sql success ch02

33

DBMS

Page 34: Sql success ch02

34

DBMS

Page 35: Sql success ch02

35

DBMS

Page 36: Sql success ch02

36

Client

Page 37: Sql success ch02

37

FTP ECHO SSH 7 21 22

23 25

88

80

110 143

Page 38: Sql success ch02

38

FTP ECHO SSH 7 21 22

23 25

88

80

110 143

Page 39: Sql success ch02

39

FTP ECHO SSH 7 21 22

23 25

88

80

110 143

Page 40: Sql success ch02

40

FTP ECHO SSH 7 21 22

23 25

88

80

110 143

Page 41: Sql success ch02

41

$ mysql --user=donald --host=atlas --port=3306

41

Page 42: Sql success ch02

42

$ psql --username donald --host atlas --port 5432

$ mysql --user=donald --host=atlas --port=3306

42

Page 43: Sql success ch02

43

$ psql --username donald --host atlas --port 5432

$ mysql --user=donald --host=atlas --port=3306

$ sqlplus donald@//atlas:1521/ORCL

43

Page 44: Sql success ch02

44

$ clpplus donald@//atlas:50000/DB2

$ psql --username donald --host atlas --port 5432

$ mysql --user=donald --host=atlas --port=3306

$ sqlplus donald@//atlas:1521/ORCL

44

Page 45: Sql success ch02

45

$ clpplus donald@//atlas:50000/DB2

$ psql --username donald --host atlas --port 5432

ATLAS\SQLEXPRESS

Database Engine

SQL Server Authentication

$ mysql --user=donald --host=atlas --port=3306

$ sqlplus donald@//atlas:1521/ORCL

45

donald

Page 46: Sql success ch02

46

create table table_name

Page 47: Sql success ch02

47

( , , )

create table table_name

Page 48: Sql success ch02

48

cReaTE tABle

( , , )

table_name

Page 49: Sql success ch02

49

tablename

TABLENAME

tABleNamE

=

=

Page 50: Sql success ch02

50

4ME

Page 51: Sql success ch02

51

4ME

Page 52: Sql success ch02

52

MY_TABLE2

4ME

Page 53: Sql success ch02

53

MY_TABLE2

MY TABLE

4ME

Page 54: Sql success ch02

54

MY_TABLE2

MY TABLE

4ME

Page 55: Sql success ch02

55

( , , )

create table table_name column_name

Page 56: Sql success ch02

56

( , , )

datatype create table table_name

column_name

Page 57: Sql success ch02

57

Text

Page 58: Sql success ch02

58

Text

Number

Page 59: Sql success ch02

59

Text

Number

Date

Page 60: Sql success ch02

60

Text

Number

Date

Binary

Page 61: Sql success ch02

61

Text

Number

Date

Binary

Page 62: Sql success ch02

62

Text datatypes

char(length)

char

Page 63: Sql success ch02

63

Text datatypes

char(length)

char(1)

Page 64: Sql success ch02

64

Text datatypes

char(length)

varchar(max length)

varchar2(max length)

Page 65: Sql success ch02

65

Text datatypes

char(length)

varchar(max length)

clob

Page 66: Sql success ch02

66

Number datatypes

int

Page 67: Sql success ch02

67

Number datatypes

int

float

Page 68: Sql success ch02

68

numeric

Number datatypes

int

float

number

Page 69: Sql success ch02

69

numeric (precision)

Number datatypes

int

float

Page 70: Sql success ch02

70

999999 99

numeric (precision)

Number datatypes

int

float

Page 71: Sql success ch02

71

999999 99

numeric

Number datatypes

int

float

(precision, scale)

Page 72: Sql success ch02

72

999999. 99

numeric

Number datatypes

int

float

(precision, scale)

Page 73: Sql success ch02

73

Date datatypes

datetime

date

Page 74: Sql success ch02

74

Date datatypes

datetime

timestamp

Page 75: Sql success ch02

75

varbinary(max length)

Binary datatypes

Page 76: Sql success ch02

76

varbinary(max length)

Binary datatypes

blob

Page 77: Sql success ch02

77

create table people (

)

peopleid number, first_name varchar2(30), surname varchar2(30), born number(4),

died number(4)

Page 78: Sql success ch02

78

create table people (

)

peopleid int, first_name varchar(30), surname varchar(30), born numeric(4),

died numeric(4)

Page 79: Sql success ch02

79 Flickr:Daniel Moyle

Page 80: Sql success ch02

80

create table people (peopleid int first_name varchar(30), surname varchar(30) born numeric(4),

died numeric(4))

,

,

Page 81: Sql success ch02

81

create table people (peopleid int first_name varchar(30), surname varchar(30) born numeric(4),

died numeric(4))

,

not null,

Page 82: Sql success ch02

82

create table people (peopleid int first_name varchar(30), surname varchar(30) born numeric(4),

died numeric(4))

not null,

not null,

Page 83: Sql success ch02

83

create table people (peopleid int first_name varchar(30), surname varchar(30) born numeric(4),

died numeric(4))

not null,

not null,

Page 84: Sql success ch02

84

create table people (peopleid int first_name varchar(30), surname varchar(30) born numeric(4),

died numeric(4))

not null,

not null,

Page 85: Sql success ch02

85

Page 86: Sql success ch02

86

Page 87: Sql success ch02

87

comment on column people.surname is 'Surname or stage name';

-- comments in an SQL statement start with a double dash

Page 88: Sql success ch02

88

comment on column people.surname is 'Surname or stage name';

-- comments in an SQL statement start with a double dash

Page 89: Sql success ch02

89

comment on column people.surname is 'Surname or stage name';

-- comments in an SQL statement start with a double dash

Page 90: Sql success ch02

90

create table people (peopleid int not null, first_name varchar(30), surname varchar(30) not null, born numeric(4),

died numeric(4))

45

Page 91: Sql success ch02

91

No woman should ever be quite accurate about her age. It looks so calculating.

Oscar Wilde The Importance of Being Earnest.

Page 92: Sql success ch02

92

create table people (peopleid int not null, first_name varchar(30), surname varchar(30) not null, born numeric(4)

died numeric(4)) not null, ,

Page 93: Sql success ch02

93

Hepburn

Surname

Audrey

Firstname

4-May-1929

Birthdate Picture

Page 94: Sql success ch02

94

Hepburn

Surname

Audrey

Firstname

4-May-1929

Birthdate Picture

Hepburn Audrey 4-May-1929

Page 95: Sql success ch02

95

Hepburn

Surname

Audrey

Firstname

4-May-1929

Birthdate Picture

Hepburn Audrey 4-May-1929

Page 96: Sql success ch02

96

Page 97: Sql success ch02

97

Page 98: Sql success ch02

98

CONSTRAINTS

Page 99: Sql success ch02

99

first_name varchar(30), surname varchar(30) not null, born numeric(4) not null, died numeric(4))

create table people (peopleid int not null ,

Page 100: Sql success ch02

100

first_name varchar(30), surname varchar(30) not null, born numeric(4) not null, died numeric(4))

primary key, create table people (peopleid int not null ,

Page 101: Sql success ch02

101

create table people (peopleid int not null primary key, first_name varchar(30), surname varchar(30) not null, born numeric(4) not null, died numeric(4) )

Page 102: Sql success ch02

102

, unique (first_name, surname))

create table people (peopleid int not null primary key, first_name varchar(30), surname varchar(30) not null, born numeric(4) not null, died numeric(4)

Page 103: Sql success ch02

103

'audrey'

'AUDREY'

'Audrey'

For Oracle, PostgreSQL and DB2 ...

Page 104: Sql success ch02

104

'audrey'

'AUDREY'

'Audrey'

For Oracle, PostgreSQL and DB2 ...

... not for SQL Server, MySQL or SQLite ...

Page 105: Sql success ch02

105

create table people (peopleid int not null primary key, first_name varchar(30),

surname varchar(30) not null, born numeric(4) not null, died numeric(4), unique (first_name, surname))

Page 106: Sql success ch02

106

create table people (peopleid int not null primary key, first_name varchar(30) surname varchar(30) not null born numeric(4) not null, died numeric(4), unique (first_name, surname))

, check (first_name = upper(first_name)),

,

check (surname = upper(surname)),

Page 107: Sql success ch02

107

Chris Date

Picture: Douglas Robertson

Page 108: Sql success ch02

108

Chris Date

Picture: Douglas Robertson

Page 109: Sql success ch02

109

create table movies (movieid int not null primary key, title varchar(60) not null, country char(2) not null, year_released numeric(4) not null check(year_released >= 1895), unique (title, country, year_released))

Page 110: Sql success ch02

110

COUNTRIES country_code country_name continent

United States us AMERICA

China cn ASIA

United Kingdom uk EUROPE

Brazil br AMERICA

India in ASIA

Italy it EUROPE

Mexico mx AMERICA

Russia ru EUROPE

Germany de EUROPE

Page 111: Sql success ch02

111

COUNTRIES country_code country_name continent

United States us AMERICA

China cn ASIA

United Kingdom uk EUROPE

Brazil br AMERICA

India in ASIA

Italy it EUROPE

Mexico mx AMERICA

Russia ru EUROPE

Germany de EUROPE

Page 112: Sql success ch02

112

create table movies (movieid int not null primary key, title varchar(60) not null, country char(2) not null, year_released numeric(4) not null check(year_released >= 1895), unique (title, country, year_released)

foreign key(country) references countries(country_code))

Page 113: Sql success ch02

113

credits

Page 114: Sql success ch02

114

movies

credits

Page 115: Sql success ch02

115

movies people

credits

Page 116: Sql success ch02

116

movies people

credits

Page 117: Sql success ch02

117

Page 118: Sql success ch02

118

PROGRAM

Page 119: Sql success ch02

119

PROGRAM

Page 120: Sql success ch02

120

Prepare the background

Page 121: Sql success ch02

121

INSERT SOME_TABLE column1 column2

Page 122: Sql success ch02

122

INSERT SOME_TABLE column1 column2

Page 123: Sql success ch02

123

INSERT SOME_TABLE column1 column2

Page 124: Sql success ch02

124

insert into table_name (list of columns) values (list of values)

Page 125: Sql success ch02

125

insert into countries(country_code, country_name, continent) values('us', 'United States', 'AMERICA')

Page 126: Sql success ch02

126

Hello World

Reminder

Page 127: Sql success ch02

127

HELLO WORLD

Reminder

Page 128: Sql success ch02

128

select * from countries

Page 129: Sql success ch02

129

select * from countries

Page 130: Sql success ch02

130

1960

Page 131: Sql success ch02

131

insert into movies(movieid, title, country, year_released) values (123, 'L''Avventura', 'it', 1960)

Page 132: Sql success ch02

132

insert into movies(movieid, title, country, year_released) values (123, 'L''Avventura', 'it', 1960)

''

Page 133: Sql success ch02

133 Flickr: Jason Dgreat

Entering

a date?

Page 134: Sql success ch02

134

'07/20/1969'

'MM/DD/YYYY'

to_date( ) ,

Page 135: Sql success ch02

135

SYSDATE

Page 136: Sql success ch02

136

CURDATE()

SYSDATE

Page 137: Sql success ch02

137

SYSDATE

CURDATE()

GETDATE()

Page 138: Sql success ch02

138 FLickr:Steve Jurvetson

Page 139: Sql success ch02

139

SQL : THE database language.

Page 140: Sql success ch02

140

SQL : THE database language. Connection : server, port, database, username, password.

Page 141: Sql success ch02

141

SQL : THE database language. Connection : server, port, database, username, password. Create table: data types + constraints.

Page 142: Sql success ch02

142

SQL : THE database language. Connection : server, port, database, username, password. Create table: data types + constraints.

Keywords and identifiers: not case-sensitive Data : can be CASE-SENSITIVE