You are on page 1of 9

ALTER u8C Auu MCull?

SCL uLSC CuS1CML8


name null? 1ype

CuS1_nAML CPA8(30)
CuS1_lu nuM8L8(38)

1 u8C

SCL AL1L8 1A8LL CuS1CML8
2 u8C CCLuMn CuS1_lu

2 Auu

SCL AL1L8 1A8LL CuS1CML8
2 Auu CuS1_lu nuM8L8(10)

3 MCull?
SCL AL1L8 1A8LL CuS1CML8
2 MCull? CuS1_lu CPA8(40) 8lMA8? kL?








The !RIMARY KEY constraint
uniquely identiIies each record in a database table.
Primary keys must contain unique values.
A primary key column cannot contain NULL values.
Each table should have a primary key, and each table can have only ONE primary key.


The T &LL constraint enIorces a column to NOT
accept NULL values.
The NOT NULL constraint enIorces a Iield to always contain a value. This means that you
cannot insert a new record, or update a record without adding a value to this Iield.

A REIG KEY oe Lable
poLs Lo a 8lMA8? kL? aoLher Lable
The "Persons" table:
!*Id Lastame 7stame Add7ess Cty
Hansen Ola Timoteivn Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 2 Stavanger
The "Orders" table:
*Id 7de74 !*Id
77895 3
2 44678 3
3 22456 2
4 24562
The "PId" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "PId" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
WHERE Clause Example
The "Persons" table:
!*Id Lastame 7stame Add7ess Cty
Hansen Ola Timoteivn Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 2 Stavanger
Now we want to select only the persons living in the city "Sandnes" Irom the table above.
We use the Iollowing SELECT statement:
SELECT * FROM Persons
WHERE City'Sandnes'
The result-set will look like this:
!*Id Lastame 7stame Add7ess Cty
Hansen Ola Timoteivn Sandnes
2 Svendson Tove Borgvn 23 Sandnes


pe7at47s All4ed 3 the WHERE Clause
With the WHERE clause, the Iollowing operators can be used:
pe7at47 Desc7pt43
Equal
~ Not equal
~ Greater than
Less than
~ Greater than or equal
Less than or equal
BETWEEN Between an inclusive range
LIKE Search Ior a pattern
IN II you know the exact value you want to return Ior at least one oI the columns



1he RDER BY keyword s used Lo sorL Lhe resulL
seL by a speced colum
SLLLC1 colum_ame(s)
l8CM Lable_ame
C8uL8 8? colum_ame(s) ASC|uLSC


The I$ERT IT statement is used to
insert a new row in a table.
It is possible to write the INSERT INTO statement in two Iorms.
The Iirst Iorm doesn't speciIy the column names where the data will be inserted, only their
values:
INSERT INTO tablename
VALUES (value, value2, value3,...)
The second Iorm speciIies both the column names and the values to be inserted:
INSERT INTO tablename (column, column2, column3,...)
VALUES (value, value2, value3,...)



$"L &!DATE Example
The "Persons" table:
!*Id Lastame 7stame Add7ess Cty
Hansen Ola Timoteivn Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 2 Stavanger
4 Nilsen Johan Bakken 2 Stavanger
5 Tjessem Jakob
Now we want to update the person "Tjessem, Jakob" in the "Persons" table.
We use the Iollowing SQL statement:
UPDATE Persons
SET Address'Nissestien 67', City'Sandnes'
WHERE LastName'Tjessem' AND FirstName'Jakob'
The "Persons" table will now look like this:
!*Id Lastame 7stame Add7ess Cty
Hansen Ola Timoteivn Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 2 Stavanger
4 Nilsen Johan Bakken 2 Stavanger
5 Tjessem Jakob Nissestien 67 Sandnes



$"L DELETE Example
The "Persons" table:
!*Id Lastame 7stame Add7ess Cty
Hansen Ola Timoteivn Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 2 Stavanger
4 Nilsen Johan Bakken 2 Stavanger
5 Tjessem Jakob Nissestien 67 Sandnes
Now we want to delete the person "Tjessem, Jakob" in the "Persons" table.
We use the Iollowing SQL statement:
DELETE FROM Persons
WHERE LastName'Tjessem' AND FirstName'Jakob'
The "Persons" table will now look like this:
!*Id Lastame 7stame Add7ess Cty
Hansen Ola Timoteivn Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 2 Stavanger
4 Nilsen Johan Bakken 2 Stavanger




SQL AVG() Syntax
SLLLC1 AvC(colum_ame) l8CM Lable_ame

SLLLC1 CusLomer l8CM Crders
WPL8L Crderrce(SLLLC1 AvC(Crderrce) l8CM Crders)



$"L MAX() $y3tax
SLLLC1 MAx(colum_ame) l8CM Lable_ame


$"L MI() $y3tax
SLLLC1 Mln(colum_ame) l8CM Lable_ame


$"L $&M() $y3tax
SLLLC1 SuM(colum_ame) l8CM Lable_ame

SQL GR&! BY Syntax
SLLLC1 colum_ame aggregaLe_ucLo(colum_ame)
l8CM Lable_ame
WPL8L colum_ame operaLor value
C8Cu 8? colum_ame


$"L GR&! BY Example
We have the Iollowing "Orders" table:
C_Id CrderDate Crderr|ce Customer
1 2008/11/12 1000 Pase
2 2008/10/23 1600 nlse
3 2008/09/02 700 Pase
4 2008/09/03 300 Pase
3 2008/08/30 2000 !ese
6 2008/10/04 100 nlse
Now we want to Iind the total sum (total order) oI each customer.
We will have to use the GROUP BY statement to group the customers.
We use the Iollowing SQL statement:
SLLLC1 CusLomerSuM(Crderrce) l8CM Crders
C8Cu 8? CusLomer
The result-set will look like this:
Customer SUM(Crderr|ce)
Pase 2000
nlse 1700
!ese 2000
We can also use the GROUP BY statement on more than one column, like this:
SLLLC1 CusLomerCrderuaLeSuM(Crderrce) l8CM Crders
C8Cu 8? CusLomerCrderuaLe

You might also like