What Is SQL?: SQL Is A Standard Language For Accessing and Manipulating Databases
What Is SQL?: SQL Is A Standard Language For Accessing and Manipulating Databases
What is SQL?
SQL stands for Structured Query Language SQL lets you access and manipulate databases SQL is an ANSI (American National Standards Institute) standard
What
an SQL do?
SQL can e!ecute "ueries against a database SQL can retrie#e data from a database SQL can insert records in a database
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
SQL can update records in a database SQL can delete records from a database SQL can create ne$ databases SQL can create ne$ tables in a database SQL can create stored procedures in a database SQL can create #ie$s in a database SQL can set permissions on tables% procedures% and #ie$s
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
/.'0S
/.'0S stands for /elational .atabase 0anagement System. /.'0S is the basis for SQL% and for all modern database systems li4e 0S SQL Ser#er% I'0 .'6% 7racle% 0ySQL% and 0icrosoft Access. )he data in /.'0S is stored in database ob+ects called tables. A table is a collections of related data entries and it consists of columns and ro$s.
.atabase )ables
A database most often contains one or more tables. ,ach table is identified by a name (e.g. 8 ustomers8 or 87rders8). )ables contain records (ro$s) $ith data. 'elo$ is an e!ample of a table called 8-ersons83
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
SQL Statements
S,L, ) ? >/70 -ersons
0ost of the actions you need to perform on a database are done $ith SQL statements. )he follo$ing SQL statement $ill select all the records in the 8-ersons8 table3 In this tutorial $e $ill teach you all about the different SQL statements.
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
Some database systems re"uire a semicolon at the end of each SQL statement. Semicolon is the standard $ay to separate each SQL statement in database systems that allo$ more than one SQL statement to be e!ecuted in the same call to the ser#er. We are using 0S Access and SQL Ser#er 6::: and $e do not ha#e to put a semicolon after each SQL statement% but some database programs force you to use it.
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
ALTER DATABASE & modifies a database CREATE TABLE & creates a ne$ table ALTER TABLE & modifies a table DROP TABLE & deletes a table CREATE INDEX & creates an inde! (search 4ey) DROP INDEX & deletes an inde!
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
o
@ @
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
o
@
10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
@ @
No$ $e $ant to select the content of the columns named 8LastName8 and 8>irstName8 from the table abo#e. We use the follo$ing S,L, ) statement3 S,L, ) LastName%>irstName >/70 -ersons
11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
o
@
S,L, ) ? ,!ample
No$ $e $ant to select all the columns from the 8-ersons8 table.
12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12
@ @
Tip: )he asteris4 (?) is a "uic4 $ay of selecting all columns1 )he result&set $ill loo4 li4e this3 P_Id 9 6 ; LastName *ansen S#endson -ettersen FirstName 7la )o#e <ari Address )imotei#n 9: 'org#n 6; Storgt 6: City Sandnes Sandnes Sta#anger
13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13
o
@ @
Na#igation in a /esult&set
0ost database soft$are systems allo$ na#igation in the result&set $ith programming functions% li4e3 0o#e&)o&>irst&/ecord% Aet&/ecord& ontent% 0o#e&)o&Ne!t&/ecord% etc. -rogramming functions li4e these are not a part of this tutorial. )o learn about accessing data $ith function calls% please #isit our A.7 tutorial or our -*- tutorial.
14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14
15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15
No$ $e $ant to select only the distinct #alues from the column named 8 ity8 from the table abo#e. We use the follo$ing S,L, ) statement3
16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16
)he W*,/,
lause
)he W*,/, clause is used to e!tract only those records that fulfill a specified criterion.
17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL WHERE Syntax
S,L, ) column=name(s) >/70 table=name W*,/, column=name operator #alue
W*,/,
lause ,!ample
18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18
No$ $e $ant to select only the persons li#ing in the city 8Sandnes8 from the table abo#e. We use the follo$ing S,L, ) statement3 S,L, ) ? >/70 -ersons W*,/, ityBCSandnesC )he result&set $ill loo4 li4e this3
19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21
lause
22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
)he AN. I 7/ operators are used to filter records based on more than one condition.
)he AN. operator displays a record if both the first condition and the second condition is true. )he 7/ operator displays a record if either the first condition or the second condition is true.
FirstName 7la
Address )imotei#n 9:
City Sandnes
24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24
25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25
7/ 7perator ,!ample
No$ $e $ant to select only the persons $ith the first name e"ual to 8)o#e8 7/ the first name e"ual to 87la83 We use the follo$ing S,L, ) statement3 S,L, ) ? >/70 -ersons W*,/, >irstNameBC)o#eC 7/ >irstNameBC7laC )he result&set $ill loo4 li4e this3 P_Id LastName 9 *ansen FirstName 7la Address )imotei#n 9: City Sandnes
26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26
2ou can also combine AN. and 7/ (use parenthesis to form comple! e!pressions). No$ $e $ant to select only the persons $ith the last name e"ual to 8S#endson8 AN. the first name e"ual to 8)o#e8 7/ to 87la83 We use the follo$ing S,L, ) statement3 S,L, ) ? >/70 -ersons W*,/, LastNameBCS#endsonC AN. (>irstNameBC)o#eC 7/ >irstNameBC7laC) )he result&set $ill loo4 li4e this3
ombining AN. I 7/
27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27
28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL ORDER BY Syntax
S,L, ) column=name(s) >/70 table=name 7/.,/ '2 column=name(s) AS J.,S
29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29
No$ $e $ant to select all the persons from the table abo#e% ho$e#er% $e $ant to sort the persons by their last name. We use the follo$ing S,L, ) statement3 S,L, ) ? >/70 -ersons 7/.,/ '2 LastName
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31
,!ample
No$ $e $ant to select all the persons from the table abo#e% ho$e#er% $e $ant to sort the persons descending by their last name. We use the follo$ing S,L, ) statement3 S,L, ) ? >/70 -ersons 7/.,/ '2 LastName .,S )he result&set $ill loo4 li4e this3 P_Id LastName FirstName Address City
32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL INSERT INTO Syntax
It is possible to $rite the INS,/) IN)7 statement in t$o forms. )he first form doesnCt specify the column names $here the data $ill be inserted% only their #alues3 INS,/) IN)7 table=name LAL(,S (#alue9% #alue6% #alue;%...) )he second form specifies both the column names and the #alues to be inserted3 INS,/) IN)7 table=name (column9% column6% column;%...) LAL(,S (#alue9% #alue6% #alue;%...)
34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34
35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35
36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36
olumns
It is also possible to only add data in specific columns. )he follo$ing SQL statement $ill add a ne$ ro$% but only add data in the 8-=Id8% 8LastName8 and the 8>irstName8 columns3 INS,/) IN)7 -ersons (-=Id% LastName% >irstName) LAL(,S (F% C)+essemC% CMa4obC) )he 8-ersons8 table $ill no$ loo4 li4e this3
37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37
38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38
39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39
No$ $e $ant to update the person 8)+essem% Ma4ob8 in the 8-ersons8 table.
40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40
41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41
42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42
43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43
44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44
No$ $e $ant to delete the person 8)+essem% Ma4ob8 in the 8-ersons8 table. We use the follo$ing SQL statement3
45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45
46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46
47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL S!L Ad"a ced
)he )7-
lause
)he )7- clause is used to specify the number of records to return. )he )7- clause can be #ery useful on large tables $ith thousands of records. /eturning a large number of records can impact on performance. Note: Not all database systems support the )7- clause.
48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL Server Syntax
S,L, ) )7- numberJpercent column=name(s) >/70 table=name
49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
Example
S,L, ) ?
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50
Oracle Syntax
S,L, ) column=name(s) >/70 table=name W*,/, /7WN(0 GB number
51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51
52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52
No$ $e $ant to select only the t$o first records in the table abo#e. We use the follo$ing S,L, ) statement3 S,L, ) )7- 6 ? >/70 -ersons )he result&set $ill loo4 li4e this3
53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53
54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54
No$ $e $ant to select only F:O of the records in the table abo#e. We use the follo$ing S,L, ) statement3 S,L, ) )7- F: -,/ ,N) ? >/70 -ersons )he result&set $ill loo4 li4e this3 P_Id 9 6 LastName *ansen S#endson FirstName 7la )o#e Address )imotei#n 9: 'org#n 6; City Sandnes Sandnes
55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL )he LI<, operator is used in a W*,/, clause to search for a specified pattern in a column.
56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56
57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57
58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58
Ne!t% $e $ant to select the persons li#ing in a city that ends $ith an 8s8 from the 8-ersons8 table. We use the follo$ing S,L, ) statement3 S,L, ) ? >/70 -ersons W*,/, ity LI<, COsC )he result&set $ill loo4 li4e this3 P_Id 9 LastName *ansen FirstName 7la Address )imotei#n 9: City Sandnes
59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59
60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60
61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL $ildcards can be used $hen searching for data in a database.
SQL Wildcards
SQL $ildcards can substitute for one or more characters $hen searching for data in a database. SQL $ildcards must be used $ith the SQL LI<, operator. With SQL% the follo$ing $ildcards can be used3 #i$dcard Descriptio O A substitute for Pero or more characters = A substitute for e!actly one character QcharlistR Any single character in charlist QScharlistR Any single character not in charlist or
62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62 62
64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64
65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65 65
66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66
67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67
68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68
)he IN 7perator
)he IN operator allo$s you to specify multiple #alues in a W*,/, clause.
SQL IN Syntax
S,L, ) column=name(s) >/70 table=name W*,/, column=name IN (#alue9%#alue6%...)
69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69
IN 7perator ,!ample
)he 8-ersons8 table3 P_Id 9 6 ; LastName *ansen S#endson -ettersen FirstName 7la )o#e <ari Address )imotei#n 9: 'org#n 6; Storgt 6: City Sandnes Sandnes Sta#anger
No$ $e $ant to select the persons $ith a last name e"ual to 8*ansen8 or 8-ettersen8 from the table abo#e. We use the follo$ing S,L, ) statement3
70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70
)he ',)W,,N operator is used in a W*,/, clause to select a range of data bet$een t$o #alues. 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71
72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72
73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73
74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74
75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75
,!ample 6
)o display the persons outside the range in the pre#ious e!ample% use N7) ',)W,,N3 S,L, ) ? >/70 -ersons W*,/, LastName N7) ',)W,,N C*ansenC AN. C-ettersenC )he result&set $ill loo4 li4e this3 P_Id 6 ; LastName S#endson -ettersen FirstName )o#e <ari Address 'org#n 6; Storgt 6: City Sandnes Sta#anger
76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL With SQL% an alias name can be gi#en to a table or to a column.
SQL Alias
2ou can gi#e a table or a column another name by using an alias. )his can be a good thing to do if you ha#e #ery long or comple! table names or column names. An alias name could be anything% but usually it is short.
77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77
78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78
Alias ,!ample
Assume $e ha#e a table called 8-ersons8 and another table called 8-roduct=7rders8. We $ill gi#e the table aliases of 8p8 and 8po8 respecti#ely. No$ $e $ant to list all the orders that 87la *ansen8 is responsible for. We use the follo$ing S,L, ) statement3 S,L, ) po.7rderI.% p.LastName% p.>irstName >/70 -ersons AS p% -roduct=7rders AS po W*,/, p.LastNameBC*ansenC AN. p.>irstNameBC7laC
79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79
80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80
81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81
82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82
%OIN3 /eturn ro$s $hen there is at least one match in both tables LEFT %OIN3 /eturn all ro$s from the left table% e#en if there are no matches in the right table RI&'T %OIN3 /eturn all ro$s from the right table% e#en if there are no matches in the left table FULL %OIN3 /eturn ro$s $hen there is a match in one of the tables
83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL INNER 'OIN Syntax
S,L, ) column=name(s) >/70 table=name9 INN,/ M7IN table=name6 7N table=name9.column=nameBtable=name6.column=name PS: INN,/ M7IN is the same as M7IN.
84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84
85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85
No$ $e $ant to list all the persons $ith any orders. We use the follo$ing S,L, ) statement3 S,L, ) -ersons.LastName% -ersons.>irstName% 7rders.7rderNo >/70 -ersons INN,/ M7IN 7rders 7N -ersons.-=IdB7rders.-=Id
86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86
)he INN,/ M7IN 4ey$ord return ro$s $hen there is at least one match in both tables. If there are ro$s in 8-ersons8 that do not ha#e matches in 87rders8% those ro$s $ill N7) be listed.
87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87
88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88
89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89
No$ $e $ant to list all the persons and their orders & if any% from the tables abo#e. We use the follo$ing S,L, ) statement3 S,L, ) -ersons.LastName% -ersons.>irstName% 7rders.7rderNo >/70 -ersons
90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90
91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91
92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92
93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93
No$ $e $ant to list all the orders $ith containing persons & if any% from the tables abo#e. We use the follo$ing S,L, ) statement3
94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94
95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95
)he /IA*) M7IN 4ey$ord returns all the ro$s from the right table (7rders)% e#en if there are no matches in the left table (-ersons).
96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL (ULL 'OIN Syntax
S,L, ) column=name(s) >/70 table=name9 >(LL M7IN table=name6 7N table=name9.column=nameBtable=name6.column=name
97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97
98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98
99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99 99
)he SQL (NI7N operator combines t$o or more S,L, ) statements. 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101
102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102
103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103 103
No$ $e $ant to list a$$ t*e di++ere t employees in Nor$ay and (SA. We use the follo$ing S,L, ) statement3
104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104 104
105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105 105
106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106 106
107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107
)he SQL S,L, ) IN)7 statement can be used to create bac4up copies of tables.
108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL SELECT INTO Syntax
We can select all columns into the ne$ table3 S,L, ) ? IN)7 ne$=table=name QIN e!ternaldatabaseR >/70 old=tablename 7r $e can select only the columns $e $ant into the ne$ table3 S,L, ) column=name(s) IN)7 ne$=table=name QIN e!ternaldatabaseR
109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109
110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110 110
111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111
lause
)he follo$ing SQL statement creates a 8-ersons='ac4up8 table $ith only the persons $ho li#es in the city 8Sandnes83 S,L, ) LastName%>irstname
112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112 112
113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113
)he
)he
114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL CREATE DATABASE Syntax
/,A), .A)A'AS, database=name
115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115 115
)he
)he
116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116 116
117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117 117
118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118 118
)he empty table can be filled $ith data $ith the INS,/) IN)7 statement.
119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119 119
SQL
onstraints are used to limit the type of data that can go into a table. onstraints can be specified $hen a table is created ($ith the /,A), )A'L, statement) or after the table is created ($ith the AL),/ )A'L, statement). We $ill focus on the follo$ing constraints3 N7) N(LL (NIQ(, -/I0A/2 <,2 >7/,IAN <,2 *, <
onstraints
120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120 120
onstraint
)he N7) N(LL constraint enforces a column to N7) accept N(LL #alues. )he N7) N(LL constraint enforces a field to al$ays contain a #alue. )his means that you cannot insert a ne$ record% or update a record $ithout adding a #alue to this field. )he follo$ing SQL enforces the 8-=Id8 column and the 8LastName8 column to not accept N(LL #alues3 /,A), )A'L, -ersons ( -=Id int N7) N(LL%
121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121
SQL (NIQ(,
onstraint
)he (NIQ(, constraint uni"uely identifies each record in a database table. )he (NIQ(, and -/I0A/2 <,2 constraints both pro#ide a guarantee for uni"ueness for a column or set of columns. A -/I0A/2 <,2 constraint automatically has a (NIQ(, constraint defined on it.
122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122
SQL (NIQ(,
onstraint on
/,A), )A'L,
)he follo$ing SQL creates a (NIQ(, constraint on the 8-=Id8 column $hen the 8-ersons8 table is created3 -yS!L: /,A), )A'L, -ersons ( -=Id int N7) N(LL% LastName #archar(6FF) N7) N(LL% >irstName #archar(6FF)%
123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123
124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124 124
125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125
SQL (NIQ(,
)o create a (NIQ(, constraint on the 8-=Id8 column $hen the table is already created% use the follo$ing SQL3 -yS!L / S!L Ser"er / Orac$e / -S Access: AL),/ )A'L, -ersons A.. (NIQ(, (-=Id)
126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126 126
)o ./7- a (NIQ(,
AL),/ )A'L, -ersons
onstraint
127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127
)he -/I0A/2 <,2 constraint uni"uely identifies each record in a database table. -rimary 4eys must contain uni"ue #alues. A primary 4ey column cannot contain N(LL #alues. ,ach table should ha#e a primary 4ey% and each table can ha#e only 7N, primary 4ey.
onstraint
onstraint on
/,A), )A'L,
128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128
129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129
130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130 130
132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132
onstraint
133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133 133
A >7/,IAN <,2 in one table points to a -/I0A/2 <,2 in another table. LetCs illustrate the foreign 4ey $ith an e!ample. Loo4 at the follo$ing t$o tables3 )he 8-ersons8 table3 P_Id LastName FirstName Address City 9 *ansen 7la )imotei#n 9: Sandnes 6 S#endson )o#e 'org#n 6; Sandnes ; -ettersen <ari Storgt 6: Sta#anger )he 87rders8 table3 O_Id OrderNo P_Id 9 NNUDF ;
onstraint
134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134 134
)he follo$ing SQL creates a >7/,IAN <,2 on the 8-=Id8 column $hen the 87rders8 table is created3 -yS!L:
onstraint on
/,A), )A'L,
135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135
136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136 136
137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137
)o create a >7/,IAN <,2 constraint on the 8-=Id8 column $hen the 87rders8 table is already created% use the follo$ing SQL3 -yS!L / S!L Ser"er / Orac$e / -S Access:
138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138 138
139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139 139
onstraint
SQL
)he
*, <
onstraint
140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140
*, < constraint is used to limit the #alue range that can be placed in a column.
SQL
)he follo$ing SQL creates a *, < constraint on the 8-=Id8 column $hen the 8-ersons8 table is created. )he *, < constraint specifies that the column 8-=Id8 must only include integers greater than :. -y S!L: /,A), )A'L, -ersons ( -=Id int N7) N(LL%
*, <
onstraint on
/,A), )A'L,
141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141 141
142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142 142
143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143 143
ityBCSandnesC)
SQL
)o create a *, < constraint on the 8-=Id8 column $hen the table is already created% use the follo$ing SQL3 -yS!L / S!L Ser"er / Orac$e / -S Access:
*, <
144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144 144
ityBCSandnesC)
)o ./7- a
)o drop a
*, <
onstraint
145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145 145
)he .,>A(L) constraint is used to insert a default #alue into a column. )he default #alue $ill be added to all ne$ records% if no other #alue is specified. )he follo$ing SQL creates a .,>A(L) constraint on the 8 ity8 column $hen the 8-ersons8 table is created3 -y S!L / S!L Ser"er / Orac$e / -S Access:
146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146 146
147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147 147
SQL .,>A(L)
)o create a .,>A(L) constraint on the 8 ity8 column $hen the table is already created% use the follo$ing SQL3 -yS!L:
148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148 148
)o ./7- a .,>A(L)
onstraint
149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149 149
)he
Inde!es allo$ the database application to find data fastT $ithout reading the $hole table. 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150
Inde!es
An inde! can be created in a table to find data more "uic4ly and efficiently. )he users cannot see the inde!es% they are +ust used to speed up searches5"ueries. Note: (pdating a table $ith inde!es ta4es more time than updating a table $ithout (because the inde!es also need an update). So you should only create inde!es on columns (and tables) that $ill be fre"uently searched against.
151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151 151
152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152 152
153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153 153
Inde!es% tables% and databases can easily be deleted5remo#ed $ith the ./7statement.
154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154 154
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL DROP INDE* Syntax "#r MS Acce!!+
./7- IN.,V inde!=name 7N table=name
155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155 155
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL DROP INDE* Syntax "#r MySQL+
AL),/ )A'L, table=name ./7- IN.,V inde!=name
156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156
157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157 157
158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158 158
159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159 159
160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160 160
161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161 161
162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162 162
./7-
7L(0N ,!ample
Ne!t% $e $ant to delete the column named 8.ate7f'irth8 in the 8-ersons8 table. We use the follo$ing SQL statement3 AL),/ )A'L, -ersons
163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163 163
Auto&increment allo$s a uni"ue number to be generated $hen a ne$ record is inserted into a table.
)he follo$ing SQL statement defines the 8-=Id8 column to be an auto&increment primary 4ey field in the 8-ersons8 table3 /,A), )A'L, -ersons ( -=Id int N7) N(LL A()7=IN /,0,N)% LastName #archar(6FF) N7) N(LL% >irstName #archar(6FF)%
165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165 165
166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166 166
)he follo$ing SQL statement defines the 8-=Id8 column to be an auto&increment primary 4ey field in the 8-ersons8 table3
167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167 167
168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168 168
)he follo$ing SQL statement defines the 8-=Id8 column to be an auto&increment primary 4ey field in the 8-ersons8 table3
169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169 169
170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170 170
In 7racle the code is a little bit more tric4y. 2ou $ill ha#e to create an auto&increment field $ith the se"uence ob+ect (this ob+ect generates a number se"uence).
171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171 171
172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172 172
A #ie$ is a #irtual table. )his chapter sho$s ho$ to create% update% and delete a #ie$.
173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173 173
SQL
In SQL% a #ie$ is a #irtual table based on the result&set of an SQL statement. A #ie$ contains ro$s and columns% +ust li4e a real table. )he fields in a #ie$ are fields from one or more real tables in the database. 2ou can add SQL functions% W*,/,% and M7IN statements to a #ie$ and present the data as if the data $ere coming from one single table.
174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174 174
SQL
If you ha#e the North$ind database you can see that it has se#eral #ie$s installed by default.
175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175 175
176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176 176
177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177 177
178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178
179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL CREATE OR REPLACE .IEW Syntax
/,A), 7/ /,-LA , LI,W #ie$=name AS S,L, ) column=name(s) >/70 table=name W*,/, condition No$ $e $ant to add the 8 ategory8 column to the 8 urrent -roduct List8 #ie$. We $ill update the #ie$ $ith the follo$ing SQL3 /,A), LI,W Q urrent -roduct ListR AS S,L, ) -roductI.%-roductName% ategory
180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180 180
181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181 181
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL DROP .IEW Syntax
./7- LI,W #ie$=name
SQL .ates
)he most difficult part $hen $or4ing $ith dates is to be sure that the format of the date you are trying to insert% matches the format of the date column in the database. As long as your data contains only the date portion% your "ueries $ill $or4 as e!pected. *o$e#er% if a time portion is in#ol#ed% it gets complicated. 'efore tal4ing about the complications of "uerying for dates% $e $ill loo4 at the most important built& in functions for $or4ing $ith dates.
182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182 182
)he follo$ing table lists the most important built&in date functions in 0ySQL3 F, ctio Descriptio NOW() /eturns the current date and time CURDATE() /eturns the current date CURTIME() /eturns the current time DATE() ,!tracts the date part of a date or date5time e!pression EXTRACT() /eturns a single part of a date5time DATE_ADD() Adds a specified time inter#al to a date DATE_SUB() Subtracts a specified time inter#al from a date DATEDIFF() /eturns the number of days bet$een t$o dates
183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183 183
)he follo$ing table lists the most important built&in date functions in SQL Ser#er3 F, ctio Descriptio GETDATE() /eturns the current date and time DATEPART() /eturns a single part of a date5time DATEADD() Adds or subtracts a specified time inter#al from a date DATEDIFF() /eturns the time bet$een t$o dates CONVERT() .isplays date5time data in different formats
184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184 184
-yS!L comes $ith the follo$ing data types for storing a date or a date5time #alue in the database3 .A), & format 2222&00&.. .A),)I0, & format3 2222&00&.. **3003SS )I0,S)A0- & format3 2222&00&.. **3003SS 2,A/ & format 2222 or 22 S!L Ser"er comes $ith the follo$ing data types for storing a date or a date5time #alue in the database3 .A), & format 2222&00&.. .A),)I0, & format3 2222&00&.. **3003SS
185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185 185
S0ALL.A),)I0, & format3 2222&00&.. **3003SS )I0,S)A0- & format3 a uni"ue number Note: )he date types are chosen for a column $hen you create a ne$ table in your database1 >or an o#er#ie$ of all data types a#ailable% go to our complete .ata )ypes reference.
186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186 186
187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187 187
S,L, ) ? >/70 7rders W*,/, 7rder.ateBC6::U&99&99C $e $ill get no result1 )his is because the "uery is loo4ing only for dates $ith no time portion. Tip: If you $ant to 4eep your "ueries simple and easy to maintain% do not allo$ time components in your dates1
N(LL #alues represent missing un4no$n data. 'y default% a table column can hold N(LL #alues.
188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188 188
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL )his chapter $ill e!plain the IS N(LL and IS N7) N(LL operators.
If a column in a table is optional% $e can insert a ne$ record or update an e!isting record $ithout adding a #alue to this column. )his means that the field $ill be sa#ed $ith a N(LL #alue. N(LL #alues are treated differently from other #alues. N(LL is used as a placeholder for un4no$n or inapplicable #alues. Note: It is not possible to compare N(LL and :T they are not e"ui#alent.
189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189
SQL IS N(LL
*o$ do $e select only the records $ith N(LL #alues in the 8Address8 column? We $ill ha#e to use the IS N(LL operator3
190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190 190
*o$ do $e select only the records $ith no N(LL #alues in the 8Address8 column? We $ill ha#e to use the IS N7) N(LL operator3
191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191 191
U itsI Stoc. 9E 6;
192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192 192
193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193
194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194 194
.ata types and ranges for 0icrosoft Access% 0ySQL and SQL Ser#er.
195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195
9 6 K K U U
K bytes
196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196 196
up to 9A' K bytes
197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197 197
In 0ySQL there are three main types 3 te!t% number% and .ate5)ime types. Te1t types: Data type Descriptio *A/(siPe) *olds a fi!ed length string (can contain letters% numbers% and special characters). )he fi!ed siPe is specified in parenthesis. an store up to 6FF characters LA/ *A/(siPe) *olds a #ariable length string (can contain letters% numbers% and special characters). )he ma!imum siPe is specified in parenthesis. an store up to 6FF characters. Note: If you put a greater #alue than 6FF it $ill be con#erted to a ),V) type )IN2),V) *olds a string $ith a ma!imum length of 6FF characters
198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198 198
S,)
199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199 199
200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200
.7('L,(siPe%d)
., I0AL(siPe%d)
201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201 201
202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202 202
203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203 203
Stora0 e
204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204 204
205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205 205
F&9N bytes
206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206 206
smallmoney money
K bytes U bytes
207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207 207
208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208 208
209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209 209
SQL aggregate functions return a single #alue% calculated from #alues in a column. (seful aggregate functions3 ALA() & /eturns the a#erage #alue
210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210 210
7(N)() & /eturns the number of ro$s >I/S)() & /eturns the first #alue LAS)() & /eturns the last #alue 0AV() & /eturns the largest #alue 0IN() & /eturns the smallest #alue S(0() & /eturns the sum
SQL scalar functions return a single #alue% based on the input #alue. (seful scalar functions3 ( AS,() & on#erts a field to upper case
211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211 211
L AS,() &
0I.() & ,!tract characters from a te!t field L,N() & /eturns the length of a te!t field /7(N.() & /ounds a numeric field to the number of decimals specified N7W() & /eturns the current system date and time >7/0A)() & >ormats ho$ a field is to be displayed Tip: )he aggregate functions and the scalar functions $ill be e!plained in details in the ne!t chapters.
212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL A.)/0 Syntax
S,L, ) ALA(column=name) >/70 table=name
213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213 213
No$ $e $ant to find the a#erage #alue of the 87rder-rice8 fields. We use the follo$ing SQL statement3 S,L, ) ALA(7rder-rice) AS 7rderA#erage >/70 7rders )he result&set $ill loo4 li4e this3
214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214 214
215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215 215
)he
7(N)() function returns the number of ro$s that matches a specified criteria.
216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216 216
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL COUNT/20 Syntax
)he 7(N)(?) function returns the number of records in a table3 7(N)(?) >/70 table=name
S,L, )
217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217 217
SQL
O_Id 9 6 ; K F
7(N)(column=name) ,!ample
OrderDate 6::U599596 6::U59:56; 6::U5:D5:6 6::U5:D5:; 6::U5:U5;: OrderPrice 9::: 9E:: N:: ;:: 6::: C,stomer *ansen Nilsen *ansen *ansen Mensen
218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218
)he result of the SQL statement abo#e $ill be 6% because the customer Nilsen has made 6 orders in total3 C,stomerNi$se 6
219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219 219
SQL
S,L, )
7(N)(?) ,!ample
7(N)(?) AS Number7f7rders >/70 7rders
220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220 220
SQL
No$ $e $ant to count the number of uni"ue customers in the 87rders8 table. We use the follo$ing SQL statement3 S,L, ) 7(N)(.IS)IN ) ustomer) AS Number7f ustomers >/70 7rders
221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221 221
222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222
223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223 223
224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224 224
225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225 225
226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226 226
227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227 227
228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228 228
229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229 229
230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230 230
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL MIN/0 Syntax
S,L, ) 0IN(column=name) >/70 table=name
231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231 231
No$ $e $ant to find the smallest #alue of the 87rder-rice8 column. We use the follo$ing SQL statement3 S,L, ) 0IN(7rder-rice) AS Smallest7rder-rice >/70 7rders )he result&set $ill loo4 li4e this3
232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232 232
233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233 233
234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234 234
235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235 235
236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236 236
237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237 237
238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238 238
)he result&set $ill loo4 li4e this3 C,stomer *ansen Nilsen SU-3OrderPrice4 FN:: FN::
239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239 239
)he result&set abo#e is not $hat $e $anted. E1p$a atio o+ )*y t*e a2o"e SELECT stateme t ca ot 2e ,sed: )he S,L, ) statement abo#e has t$o columns specified ( ustomer and S(0(7rder-rice). )he 8S(0(7rder-rice)8 returns a single #alue (that is the total sum of the 87rder-rice8 column)% $hile 8 ustomer8 returns E #alues (one #alue for each ro$ in the 87rders8 table). )his $ill therefore not gi#e us the correct result. *o$e#er% you ha#e seen that the A/7(- '2 statement sol#es this problem.
240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240
olumn
We can also use the A/7(- '2 statement on more than one column% li4e this3 S,L, ) ustomer%7rder.ate%S(0(7rder-rice) >/70 7rders A/7(- '2 ustomer%7rder.ate
)he *ALINA
lause
)he *ALINA clause $as added to SQL because the W*,/, 4ey$ord could not be used $ith aggregate functions.
241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241 241
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL HA.IN) Syntax
S,L, ) column=name% aggregate=function(column=name) >/70 table=name W*,/, column=name operator #alue A/7(- '2 column=name *ALINA aggregate=function(column=name) operator #alue
242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242 242
No$ $e $ant to find if any of the customers ha#e a total order of less than 6:::.
243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243 243
No$ $e $ant to find if the customers 8*ansen8 or 8Mensen8 ha#e a total order of more than 9F::. We add an ordinary W*,/, clause to the SQL statement3
244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244 244
245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245 245
246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246 246
No$ $e $ant to select the content of the 8LastName8 and 8>irstName8 columns abo#e% and con#ert the 8LastName8 column to uppercase.
247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247 247
248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248 248
249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249 249
No$ $e $ant to select the content of the 8LastName8 and 8>irstName8 columns abo#e% and con#ert the 8LastName8 column to lo$ercase.
250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250 250
251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251
252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252 252
253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253 253
254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254 254
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
No$ $e $ant to select the length of the #alues in the 8Address8 column abo#e. We use the follo$ing S,L, ) statement3
256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256
257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257 257
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL ROUND/0 Syntax
S,L, ) /7(N.(column=name%decimals) >/70 table=name Parameter column=name decimals Descriptio /e"uired. )he field to round. /e"uired. Specifies the number of decimals to be returned.
258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258 258
No$ $e $ant to display the product name and the price rounded to the nearest integer. We use the follo$ing S,L, ) statement3
259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259 259
260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260 260
SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLS QLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQLSQL SQLSQLSQLSQLSQLSQLSQLSQLSQL SQL NOW/0 Syntax
S,L, ) N7W() >/70 table=name
261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261 261
No$ $e $ant to display the products and prices per todayCs date. We use the follo$ing S,L, ) statement3 S,L, ) -roductName% (nit-rice% No$() as -er.ate >/70 -roducts )he result&set $ill loo4 li4e this3 Prod,ctName Marlsberg 0ascarpone U itPrice 9:.KF ;6.FE PerDate 9:5N56::U 9936F3:6 A0 9:5N56::U 9936F3:6 A0
262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262 262
263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263 263
264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264 264
265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265 265
AL),/ )A'L,
AS (alias)
266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266 266
',)W,,N
267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267 267
/,A), IN.,V
/,A), LI,W
268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268 268
269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269 269
*ALINA
270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270 270
INS,/) IN)7
INN,/ M7IN
271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271 271
/IA*) M7IN
>(LL M7IN
272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272 272
7/.,/ '2
S,L, ) S,L, ) ?
273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273 273
274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274 274
(NI7N ALL
(-.A),
W*,/,
275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275 275
276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276 276