Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Example
S!L!CT ) &RO' Persons L('(T *
Oracle Syntax
S!L!CT column#name$s% &RO' table#name +,!R! RO+ -' ./ number
Example
S!L!CT ) &RO' Persons +,!R! RO+ -' ./*
ow we want to select only the two first records in the table above. +e use the following S!L!CT statement1 S!L!CT TOP 4 ) &RO' Persons The result:set will loo; li;e this1
P_Id 2 4
ow we want to select only *3< of the records in the table above. +e use the following S!L!CT statement1 S!L!CT TOP *3 P!RC! T ) &RO' Persons The result:set will loo; li;e this1 P_Id 2 4 LastName ,ansen Svendson FirstName Ola Tove Address Timoteivn 23 5orgvn 46 City Sandnes Sandnes
The LI E Operat!r
The L(7! operator is used to search for a specified pattern in a column.
SQL LI E Syntax
S!L!CT column#name$s% &RO' table#name +,!R! column#name L(7! pattern
LI E Operat!r Example
The 0Persons0 table1 P_Id 2 4 6 LastName ,ansen Svendson Pettersen FirstName Ola Tove 7ari Address Timoteivn 23 5orgvn 46 Storgt 43 City Sandnes Sandnes Stavanger
ow we want to select the persons living in a city that starts with 0s0 from the table above. +e use the following S!L!CT statement1
S!L!CT ) &RO' Persons +,!R! City L(7! =s<= The 0<0 sign can be used to define wildcards $missing letters in the pattern% both before and after the pattern. The result:set will loo; li;e this1 P_Id 2 4 6 LastName ,ansen Svendson Pettersen FirstName Ola Tove 7ari Address Timoteivn 23 5orgvn 46 Storgt 43 City Sandnes Sandnes Stavanger
e>t? we want to select the persons living in a city that ends with an 0s0 from the 0Persons0 table. +e use the following S!L!CT statement1 S!L!CT ) &RO' Persons +,!R! City L(7! =<s= The result:set will loo; li;e this1 P_Id 2 4 LastName ,ansen Svendson FirstName Ola Tove Address Timoteivn 23 5orgvn 46 City Sandnes Sandnes
e>t? we want to select the persons living in a city that contains the pattern 0tav0 from the 0Persons0 table. +e use the following S!L!CT statement1 S!L!CT ) &RO' Persons +,!R! City L(7! =<tav<= The result:set will loo; li;e this1 P_Id 6 LastName Pettersen FirstName 7ari Address Storgt 43 City Stavanger
(t is also possible to select the persons living in a city that 0Persons0 table? by using the OT ;eyword. +e use the following S!L!CT statement1 S!L!CT ) &RO' Persons +,!R! City OT L(7! =<tav<= The result:set will loo; li;e this1 P_Id 2 4 LastName ,ansen Svendson FirstName Ola Tove
SQL +ildcards
SQL wildcards can be used when searching for data in a database.
SQL "ildcards
SQL wildcards can substitute for one or more characters when searching for data in a database. SQL wildcards must be used with the SQL L(7! operator. +ith SQL? the following wildcards can be used1 Wildcard < # BcharlistC BDcharlistC or [!charlist] Description @ substitute for Aero or more characters @ substitute for e>actly one character @ny single character in charlist @ny single character not in charlist
e>t? we want to select the persons living in a city that contains the pattern 0nes0 from the 0Persons0 table. +e use the following S!L!CT statement1 S!L!CT ) &RO' Persons +,!R! City L(7! =<nes<= The result:set will loo; li;e this1 P_Id 2 4 LastName ,ansen Svendson FirstName Ola Tove Address Timoteivn 23 5orgvn 46 City Sandnes Sandnes
+e use the following S!L!CT statement1 S!L!CT ) &RO' Persons +,!R! &irst ame L(7! =#la= The result:set will loo; li;e this1 P_Id 2 LastName ,ansen FirstName Ola Address Timoteivn 23 City Sandnes
e>t? we want to select the persons with a last name that starts with 0S0? followed by any character? followed by 0end0? followed by any character? followed by 0on0 from the 0Persons0 table. +e use the following S!L!CT statement1 S!L!CT ) &RO' Persons +,!R! Last ame L(7! =S#end#on= The result:set will loo; li;e this1 P_Id 4 LastName Svendson FirstName Tove Address 5orgvn 46 City Sandnes
e>t? we want to select the persons with a last name that do not start with 0b0 or 0s0 or 0p0 from the 0Persons0 table. +e use the following S!L!CT statement1 S!L!CT ) &RO' Persons +,!R! Last ame L(7! =BEbspC<= The result:set will loo; li;e this1 P_Id 2 LastName ,ansen FirstName Ola Address Timoteivn 23 City Sandnes
SQL (
The (
Operator
The IN Operat!r
operator allows you to specify multiple values in a +,!R! clause.
SQL IN Syntax
S!L!CT column#name$s%
$value2?value4?...%
IN Operat!r Example
The 0Persons0 table1 P_Id 2 4 6 LastName ,ansen Svendson Pettersen FirstName Ola Tove 7ari Address Timoteivn 23 5orgvn 46 Storgt 43 City Sandnes Sandnes Stavanger
ow we want to select the persons with a last name eFual to 0,ansen0 or 0Pettersen0 from the table above. +e use the following S!L!CT statement1 S!L!CT ) &RO' Persons +,!R! Last ame ( $=,ansen=?=Pettersen=% The result:set will loo; li;e this1 P_Id 2 6 LastName ,ansen Pettersen FirstName Ola 7ari Address Timoteivn 23 Storgt 43 City Sandnes Stavanger
SQL 5!T+!!
Operator
The 5!T+!! operator is used in a +,!R! clause to select a range of data between two values.
ow we want to select the persons with a last name alphabetically between 0,ansen0 and 0Pettersen0 from the table above. +e use the following S!L!CT statement1
S!L!CT ) &RO' Persons +,!R! Last ame 5!T+!! =,ansen= @ G =Pettersen= The result:set will loo; li;e this1 P_Id 2 LastName ,ansen FirstName Ola Address Timoteivn 23 City Sandnes
(n some databases? persons with the Last ame of 0,ansen0 or 0Pettersen0 will not be listed? because the 5!T+!! operator only selects fields that are between and e>cluding the test values%. (n other databases? persons with the Last ame of 0,ansen0 or 0Pettersen0 will be listed? because the 5!T+!! operator selects fields that are between and including the test values%. @nd in other databases? persons with the Last ame of 0,ansen0 will be listed? but 0Pettersen0 will not be listed $li;e the e>ample above%? because the 5!T+!! operator selects fields between the test values? including the first test value and e>cluding the last test value. Therefore1 Chec; how your database treats the 5!T+!! operator.
Example *
To display the persons outside the range in the previous e>ample? use S!L!CT ) &RO' Persons +,!R! Last ame OT 5!T+!! =,ansen= @ G =Pettersen= The result:set will loo; li;e this1 P_Id 4 6 LastName Svendson Pettersen FirstName Tove 7ari Address 5orgvn 46 Storgt 43 City Sandnes Stavanger OT 5!T+!! 1
SQL @lias
+ith SQL? an alias name can be given to a table or to a column.
SQL +lias
Hou can give a table or a column another name by using an alias. This can be a good thing to do if you have very long or comple> table names or column names. @n alias name could be anything? but usually it is short.
+lias Example
@ssume we have a table called 0Persons0 and another table called 0Product#Orders0. +e will give the table aliases of 0p0 and 0po0 respectively. ow we want to list all the orders that 0Ola ,ansen0 is responsible for.
+e use the following S!L!CT statement1 S!L!CT po.Order(G? p.Last ame? p.&irst ame &RO' Persons @S p? Product#Orders @S po +,!R! p.Last ame/=,ansen= @ G p.&irst ame/=Ola= The same S!L!CT statement without aliases1 S!L!CT Product#Orders.Order(G? Persons.Last ame? Persons.&irst ame &RO' Persons? Product#Orders +,!R! Persons.Last ame/=,ansen= @ G Persons.&irst ame/=Ola= @s you=ll see from the two S!L!CT statements aboveI aliases can ma;e Fueries easier to both write and to read.
SQL Joins
SQL Koins are used to Fuery data from two or more tables? based on a relationship between certain columns in these tables.
SQL .OIN
The JO( ;eyword is used in an SQL statement to Fuery data from two or more tables? based on a relationship between certain columns in these tables. Tables in a database are often related to each other with ;eys. @ primary ;ey is a column $or a combination of columns% with a uniFue value for each row. !ach primary ;ey value must be uniFue within the table. The purpose is to bind data together? across tables? without repeating all of the data in every table. Loo; at the 0Persons0 table1 P_Id 2 4 6 LastName ,ansen Svendson Pettersen FirstName Ola Tove 7ari Address Timoteivn 23 5orgvn 46 Storgt 43 City Sandnes Sandnes Stavanger
ote that the 0P#(d0 column is the primary ;ey in the 0Persons0 table. This means that no two rows can have the same P#(d. The P#(d distinguishes two persons even if they have the same name. e>t? we have the 0Orders0 table1 O_Id 2 4 6 8 * OrderNo LLMN* 88OLM 448*O 48*O4 68LO8 P_Id 6 6 2 2 2*
ote that the 0O#(d0 column is the primary ;ey in the 0Orders0 table and that the 0P#(d0 column refers to the persons in the 0Persons0 table without using their names. otice that the relationship between the two tables above is the 0P#(d0 column.
JOIN1 Return rows when there is at least one match in both tables LEFT JOIN1 Return all rows from the left table? even if there are no matches in the right table RI !T JOIN1 Return all rows from the right table? even if there are no matches in the left table F"LL JOIN1 Return rows when there is a match in one of the tables
ey0!rd
;eyword return rows when there is at least one match in both tables.
The 0Orders0 table1 O_Id 2 4 6 8 * OrderNo LLMN* 88OLM 448*O 48*O4 68LO8 P_Id 6 6 2 2 2*
ow we want to list all the persons with any orders. +e use the following S!L!CT statement1 S!L!CT Persons.Last ame? Persons.&irst ame? Orders.Order o &RO' Persons ( !R JO( Orders O Persons.P#(d/Orders.P#(d ORG!R 5H Persons.Last ame The result:set will loo; li;e this1
The ( !R JO( ;eyword return rows when there is at least one match in both tables. (f there are rows in 0Persons0 that do not have matches in 0Orders0? those rows will OT be listed.
7eyword
ey0!rd
The L!&T JO( ;eyword returns all rows from the left table $table#name2%? even if there are no matches in the right table $table#name4%.
The 0Orders0 table1 O_Id 2 4 6 8 * OrderNo LLMN* 88OLM 448*O 48*O4 68LO8 P_Id 6 6 2 2 2*
ow we want to list all the persons and their orders : if any? from the tables above. +e use the following S!L!CT statement1 S!L!CT Persons.Last ame? Persons.&irst ame? Orders.Order o &RO' Persons L!&T JO( Orders O Persons.P#(d/Orders.P#(d ORG!R 5H Persons.Last ame
The result:set will loo; li;e this1 LastName ,ansen ,ansen Pettersen Pettersen Svendson FirstName Ola Ola 7ari 7ari Tove OrderNo 448*O 48*O4 LLMN* 88OLM
The L!&T JO( ;eyword returns all the rows from the left table $Persons%? even if there are no matches in the right table $Orders%.
The 0Orders0 table1 O_Id 2 4 6 8 * OrderNo LLMN* 88OLM 448*O 48*O4 68LO8 P_Id 6 6 2 2 2*
ow we want to list all the orders with containing persons : if any? from the tables above. +e use the following S!L!CT statement1 S!L!CT Persons.Last ame? Persons.&irst ame? Orders.Order o &RO' Persons
R(P,T JO( Orders O Persons.P#(d/Orders.P#(d ORG!R 5H Persons.Last ame The result:set will loo; li;e this1 LastName ,ansen ,ansen Pettersen Pettersen FirstName Ola Ola 7ari 7ari OrderNo 448*O 48*O4 LLMN* 88OLM 68LO8 The R(P,T JO( ;eyword returns all the rows from the right table $Orders%? even if there are no matches in the left table $Persons%.
7eyword
ey0!rd
The 0Orders0 table1 O_Id 2 4 6 8 * OrderNo LLMN* 88OLM 448*O 48*O4 68LO8 P_Id 6 6 2 2 2*
ow we want to list all the persons and their orders? and all the orders with their persons. +e use the following S!L!CT statement1
S!L!CT Persons.Last ame? Persons.&irst ame? Orders.Order o &RO' Persons &-LL JO( Orders O Persons.P#(d/Orders.P#(d ORG!R 5H Persons.Last ame The result:set will loo; li;e this1 LastName ,ansen ,ansen Pettersen Pettersen Svendson FirstName Ola Ola 7ari 7ari Tove 68LO8 The &-LL JO( ;eyword returns all the rows from the left table $Persons%? and all the rows from the right table $Orders%. (f there are rows in 0Persons0 that do not have matches in 0Orders0? or if there are rows in 0Orders0 that do not have matches in 0Persons0? those rows will be listed as well. OrderNo 448*O 48*O4 LLMN* 88OLM
SQL - (O
The SQL - (O
Operator
operator combines two or more S!L!CT statements.
34 36 38 $Employees_"#A$1 E_ID 32 34 36 38
E_Name Turner? Sally 7ent? Clar; Svendson? Stephen Scott? Stephen orway and -S@.
ow we want to list all t&e di''erent employees in +e use the following S!L!CT statement1 S!L!CT !# ame &RO' !mployees# orway - (O S!L!CT !# ame &RO' !mployees#-S@ The result:set will loo; li;e this1 E_Name ,ansen? Ola Svendson? Tove Svendson? Stephen Pettersen? 7ari Turner? Sally 7ent? Clar; Scott? Stephen
Note: This command cannot be used to list all employees in orway and -S@. (n the e>ample above we have two employees with eFual names? and only one of them will be listed. The - (O command selects only distinct values.
Pettersen? 7ari Turner? Sally 7ent? Clar; Svendson? Stephen Scott? Stephen
e>ternaldatabaseC
Or we can select only the columns we want into the new table1 S!L!CT column#name$s% ( TO new#table#name B( &RO' old#tablename
e>ternaldatabaseC
=5ac;up.mdb=
+e can also copy only a few fields into the new table1 S!L!CT Last ame?&irst ame ( TO Persons#5ac;up &RO' Persons
$ P#(d int? Last ame varchar$4**%? &irst ame varchar$4**%? @ddress varchar$4**%? City varchar$4**% % The P#(d column is of type int and will hold a number. The Last ame? &irst ame? @ddress? and City columns are of type varchar with a ma>imum length of 4** characters. The empty 0Persons0 table will now loo; li;e this1 P_Id LastName FirstName Address City
The empty table can be filled with data with the ( S!RT ( TO statement.
SQL Constraints
SQL C!nstraints
Constraints are used to limit the type of data that can go into a table. Constraints can be specified when a table is created $with the CR!@T! T@5L! statement% or after the table is created $with the @LT!R T@5L! statement%. +e will focus on the following constraints1
OT
-LL
SQL
OT
-LL Constraint
-LL values.
-LL?
SQL UNIQUE
o!strai!t
-LL?
#,L #er-er . Oracle . )# Access: CR!@T! T@5L! Persons $ P#(d int OT -LL - (Q-!? Last ame varchar$4**% OT &irst ame varchar$4**%? @ddress varchar$4**%? City varchar$4**% %
-LL?
To allow naming of a - (Q-! constraint? and for defining a - (Q-! constraint on multiple columns? use the following SQL synta>1 )y#,L . #,L #er-er . Oracle . )# Access: CR!@T! T@5L! Persons $ P#(d int OT -LL? Last ame varchar$4**% OT -LL? &irst ame varchar$4**%? @ddress varchar$4**%? City varchar$4**%? CO STR@( T uc#Person(G - (Q-! $P#(d?Last ame% %
)y#,L . #,L #er-er . Oracle . )# Access: @LT!R T@5L! Persons @GG CO STR@( T uc#Person(G - (Q-! $P#(d?Last ame%
!ach table should have a primary ;ey? and each table can have only O ! primary ;ey.
SQL PRIM+R5
)y#,L: CR!@T! T@5L! Persons $ P#(d int OT -LL? Last ame varchar$4**% OT &irst ame varchar$4**%? @ddress varchar$4**%? City varchar$4**%? PR('@RH 7!H $P#(d% %
The following SQL creates a PR('@RH 7!H on the 0P#(d0 column when the 0Persons0 table is created1
-LL?
#,L #er-er . Oracle . )# Access: CR!@T! T@5L! Persons $ P#(d int OT -LL PR('@RH 7!H? Last ame varchar$4**% OT -LL? &irst ame varchar$4**%? @ddress varchar$4**%? City varchar$4**% % To allow naming of a PR('@RH 7!H constraint? and for defining a PR('@RH 7!H constraint on multiple columns? use the following SQL synta>1 )y#,L . #,L #er-er . Oracle . )# Access: CR!@T! T@5L! Persons $ P#(d int OT -LL?
Last ame varchar$4**% OT -LL? &irst ame varchar$4**%? @ddress varchar$4**%? City varchar$4**%? CO STR@( T p;#Person(G PR('@RH 7!H $P#(d?Last ame% %
SQL PRIM+R5
To create a PR('@RH 7!H constraint on the 0P#(d0 column when the table is already created? use the following SQL1 )y#,L . #,L #er-er . Oracle . )# Access: @LT!R T@5L! Persons @GG PR('@RH 7!H $P#(d% To allow naming of a PR('@RH 7!H constraint? and for defining a PR('@RH 7!H constraint on multiple columns? use the following SQL synta>1 )y#,L . #,L #er-er . Oracle . )# Access: @LT!R T@5L! Persons @GG CO STR@( T p;#Person(G PR('@RH 7!H $P#(d?Last ame% Note: (f you use the @LT!R T@5L! statement to add a primary ;ey? the primary ;ey column$s% must already have been declared to not contain -LL values $when the table was first created%.
T! /ROP a PRIM+R5
)y#,L: @LT!R T@5L! Persons GROP PR('@RH 7!H #,L #er-er . Oracle . )# Access: @LT!R T@5L! Persons GROP CO STR@( T p;#Person(G
E5 C!nstraint
SQL &OR!(P
SQL 1OREI2N
@ &OR!(P
7!H Constraint
E5 C!nstraint
Let=s illustrate the foreign ;ey with an e>ample. Loo; at the following two tables1 The 0Persons0 table1 P_Id 2 4 6 LastName ,ansen Svendson Pettersen FirstName Ola Tove 7ari Address Timoteivn 23 5orgvn 46 Storgt 43 City Sandnes Sandnes Stavanger
4 6 8
6 4 2
ote that the 0P#(d0 column in the 0Orders0 table points to the 0P#(d0 column in the 0Persons0 table. The 0P#(d0 column in the 0Persons0 table is the PR('@RH 7!H in the 0Persons0 table. The 0P#(d0 column in the 0Orders0 table is a &OR!(P The &OR!(P 7!H in the 0Orders0 table.
7!H constraint is used to prevent actions that would destroy lin;s between tables.
The &OR!(P 7!H constraint also prevents that invalid data form being inserted into the foreign ;ey column? because it has to be one of the values contained in the table it points to.
SQL 1OREI2N
)y#,L:
CR!@T! T@5L! Orders $ O#(d int OT -LL? Order o int OT -LL? P#(d int? PR('@RH 7!H $O#(d%? &OR!(P 7!H $P#(d% R!&!R! C!S Persons$P#(d% % #,L #er-er . Oracle . )# Access: CR!@T! T@5L! Orders $ O#(d int OT -LL PR('@RH 7!H? Order o int OT -LL? P#(d int &OR!(P 7!H R!&!R! C!S Persons$P#(d% % To allow naming of a &OR!(P 7!H constraint? and for defining a &OR!(P columns? use the following SQL synta>1 )y#,L . #,L #er-er . Oracle . )# Access: CR!@T! T@5L! Orders $ O#(d int OT -LL? Order o int OT -LL? P#(d int? PR('@RH 7!H $O#(d%? CO STR@( T f;#PerOrders &OR!(P R!&!R! C!S Persons$P#(d% % 7!H constraint on multiple
7!H $P#(d%
SQL 1OREI2N
To create a &OR!(P 7!H constraint on the 0P#(d0 column when the 0Orders0 table is already created? use the following SQL1 )y#,L . #,L #er-er . Oracle . )# Access:
@LT!R T@5L! Orders @GG &OR!(P 7!H $P#(d% R!&!R! C!S Persons$P#(d% To allow naming of a &OR!(P 7!H constraint? and for defining a &OR!(P columns? use the following SQL synta>1 )y#,L . #,L #er-er . Oracle . )# Access: @LT!R T@5L! Orders @GG CO STR@( T f;#PerOrders &OR!(P 7!H $P#(d% R!&!R! C!S Persons$P#(d% 7!H constraint on multiple
T! /ROP a 1OREI2N
To drop a &OR!(P )y#,L: @LT!R T@5L! Orders GROP &OR!(P 7!H f;#PerOrders #,L #er-er . Oracle . )# Access: @LT!R T@5L! Orders GROP CO STR@( T f;#PerOrders
E5 C!nstraint
SQL
HE K
o!strai!t
SQL C3EC
C!nstraint
The C,!C7 constraint is used to limit the value range that can be placed in a column. (f you define a C,!C7 constraint on a single column it allows only certain values for this column. (f you define a C,!C7 constraint on a table it can limit the values in certain columns based on values in other columns in the row.
SQL C3EC
The following SQL creates a C,!C7 constraint on the 0P#(d0 column when the 0Persons0 table is created. The C,!C7 constraint specifies that the column 0P#(d0 must only include integers greater than 3. )y #,L: CR!@T! T@5L! Persons $ P#(d int OT -LL? Last ame varchar$4**% OT &irst ame varchar$4**%? @ddress varchar$4**%? City varchar$4**%? C,!C7 $P#(dR3% %
-LL?
#,L #er-er . Oracle . )# Access: CR!@T! T@5L! Persons $ P#(d int OT -LL C,!C7 $P#(dR3%? Last ame varchar$4**% OT -LL? &irst ame varchar$4**%? @ddress varchar$4**%?
City varchar$4**% % To allow naming of a C,!C7 constraint? and for defining a C,!C7 constraint on multiple columns? use the following SQL synta>1 )y#,L . #,L #er-er . Oracle . )# Access: CR!@T! T@5L! Persons $ P#(d int OT -LL? Last ame varchar$4**% OT -LL? &irst ame varchar$4**%? @ddress varchar$4**%? City varchar$4**%? CO STR@( T ch;#Person C,!C7 $P#(dR3 @ G City/=Sandnes=% %
SQL C3EC
To create a C,!C7 constraint on the 0P#(d0 column when the table is already created? use the following SQL1 )y#,L . #,L #er-er . Oracle . )# Access: @LT!R T@5L! Persons @GG C,!C7 $P#(dR3% To allow naming of a C,!C7 constraint? and for defining a C,!C7 constraint on multiple columns? use the following SQL synta>1 )y#,L . #,L #er-er . Oracle . )# Access: @LT!R T@5L! Persons @GG CO STR@( T ch;#Person C,!C7 $P#(dR3 @ G City/=Sandnes=%
T! /ROP a C3EC
C!nstraint
To drop a C,!C7 constraint? use the following SQL1 #,L #er-er . Oracle . )# Access: @LT!R T@5L! Persons GROP CO STR@( T ch;#Person
-LL?
City varchar$4**% G!&@-LT =Sandnes= % The G!&@-LT constraint can also be used to insert system values? by using functions li;e P!TG@T!$%1 CR!@T! T@5L! Orders $ O#(d int OT -LL? Order o int OT -LL? P#(d int? OrderGate date G!&@-LT P!TG@T!$% %
(nde>es
@n inde> can be created in a table to find data more Fuic;ly and efficiently. The users cannot see the inde>es? they are Kust used to speed up searchesSFueries. Note: -pdating a table with inde>es ta;es more time than updating a table without $because the inde>es also need an update%. So you should only create inde>es on columns $and tables% that will be freFuently searched against.
ow we want to add a column named 0GateOf5irth0 in the 0Persons0 table. +e use the following SQL statement1 @LT!R T@5L! Persons @GG GateOf5irth date otice that the new column? 0GateOf5irth0? is of type date and is going to hold a date. The data type specifies what type of data the column can hold. &or a complete reference of all the data types available in 'S @ccess? 'ySQL? and SQL Server? go to our complete Gata Types reference. The 0Persons0 table will now li;e this1 P_Id 2 4 LastName ,ansen Svendson FirstName Ola Tove Address Timoteivn 23 5orgvn 46 City Sandnes Sandnes DateO'+irt&
Pettersen
7ari
Storgt 43
Stavanger
5y default? the starting value for @-TO#( CR!'! T is 2? and it will increment by 2 for each new record. To let the @-TO#( CR!'! T seFuence start with another value? use the following SQL statement1 @LT!R T@5L! Persons @-TO#( CR!'! T/233 To insert a new record into the 0Persons0 table? we will not have to specify a value for the 0P#(d0 column $a uniFue value will be added automatically%1 ( S!RT ( TO Persons $&irst ame?Last ame% 9@L-!S $=Lars=?='onsen=% The SQL statement above would insert a new record into the 0Persons0 table. The 0P#(d0 column would be assigned a uniFue value. The 0&irst ame0 column would be set to 0Lars0 and the 0Last ame0 column would be set to 0'onsen0.
To insert a new record into the 0Persons0 table? we will not have to specify a value for the 0P#(d0 column $a uniFue value will be added automatically%1 ( S!RT ( TO Persons $&irst ame?Last ame% 9@L-!S $=Lars=?='onsen=% The SQL statement above would insert a new record into the 0Persons0 table. The 0P#(d0 column would be assigned a uniFue value. The 0&irst ame0 column would be set to 0Lars0 and the 0Last ame0 column would be set to 0'onsen0.
SQL (iews
@ view is a virtual table. This chapter shows how to create? update? and delete a view.
The view 0Current Product List0 lists all active products $products that are not discontinued% from the 0Products0 table. The view is created with the following SQL1 CR!@T! 9(!+ BCurrent Product ListC @S S!L!CT Product(G?Product ame &RO' Products +,!R! Giscontinued/ o +e can Fuery the view above as follows1 S!L!CT ) &RO' BCurrent Product ListC @nother view in the orthwind sample database selects every product in the 0Products0 table with a unit price higher than the average unit price1 CR!@T! 9(!+ BProducts @bove @verage PriceC @S S!L!CT Product ame?-nitPrice &RO' Products +,!R! -nitPriceR$S!L!CT @9P$-nitPrice% &RO' Products% +e can Fuery the view above as follows1 S!L!CT ) &RO' BProducts @bove @verage PriceC @nother view in the orthwind database calculates the total sale for each category in 2NNL. this view selects its data from another view called 0Product Sales for 2NNL01 CR!@T! 9(!+ BCategory Sales &or 2NNLC @S S!L!CT G(ST( CT Category ame?Sum$ProductSales% @S CategorySales &RO' BProduct Sales for 2NNLC PRO-P 5H Category ame +e can Fuery the view above as follows1 S!L!CT ) &RO' BCategory Sales &or 2NNLC +e can also add a condition to the Fuery. 05everages01 S!L!CT ) &RO' BCategory Sales &or 2NNLC +,!R! Category ame/=5everages= ow we want to see the total sale only for the category ote that
G@T!T('! : format1 HHHH:'':GG ,,1''1SS T('!ST@'P : format1 HHHH:'':GG ,,1''1SS H!@R : format HHHH or HH
#,L #er-er comes with the following data types for storing a date or a dateStime value in the database1
G@T! : format HHHH:'':GG G@T!T('! : format1 HHHH:'':GG ,,1''1SS S'@LLG@T!T('! : format1 HHHH:'':GG ,,1''1SS T('!ST@'P : format1 a uniFue number
Note: The date types are chosen for a column when you create a new table in your databaseE &or an overview of all data types available? go to our complete Gata Types reference.
ow we want to select the records with an OrderGate of 0433M:22:220 from the table above. +e use the following S!L!CT statement1 S!L!CT ) &RO' Orders +,!R! OrderGate/=433M:22:22= The result:set will loo; li;e this1 OrderId 2 6 Prod(ctName Peitost 'oAAarella di Piovanni OrderDate 433M:22:22 433M:22:22
ow? assume that the 0Orders0 table loo;s li;e this $notice the time component in the 0OrderGate0 column%1 OrderId 2 4 6 8 Prod(ctName Peitost Camembert Pierrot 'oAAarella di Piovanni 'ascarpone &abioli OrderDate 433M:22:22 26146188 433M:22:3N 2*18*142 433M:22:22 22124132 433M:23:4N 281*O1*N
(f we use the same S!L!CT statement as above1 S!L!CT ) &RO' Orders +,!R! OrderGate/=433M:22:22= we will get no resultE This is because the Fuery is loo;ing only for dates with no time portion.
Tip: (f you want to ;eep your Fueries simple and easy to maintain? do not allow time components in your datesE
SQL
-LL 9alues
-LL values. OT -LL operators. -LL and (S
-LL values represent missing un;nown data. 5y default? a table column can hold This chapter will e>plain the (S
Suppose that the 0@ddress0 column in the 0Persons0 table is optional. This means that if we insert a record with no value for the 0@ddress0 column? the 0@ddress0 column will be saved with a -LL value. ,ow can we test for -LL valuesT -LL values with comparison operators? such as /? .? or .R. -LL and (S OT -LL operators instead.
SQL IS N#LL
,ow do we select only the records with +e will have to use the (S -LL values in the 0@ddress0 columnT -LL operator1
S!L!CT Last ame?&irst ame?@ddress &RO' Persons +,!R! @ddress (S -LL The result:set will loo; li;e this1 LastName ,ansen Pettersen Tip: @lways use (S FirstName Ola 7ari -LL to loo; for -LL values. Address
The result:set will loo; li;e this1 LastName Svendson FirstName Tove Address 5orgvn 46
SQL
-LL &unctions
Suppose that the 0-nitsOnOrder0 column is optional? and may contain +e have the following S!L!CT statement1 S!L!CT Product ame?-nitPrice)$-nits(nStoc;U-nitsOnOrder% &RO' Products (n the e>ample above? if any of the 0-nitsOnOrder0 values are
-LL.
9L$%? (& -LL$%? and CO@L!SC!$% functions can also be used to achieve the same result. -LL values to be Aero. -LL it will not harm the calculation? because (S -LL$% returns a Aero if
S!L!CT Product ame?-nitPrice)$-nits(nStoc;U(S -LL$-nitsOnOrder?3%% &RO' Products Oracle Oracle does not have an (S -LL$% function. ,owever? we can use the same result1 S!L!CT Product ame?-nitPrice)$-nits(nStoc;U 9L$-nitsOnOrder?3%% &RO' Products )y#,L 'ySQL does have an (S -LL$% function. ,owever? it wor;s a little bit different from 'icrosoft=s (S -LL$% function. (n 'ySQL we can use the (& -LL$% function? li;e this1 S!L!CT Product ame?-nitPrice)$-nits(nStoc;U(& -LL$-nitsOnOrder?3%% &RO' Products or we can use the CO@L!SC!$% function? li;e this1 S!L!CT Product ame?-nitPrice)$-nits(nStoc;UCO@L!SC!$-nitsOnOrder?3%% 9L$% function to achieve the
Gata types and ranges for 'icrosoft @ccess? 'ySQL and SQL Server.
'emo
@uto umber
8 bytes
GateSTime HesS o
M bytes
@ logical field can be displayed as HesS o? TrueS&alse? or OnSOff. (n 2 bit code? use the constants True and &alse $eFuivalent to :2 and 3%. Note: ull values are not allowed in HesS o fields Can store pictures? audio? video? or other 5LO5s $5inary Large O5Kects% up to 2P5 Contain lin;s to other files? including web pages Let you type a list of options? which can then be chosen from a drop: down list 8 bytes
9@RC,@R$siAe%
,olds a string with a ma>imum length of 2O?LLL?42* characters &or 5LO5s $5inary Large O5Kects%. ,olds up to 2O?LLL?42* bytes of data ,olds a string with a ma>imum length of 8?4N8?NOL?4N* characters &or 5LO5s $5inary Large O5Kects%. ,olds up to 8?4N8?NOL?4N* bytes of data Let you enter a list of possible values. Hou can list up to O**6* values in an ! -' list. (f a value is inserted that is not in the list? a blan; value will be inserted. Note: The values are sorted in the order you enter them. Hou enter the possible values in this format1 ! -'$=Q=?=H=?=V=%
S!T
Similar to ! -' e>cept that S!T may contain up to O8 list items and can store more than one choice
N(m1er types: Data type T( H( T$siAe% Description :24M to 24L normal. 3 to 4** - S(P !G). The ma>imum number of digits may be specified in parenthesis :64LOM to 64LOL normal. 3 to O**6* - S(P !G). The ma>imum number of digits may be specified in parenthesis :M6MMO3M to M6MMO3L normal. 3 to 2OLLL42* - S(P !G). The ma>imum number of digits may be specified in parenthesis :428L8M6O8M to 428L8M6O8L normal. 3 to 84N8NOL4N* - S(P !G). The ma>imum number of digits may be specified in parenthesis :N4466L436OM*8LL*M3M to N4466L436OM*8LL*M3L normal. 3 to 2M88OL883L6L3N**2O2* - S(P !G). The ma>imum number of digits may be specified in parenthesis @ small number with a floating decimal point. The ma>imum number of digits may be specified in the siAe parameter. The ma>imum number of digits to the right of the decimal point is specified in the d parameter @ large number with a floating decimal point. The ma>imum number of digits may be specified in the siAe parameter. The ma>imum number of digits to the right of the decimal point is specified in the d parameter @ GO-5L! stored as a string ? allowing for a fi>ed decimal point. The ma>imum number of digits may be specified in the siAe parameter. The ma>imum number of digits to the right of the decimal point is specified in the d parameter
S'@LL( T$siAe%
'!G(-'( T$siAe%
( T$siAe%
5(P( T$siAe%
&LO@T$siAe?d%
GO-5L!$siAe?d%
G!C('@L$siAe?d%
)The integer types have an e>tra option called - S(P !G. ormally? the integer goes from an negative to positive value. @dding the - S(P !G attribute will move that range up so it starts at Aero instead of a negative number. Date types: Data type G@T!$% Description @ date. &ormat1 HHHH:'':GG Note: The supported range is from =2333:32:32= to =NNNN:24:62= G@T!T('!$% )@ date and time combination. &ormat1 HHHH:'':GG ,,1''1SS
Note: The supported range is from =2333:32:32 33133133= to =NNNN:24:62 461*N1*N= T('!ST@'P$% )@ timestamp. T('!ST@'P values are stored as the number of seconds since the -ni> epoch $=2NL3:32:32 33133133= -TC%. &ormat1 HHHH:'':GG ,,1''1SS Note: The supported range is from =2NL3:32:32 33133132= -TC to =436M:32:3N 3612813L= -TC T('!$% @ time. &ormat1 ,,1''1SS Note: The supported range is from =:M6M1*N1*N= to =M6M1*N1*N= H!@R$% @ year in two:digit or four:digit format. Note: 9alues allowed in four:digit format1 2N32 to 42**. 9alues allowed in two: digit format1 L3 to ON? representing years from 2NL3 to 43ON )!ven if G@T!T('! and T('!ST@'P return the same format? they wor; very differently. (n an ( S!RT or -PG@T! Fuery? the T('!ST@'P automatically set itself to the current date and time. T('!ST@'P also accepts various formats? li;e HHHH''GG,,''SS? HH''GG,,''SS? HHHH''GG? or HH''GG.
&i>ed:length binary data. 'a>imum M?333 bytes 9ariable:length binary data. 'a>imum M?333 bytes 9ariable:length binary data. 'a>imum 4P5 9ariable:length binary data. 'a>imum 4P5
Description @llows whole numbers from 3 to 4** @llows whole numbers between :64?LOM and 64?LOL @llows whole numbers between :4?28L?8M6?O8M and 4?28L?8M6?O8L @llows whole numbers between :N?446?6L4?36O?M*8?LL*?M3M and N?446?6L4?36O?M*8?LL*?M3L &i>ed precision and scale numbers. @llows numbers from :23D6M U2 to 23D6M W2. The p parameter indicates the ma>imum total number of digits that can be stored $both to the left and to the right of the decimal point%. p must be a value from 2 to 6M. Gefault is 2M. The s parameter indicates the ma>imum number of digits stored to the right of the decimal point. s must be a value from 3 to p. Gefault value is 3
decimal$p?s%
*:2L bytes
numeric$p?s%
&i>ed precision and scale numbers. @llows numbers from :23D6M U2 to 23D6M W2. The p parameter indicates the ma>imum total number of digits that can be stored $both to the left and to the right of the decimal point%. p must be a value from 2 to 6M. Gefault is 2M. The s parameter indicates the ma>imum number of digits stored to the right of the decimal point. s must be a value from 3 to p. Gefault value is 3
*:2L bytes
smallmoney money
'onetary data from :428?L8M.6O8M to 428?L8M.6O8L 'onetary data from :N44?66L?436?OM*?8LL.*M3M to N44?66L?436?OM*?8LL.*M3L &loating precision number data from :2.LN! U 63M to 2.LN! U 63M. The n parameter indicates whether the field should hold 8 or M bytes. float$48% holds a 8:byte field and float$*6% holds an M:byte field. Gefault value of n is *6.
8 bytes M bytes
float$n%
8 or M bytes
8 bytes
Description &rom January 2? 2L*6 to Gecember 62? NNNN with an accuracy of 6.66 milliseconds &rom January 2? 3332 to Gecember 62? NNNN with an accuracy of 233 nanoseconds &rom January 2? 2N33 to June O? 43LN with an accuracy of 2 minute Store a date only. &rom January 2? 3332 to Gecember 62? NNNN Store a time only to an accuracy of 233 nanoseconds The same as datetime4 with the addition of a time Aone offset
#tora/e M bytes
datetime4
O:M bytes
timestamp
Stores a uniFue number that gets updated every time a row gets created or modified. The timestamp value is based upon an internal cloc; and does not correspond to real time. !ach table may have only one timestamp variable
Ot&er data types: Data type sFl#variant Description Stores up to M?333 bytes of data of various data types? e>cept te>t? nte>t? and timestamp Stores a globally uniFue identifier $P-(G% Stores Q'L formatted data. 'a>imum 4P5 Stores a reference to a cursor used for database operations Stores a result:set for later processing