SQL Advanced 2
-
Upload
ashleyrulzzzzzzz -
Category
Documents
-
view
224 -
download
0
Transcript of SQL Advanced 2
-
8/9/2019 SQL Advanced 2
1/6
Experiment No. – 9, 10
(2)Aim: To Study SQL Advn!ed.
T"eory:
SQL Constraints
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 CREATE TABLE statement) or after the
table is created (with the ALTER TABLE statement).
e will focus on the following constraints!
• "#T "$LL
•
$"%&$E• 'R%AR *E
• +#RE%," *E
• C-EC*
• E+A$LT
1.SQL NOT NULL Constraint
The "#T "$LL constraint enforces a column to "#T accept "$LL /alues.
The "#T "$LL constraint enforces a field to always contain a /alue. This means that you cannot insert a
new record0 or update a record without adding a /alue to this field.
The following 1&L enforces the 2'3%d2 column and the 2Last"ame2 column to not accept "$LL /alues!
CREATE TABLE B%T
(
'3%d int "#T "$LL0
Last"ame /archar(455) "#T "$LL0
+irst"ame /archar(455)0
Address /archar(455)0City /archar(455)
)
SQL# $%EATE TA&LE '&T
2 (
*+d int NT N-LL,
L/tNme vr!"r(2) NT N-LL,
-
8/9/2019 SQL Advanced 2
2/6
ir/tNme vr!"r(2),
Addre// vr!"r(2),
3 $ity vr!"r(2)
4 )
9 5
T67e !reted.
SQL# in/ert into '&T 2 v7ue/(1,88,8/n8,8/in8,8mum6i8)5
in/ert into '&T
E%%% t 7ine 1:
%A;0100: !nnot in/ert N-LL into ( !reted.
2.SQL UNIQUE Constraint
The $"%&$E constraint uni6uely identifies each record in a database table.
The $"%&$E and 'R%AR *E constraints both pro/ide a guarantee for uni6ueness for a column or
set of columns.
A 'R%AR *E constraint automatically has a $"%&$E constraint defined on it.
"ote that you can ha/e many $"%&$E constraints per table0 but only one 'R%AR *E constraint per
table.
Syntx:
CREATE TABLE table3name
(
'3%d int "#T "$LL $"%&$E0
Last"ame /archar(455) "#T "$LL0
+irst"ame /archar(455)0
Address /archar(455)0City /archar(455)
)
SQL# $%EATE TA&LE '&T1
2 (
*+d int NT N-LL -NQ-E,
L/tNme vr!"r(2) NT N-LL,
ir/tNme vr!"r(2),
Addre// vr!"r(2),
-
8/9/2019 SQL Advanced 2
3/6
3 $ity vr!"r(2)
4 )
9 5
T67e !reted.
3. SQL PRIMARY KEY Constraint on CREATE TABLE
The following 1&L creates a 'R%AR *E on the 2'3%d2 column when the 2B%T42 table is created!
CREATE TABLE B%T4
(
'3%d int "#T "$LL 'R%AR *E0
Last"ame /archar(455) "#T "$LL0+irst"ame /archar(455)0
Address /archar(455)0
City /archar(455))
4. SQL FOREIN KEY Constraint
A +#RE%," *E in one table points to a 'R%AR *E in another table.
Let7s illustrate the foreign 8ey with an e9ample. Loo8 at the following two tables!
The 2B%T2 table!
*+d L/tNme ir/tNme Addre// $ity
: An/ri 1ana 1a8ina8a mumbai
4 1hai8h 1ira; Andheri mumbai
The 2#rders2 table!
+d rderNo *+d
:
-
8/9/2019 SQL Advanced 2
4/6
SQL FOREIN KEY Constraint on CREATE TABLE
The following 1&L creates a +#RE%," *E on the 2'3%d2 column when the 2#rders2 table is created!
CREATE TABLE #rders
(#3%d int "#T "$LL 'R%AR *E0
#rder"o int "#T "$LL0
'3%d int +#RE%," *E RE+ERE"CE1 'ersons('3%d)
)
!.SQL C"ECK Constraint
The C-EC* constraint is used to limit the /alue range that can be placed in a column.
%f you define a C-EC* constraint on a single column it allows only certain /alues for this column.
CREATE TABLE B%T
(
'3%d int "#T "$LL C-EC* ('3%d4)0
Last"ame /archar(455) "#T "$LL0
+irst"ame /archar(455)0
Address /archar(455)0City /archar(455)
)
SQL# $%EATE TA&LE '&T
2 (
*+d int NT N-LL $?E$@ (*+d#2),
L/tNme vr!"r(2) NT N-LL,
ir/tNme vr!"r(2),
Addre// vr!"r(2),
3 $ity vr!"r(2)
4 )
9 5
T67e !reted.
SQL# in/ert into '&T
2 v7ue/(1,8/"i"8,8/n8,8/in8,8mum6i8)5
in/ert into '&T
E%%% t 7ine 1:
%A;02290: !"e! !on/trint (S$TT.SS+$000) vio7ted
#. T$% &ROP TABLE Stat%'%nt
-
8/9/2019 SQL Advanced 2
5/6
The R#' TABLE statement is used to delete a table.
R#' TABLE table3name
(.T$% &ROP &ATABASE Stat%'%nt
The R#' ATABA1E statement is used to delete a database.
R#' ATABA1E database3name
). T$% TRUNCATE TABLE Stat%'%nt
hat if we only want to delete the data inside the table0 and not the table itself
Then0 use the TR$"CATE TABLE statement!
TR$"CATE TABLE table3name
*.T$% ALTER TABLE Stat%'%nt
The ALTER TABLE statement is used to add0 delete0 or modify columns in an e9isting table.
SQL ALTER TABLE S+nta,
To add a column in a table0 use the following synta9!
ALTER TABLE table3name
A column3name datatype
To delete a column in a table0 use the following synta9 (notice that some database systems don7t allow
deleting a column)!
-
8/9/2019 SQL Advanced 2
6/6
ALTER TABLE table3name
R#' C#L$" column3name
$on!7u/ion: Thus we ha/e successfully implemented 1&L ad/anced 6ueries.