Data Definition Language (DDL) Commands Integrity Constraints
Data Definition Language (DDL) Commands Integrity Constraints
NO: l
DATE: DATA DEFINITION LANGUAGE (DDL)
OS I l ,,. ' COMMANDS AND INTEGRITY
CONSTRAINTS
AIM:
To execute and verify the Data Definition Language commands and Integrity
constraints in RDBMS
PROCEDURE:
SQL: Structured Query Language isa nonproced4ral language used for storing and
managing data in relational database management system (RDBMS).
5 TRUNCATE
\~\U'\£ .~ ~ ~ , . ~½~ 0 - ~
'u' ~ ~ rt)~ ~
~ Go. wtthol.U-
6 DESC
\J eM'. ~flcuY \JQ. i
~~ u,~a D
~ ~ ~ O~ew tti~
(.,.,Lt 0.
Integrity Constramts
• Integrity constraints are used to ensure accuracy and consistency of data in a
relational database
• Data integrity is handled in a relational database through the concept of
referenti al integrit) .
• Many types of integrity constraints play a role in referential integrity (RI)
Primary Key Constraints
• Primary key is the term used to identify one or more columns in a table that
make a row of data unique.
• Althoug h the primary key typically consists of one column in a table, more
than one column can comprise the primary key
• The primary key is assigned at table creation in any of the following two forms.
The followin g example identifies the EMP _ID column as the PRIMARY KEY for
the EMPLOYEES table : (The primary key in this case is an implied constraint).
d by either of th c
A primary key that consists of more than one colum n can be define
following methods:
To define a PRIMARY KEY al table lcvcl(Composite Primary Key):
Example:
CREATE TABLE EMPLOYEE TBL
(EMP_ID CHAR(9) NOTNULL PRJMARYKEY,
EMP_NAME VARCHAR (40) NOT NULL ,
EMP_ST_ADDR VARCHAR(20) NOTNULL,
EMP_PHONE INTEGER(l0) NULL UNIQUE);
Example:
CREATE TABLE E\1PLOYEE_TBL
1E\ IP ID CHA R(9) NOT Nl.JLL,
E\IP \"A~1E V.A.RCHAR2(40) N0T NU LL
E\lP =ST _AD DR YAR CHA R2(2 0) ~0T
NU LL
HvfP llP NUMBER(5) NOT NULL.
PAY RA TE NUMBER( 4,2) NOT NULL,
EMP _PHONE NUMBER(lO) NULL, PRIMARY KEY (EMl'_ID),
CONSTRAINT CHK_EMP_Z IP CHECK ( EMP_ZIP = '46234');
Dropping Constraints
• An) constraint that you have defined can be dropped using the ALTER TABLE
command with the DROP CONSTRAINT option
• To drop the primary key constraint in the EMPLOYEES table, you can use the
following command:
ALTER TABLE EMPLOYEES DROP CONSTRAINT EMPLOYEES_PK;
(Or)
ALTER TABLE EMPLOYEES DROP PRIMARY KEY;
DDL Commands:
I. Create a table PRODUCT with the attributes product id. vendor id. product name, price,
quantity.
Query:
CP.EA1E t:~ p-totlu.tJ:;; t P ~ - td.. LIU:: , \Jemloi)l.,.. ~ Lf\k / Pfoelut t-~
Query: l
Query:
~\..1Ef< 6~ Pioduci- t>Ror {ow.mo \)~ ~ - \t:l ;
5. Change the name of the existi ng table.
Query:
Integrity Constraints:
1. Write a Quer y to set prima ry key for the
suitab le attribute for the produ ct table .
Query:
(:, ~ ~ l>'t &~ 1- ( , ()"i' o~t~ ~ I {rt oclutt noftl e UM.cl--ort C.n;,
f>'tte_ \M-l ~ •tf2k, ~f\V" (o.iA A- ~l:11
pi ·~ ~ t ()"t~elutt-\c.l )) ;
2. Create a table VEN DOR _DET AILS and establish refere
nce to PRO DUC T table.
Query:
t"i- e~ ~ '\)~i l ~~~ \lo.n~C-m'>, \)e,J.z,"f \d-- tft.(.,,.
Pio c4,u::h6 ~ , c~ l"'\h) 't ~n,t ~ \ t\.O"f ei'~ ~tn i,d .u. .cH J\~
~
P't "~~ c pt~d.m:\.- \ll~\ ; .
3. Appl y the check const raint for any attribute.
Quer y:
Additional Questions:
, age,phone
I Create a tab le person with the following attributes Name of the person
no,adhar id. address.
Query:
cuw,
4. Add a constraint for an) attribute whose values should not be empty.
Query:
5. Apply the appropriate constraint for any attribute such that it should not accept any
duplicate values.
Query:
RESULT
Thus the data definition language commands and integrity constraints were
successfully executed and verified in RDBMS.
MARK ALLOCATION
CONTINUOUS INTERNAL ASSESSMENT
Conduct of Experiment (20)
Record & Result (15)
Viva (15)
--
Total (SO)
·-
Signature of the Faculty with Date
-- - ·-
DDL (om~ (OLLcput) ·.
i ~ \Q\o_\..e l~l~'n.te~
No..fTuQ.
- --
f\) 1..1.il '? , ~re
~
---- ,__ - -·
PlcJ..lU: b - ~iel f\.J.u.m,b-e>, <38 )
P0t.tue. ~~ \ ( ~ ")
(lµa.n)::_'L~ f\A.lrnb eh C~ )
OJ>ON~
~~ c,~>
\\ffi"t thoJ\ Cl [ ~o )
\f ~ - f\D.ffl.E:
4~ '"1C\.b!J0 0-1.t&-fld
~ ( 1 .) )
L'lJ-_\l\n.td~
\) ~V\t\CII.TT - f1Clll"'-e
\lot1Ch® 2 Cso)
5 ~ct.bl() '1'f'J1o.-~
d ,0-.bue. c~
3 'l:~ ~
l+ - "lnh>-£ ~
s- '\~ ~~
(ill._~ J-
~- 7Qhl£
l \~ ( ~
NQm.R.
.....__ - .... ,-..,
fUul,..,
- - _:_~
tvDJYLR \)~(_~~
~t%')
~
p'f'O<U' _{)O m.vro~ t~\
Ao.clhan ~1J.. \lcl}-1.c)'O.h(~)
~().)-\~(~)
9~5
?.. . f'tlo.me n,I.JJJ?
--T~'('
...... ,..
vohcJ1W1 r ro)
f\lC\.YT1J{\
t'Ul i1lJo 'th rg~)
re-e
ruuno~-, tg___,)
~~ft(? .(\O
l hx, ( C(l'i)
Virulhl\.)\_ tJ nl01 mm L
\) cu '('h..Q)i LsD)
~rlthl.Eb~
y_ D t-Sc y0U"O 0
~~
nlctr11,e__ r\WJ 1
-- - --
\jar-t&on ()())
ru~
fU.trrJ:, 'eh t <3» )
~
~ Qi\ C~)
?\on£ .SW
fi\)Cl1 n)Ull
c)vv"\. c. t+ ~ ')
A rul\-m,_fi,l-
\JOS\. ChDY\ (.)())
{)\~)
c5 ,Clble ~ -
EX.NO: 2 DATA MANIPULATION LANGU AGE (DML),
DATE:
IU0Z/i1
DATA CONTR OL LANGUAGE (DCL) AND
TRANSACTION CONTR OL LANGU AGE
(TCL) COMMA NDS
AJM
To study the various DML, DCL and TCL commands and implement them on
the database.
PROCEDURE
Data Manipulat ion Language
Select
~
~eu:-\. On
{<,'e.-l.ile.Ueb
5~-1- ,e_u--u,rn \:..o..hl_e.n c'.l)-(1R
tr4'm do l:el::nhe,
Update :.v1 r\.n__9
~ ~hl.en.ClJ'"\.E? &ek- mocli~y 'Q 1 ~ Vi
C.Ow.mna.me_ ::. ~, \ l ~ '1 w ~ Q, ta.hle
' ' lond:t tld n "
Delete O~--e ~~ ~ OeLe}-,e.h w Q.X..~tv1g
Whet-e C('.)lLu-nfl '1 CUYt.€ ~ I \Jl\.lu-e 'I 1fuJ~ .
Pri\'ik~l'"
--
l)cscription
------
command
<;,-nnt c~1.'lllf\l- Pi,\J \..l~Ml"J)1e Nl :t\: u, LU:,.e,d l~ g tu e P& IJfU C,S,
~1' \Jh~• l(H'< I'(' l l'I l '11 ())\ nru,r,(> • lo Hl-0 u>.,,q)\ lo Cltc£'.).S H·w cloJ.A
~f\ \ h,e__Ji,_~_Q_ - - - -
(x,e t~H,Q f)'1'll)d~~Q ~1(\ , Tl,Q c,n .., \.:- ( ,S, l Lb <'rJ. fi) Vfo ~J:?.mt.R
----------
l t\_b u. nMn e l1Ju,r"
1
Lili€.WlM fle , PW\JTl.J S 5\on rp~n, ~ CJ.bet'!.
~e ~ "'iolU,D-.Cl::u:, r,
QUERIES:
DATA l\1ANI PULATJON LANGUAGE
INSERT
j ~- Write a quer) to add live r;wn.ls to the existing table PRODUCT.
Query: r'1be\.fl:: IJU.O Pi~ \h.lJJ...eb l I , , ~ 1 ~00 1 It- J ;
1
' Pe;i
1
VU)e)u: ·~
tl'fcru~c..t \.l:.tllJ,Q).) L c:, , U10fOIJ'l.,t-Q, "" , 1'..Z-0 1 ..,_ J •
~ \JU:O P1 ocllu:t:- ( t d. ' n (JJ")1R ' P1 ·tt~.e > \Ja.1.u.eb ( , o , l lrlJ:2 ' , ~ a )
3. A company has introduced three new products. Add its details using a single insert
statement.
Query:
~ .vtko "1~ \hJ-u..€h ( ~ '1 0 I 'cf rJ~ f/ <f VI D 1 el p i ~ I cf QWVLtti-y ),,
SELECT
1. Display all the records in the product table.
Query:
s~
.
i'-8noff1 pto~;
2. Display any two columns of the table in descending order of product price.
Query:
Eie.1.xvd:. Pf~_(lru
.$~ no.m.e / C\_JJ.OJU:~ ~,r,. ~ Oicleh~ pilte_ o--el>C~
3. Display the attribute values of a table with in a particular price range.
Query:
- - - - - - _ £ - - --------:----:-::-~---;-~----:-;---,
5. Displn) onl) the llrsl l'ft r~cords from the tab le by converting NULL value to the
sric1.:i1kd , ..,luc".
Qucr~:
(\ It
lPDATE
1. Copy the content of the product table in a new table and update the rate of all products
b) 2%.
Que11:
'1
Write a SQL statement to increase the rate of those products by 50 rs whose rate is less
than 500 rs.
Query:
'l,\ftlnle. f>i~Lu.a- 5.ot ptu,e ':,
Pitt.€ ;- So w heuQ ( f!(\.Clf> L 5o o)~
3 ts. 15% on grocer y on
· Write an SQL query to give 20% reduction on medicinal produc
5 % reduct ion on stationary.
Query :
4. Consi der that today is book vendors birthday , add one to his AGE.
Query :
DELETE
1 Elimin ate the produc t which has minimum price.
Query:
~ &telf'7 Pt£riurl- wh.eu Q r1v:.e ~ l ~ rrun (Pf~ >
~ P1~-);
15% GST.
2. It's time for budget cuts. Remove a11 products which have more than
Query :
Query :
\:.eu)l._,Q i>iMJ.utl: ~p ul. I.
~
DCL COMMANDS
GRANT
1. Admin wants to share his database to the public and also allow them to update his
database.
Query:
2. Vendor2 wants to share his database to the public and allow them only to view his
database.
Query:
REVOKE
TCL COMMANDS
COMMIT
1. Make the changes done in the transaction permanent.
Query:
ROLLBACK
SAVEPOlNT
Additional Questions:
1. Create a banking database with the necessary attributes to maintain the balance and
loan details in separate tables.
Que~·:
t _ ~ 6:uil.e. ~ pJ •lfU:- r no.rn;e Lh)-1 Cttll.)'"\ ( ~o ") ' W-OJ"IIJ..-; fLl- ") r•
{'\~"-'Q_ ~le l.Oc,..J\ ( w uu- t no...tn..R uvichcm ( 2c,) , \ can -Mltl~ '(<U:-) ~
2. Identify the customer having less than the minimal balance and close thejr account.
Que~·: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\So . \S
3
s -Soo ,Sen.leb tO , \
l
3. LCL\ou€ Lf~cl
Pi ~cllul ·~ u~(l~ - x.J--
I ~ ti 4
2 noo Pt<\c/ I ( ?-
b bc6 dCl--~eh ~
7 ~DO rct'e.-~ ID
4~ lctbl.JQ. Uf&tl-td'
J ) ~ t.6~~~:
l. "TLLo\JQ ~
~ ~ o.bJJe
r- ~°'
'3. '16.h,lQ ~
.
\\ ~ -\&- pr~..n ~ ~
1..()0
p QI\. '?..C().t--..
~o PQflti \ 60. 0 7 1~
7 ~c) no~ '141 ~ )
t>of) lh~~ l')..t) .. ~J)
l~DO f~
§C ,{) Lt 2
Sa'o POflQ)l Eb . D4
;~ 1.t, ,\:> l
SDt'>
RESULT
fhus the Data M unipulation Language (DML), Data Control Language (DCL)
and Transaction Control Language (TCL) commands were succcssfull: executed and
verified in RDBMS.
MARK ALLOCATION
CONTlNUOUS INTERNAL ASSESSMENT
Conduct of Experiment (20)
Viva (15)
Total (SO)