ISM LAB File Sample-1
ISM LAB File Sample-1
Paper Code:
BBA307AcademicSession:20
23-2024
DepartmentofBusinessAdministration
SubmittedTo: Submittedby:
CourseObjectiveandProgramObjective
COPOISMLab PO PO PO PO PO PO PO PO
1 2 3 4 5 6 7 8
CO1 Select the relevant data 3 1 3 2 3 1 2 2
fordecisionmakingusingSQ
L
CO2 Understandthe relevance ofE- 3 3 3 2 3 1 2 3
RModels
CO3 CreateandManipulate 3 3 3 2 3 1 2 3
databases
1
MaharajaAgrasenInstituteofManagementStudies
AffiliatedtoGGSIPUniversity,Recognizedu/s2(f)ofUGCRecognizedbyBa
rCouncilof India,ISO9001:2015CertifiedInstitutionSector22,
Rohini,Delhi-110086, India;www.maims.ac.in
7 Assignment5
8 Assignment6
9 Assignment7
10 Assignment8
11 Assignment9
12 Assignment10
13 Assignment11
14 Assignment12
15 Assignment13
16 Assignment14
17 Assignment15
18 Assignment16–NestedQuery
19 Assignment17–NestedQuery
20 Assignment18– JoinQuery
21 Assignment19– JoinQuery
22 Assignment20 –ERDiagram
23 Assignment21 –ERDiagram
24 Assignment22 –ERDiagram
2
MaharajaAgrasenInstituteofManagementStudies
AffiliatedtoGGSIPUniversity,Recognizedu/s2(f)ofUGCRecognizedbyBa
rCouncilof India,ISO9001:2015CertifiedInstitutionSector22,
Rohini,Delhi-110086, India;www.maims.ac.in
ERDiagram,DBMS basicSQL
Followingarethemaincomponents anditssymbolsinERDiagrams:
• Rectangles:ThisEntityRelationshipDiagramsymbolrepresentsentitytypes
• Ellipses:Symbolrepresentattributes
• Diamonds:Thissymbolrepresentsrelationship types
• Lines:Itlinksattributestoentitytypesandentitytypeswithotherrelationshiptypes
• Primarykey:attributes areunderlined
• DoubleEllipses:Representmulti-valuedattributes
WHATISENTITY?
A real-world thing either living or non-living that is easily recognizable and non-recognizable. It
isanything in the enterprise that is to be represented in our database. It may be a physical thing
orsimplyafact abouttheenterpriseoran event thathappens inthereal world.
An entity can be place, person, object, event or a concept, which stores data in the database.
Thecharacteristics of entities are must have an attribute, and a unique key. Every entity is made up
ofsome‘attributes’whichrepresentthat entity.
Examplesofentities:
• Person:Employee,Student,Patient
• Place:Store,Building
• Object:Machine,product,and Car
3
MaharajaAgrasenInstituteofManagementStudies
AffiliatedtoGGSIPUniversity,Recognizedu/s2(f)ofUGCRecognizedbyBa
rCouncilof India,ISO9001:2015CertifiedInstitutionSector22,
Rohini,Delhi-110086, India;www.maims.ac.in
• Event:Sale,Registration,Renewal
• Concept:Account, Course
WeakEntities
Aweakentityis a typeofentity whichdoesn’thaveitskeyattribute.Itcanbeidentifieduniquely
byconsideringtheprimarykey ofanotherentity.Forthat,weakentitysets needtohaveparticipation.
Attributes
Itisasingle-valuedpropertyofeitheranentity-typeorarelationship-type.
Forexample,alecturemighthaveattributes: time,date,duration,place,etc.Anattributein
ERDiagramexamples, is represented byan Ellipse
TypesofAttributes Description
Q.2. ConvertERtotabledatabaseofanyspecificERDiagram.
4
MaharajaAgrasenInstituteofManagementStudies
AffiliatedtoGGSIPUniversity,Recognizedu/s2(f)ofUGCRecognizedbyBa
rCouncilof India,ISO9001:2015CertifiedInstitutionSector22,
Rohini,Delhi-110086, India;www.maims.ac.in
Entityrelationshipdiagramisthegraphicalrepresentationofentitiesandrelationshipsamongthoseentities
in thedatabase.
Example
Schema:Employee(Emp_No,Emp_name,Salary)
Emp_No Emp_name Salary
Schema:Department(Dept_id,Dept_name)
Emp_No Dept_id since
Schema:Worksin(Emp_no,Dept_id,since)
Q.3. What are the types of SQL? Briefly Explain DDL, DML & SELECT
command.TypesofSQL Commands:-
5
MaharajaAgrasenInstituteofManagementStudies
AffiliatedtoGGSIPUniversity,Recognizedu/s2(f)ofUGCRecognizedbyBa
rCouncilof India,ISO9001:2015CertifiedInstitutionSector22,
Rohini,Delhi-110086, India;www.maims.ac.in
1. DataDefinitionLanguage(DDL)
DDL changes the structure of the table like creating a table, deleting a table, altering a
table,etc. All the command of DDL are auto-committed that means it permanently save all
thechangesin thedatabase.
Herearesomecommands thatcomeunderDDL:
a. CREATEItisusedtocreateanewtableinthedatabase.Synta
x
CREATETABLEtable_name(colu
mn1datatype,column2data
type,column3datatype,
.....
columnNdatatype,
PRIMARYKEY(oneormorecolumns)
);
Example:
CREATETABLECUSTOMERS(
ID INTNOTNULL,
NAME VARCHAR(20)NOTNULL,
AGE INTNOTNULL,
ADDRESS CHAR(25),
6
MaharajaAgrasenInstituteofManagementStudies
AffiliatedtoGGSIPUniversity,Recognizedu/s2(f)ofUGCRecognizedbyBa
rCouncilof India,ISO9001:2015CertifiedInstitutionSector22,
Rohini,Delhi-110086, India;www.maims.ac.in
SALARY DECIMAL(18,2),
PRIMARYKEY(ID)
);
b. DROP:Itisusedtodeleteboththestructureandrecord stored inthetable.
Syntax
DROPTABLEtable_name;
Example
DROPTABLECUSTOMERS;
c. ALTER: It is used to alter the structure of the database. The ALTER TABLE command can add
ordelete columns, create or destroy indexes, change the type of existing columns, or rename columns
orthetableitself.
TheALTERTABLEcommandcanalsochangecharacteristicsofatablesuchasthestorageengineusedforthe
table.
Syntax
FollowingisthebasicsyntaxofanALTERTABLEcommand−
ALTERTABLEtable_name[alter_option...];
ALTERTABLE−ADDColumn
If you need to add a new column to a table, you should use the ADD COLUMN option along with
ALTERTABLEstatementasshown below−
ALTERTABLEtable_nameADDcolumn_namedatatype;
Example
FollowingistheexampletoADDaNewColumntoanexistingtable−
ALTERTABLECUSTOMERSADDSEXchar(1);
ALTERTABLE−DROPCOLUMN
Ifyouneedtodropanexistingcolumnfromatable,youshouldusetheDROPCOLUMNoptionalongwithALTERTA
BLEstatementasshown below.
ALTERTABLEtable_nameDROPCOLUMNcolumn_name;
Example
FollowingistheexampletoDROPsexcolumnfromtheexistingtable.
ALTERTABLECUSTOMERSDROPCOLUMNSEX;
7
MaharajaAgrasenInstituteofManagementStudies
AffiliatedtoGGSIPUniversity,Recognizedu/s2(f)ofUGCRecognizedbyBa
rCouncilof India,ISO9001:2015CertifiedInstitutionSector22,
Rohini,Delhi-110086, India;www.maims.ac.in
ALTERTABLE−ADDPRIMARYKEY
Syntaxtoaddaprimarykeyinanexistingtableofadatabase−
ALTERTABLEtable_name
ADD CONSTRAINT
constraint_namePRIMARYKEY(colu
mn1,column2...);
Example
Before we add a primary key to an existing table, first let's create a new table called EMPLOYEES
asfollows:
ALTERTABLEEMPLOYEES
ADDCONSTRAINTMyPrimaryKeyPRIMAR
YKEY(ID);
ALTERTABLE−DROPPRIMARYKEY
Syntaxtodeleteaprimarykeyfromanexistingtableofadatabase−
ALTERTABLEtable_nameDROPPRIMARYKEY;
Example
FollowingquerydeletesprimarykeyconstraintfromthecolumnIDofEMPLOYEEStable−
ALTERTABLEEMPLOYEESDROPPRIMARYKEY;
ALTERTABLE−ADDCONSTRAINT
Syntaxtoaddauniqueconstrainttoacolumnofanexistingtable−
ALTERTABLEtable_name
ADD CONSTRAINT
constraint_nameUNIQUE(column1,
column2...);
Example
FollowingqueryaddsUNIQUEconstrainttothetableCUSTOMERS−
ALTER TABLE EMPLOYEES ADD CONSTRAINT
CONSTUNIQUE(NAME);
ALTERTABLE−DROPCONSTRAINT
Syntaxtodropauniqueconstraintfromanexistingtable−
ALTERTABLEtable_nameDROPCONSTRAINT
constraint_name;
Example
FollowingqueryaddsUNIQUEconstrainttothetableCUSTOMERS−
ALTERTABLEEMPLOYEESDROPCONSTRAINTCONST;
8
MaharajaAgrasenInstituteofManagementStudies
AffiliatedtoGGSIPUniversity,Recognizedu/s2(f)ofUGCRecognizedbyBa
rCouncilof India,ISO9001:2015CertifiedInstitutionSector22,
Rohini,Delhi-110086, India;www.maims.ac.in
ALTERTABLE−RENAMECOLUMN
Followingisthesyntaxtorenameacolumnnameofanexistingtable−
ALTERTABLEtable_name
RENAMECOLUMNold_column_nametonew_column_name;
Example
FollowingqueryrenamesNAMEcolumnintableCUSTOMERS−
ALTERTABLECUSTOMERSRENAMECOLUMNnametofull_name;
ALTERTABLE− MODIFYDATATYPE
FollowingisthesyntaxtochangethedatatypeofanycolumninMySQL,MSServerandOracle.
ALTERTABLEtable_nameMODIFYCOLUMNcolumn_namedatatyp
e;
Example
FollowingquerymodifiesdatatypeofSALARYcolumninMySQLCUSTOMERStable−
ALTERTABLECUSTOMERSMODIFYCOLUMNEIDNumber(18,0);
c.RENAMETABLECommand:YoucanchangeaMySQLtablenameusingSQLRENAMETABLE
statement.
Syntax
RENAMETABLEtable_nameTOnew_table_name;
Syntax
FollowingisthesyntaxoftheSQLALTERTABLEcommandtorenameanexistingtable−
ALTERTABLEtable_nameRENAME[TO|AS]new_table_name
Example
FollowingSQLALTERTABLEstatementwillchangethetablenamefromBUYERStoCUSTOMERS.
ALTERTABLEBUYERSRENAMETOCUSTOMERS;
d.TRUNCATE:Itisusedto deleteallthe rows from the tableand freethespacecontaining thetable.
Syntax:
TRUNCATETABLEtable_name;
Example:
TRUNCATETABLECUSTOMERS;
2. DataManipulationLanguage
DMLcommandsareusedtomodifythedatabase.It isresponsible
forallformofchangesinthedatabase.
9
MaharajaAgrasenInstituteofManagementStudies
AffiliatedtoGGSIPUniversity,Recognizedu/s2(f)ofUGCRecognizedbyBa
rCouncilof India,ISO9001:2015CertifiedInstitutionSector22,
Rohini,Delhi-110086, India;www.maims.ac.in
ThecommandofDMLisnotauto-committed thatmeansitcan'tpermanentlysaveall
thechanges in the database. They can be rollback. Here are some commands that come
underDML:
INSERT
UPDATE
DELETE
ThereisanothersyntaxofINSERTINTOstatementwhereyoucanspecifyonlycolumnvalueswithoutcolumnn
ames.But,makesuretheorderofthevaluesisinthesameorderasthecolumnsinthetable.
Or
FollowingissecondsyntaxoftheSQLINSERTQuery−
INSERTINTOTABLE_NAMEVALUES(value1,value2...valueN);
Example
ThefollowingSQLINSERTINTOstatementswillcreatethreerecordsintheemptyCUSTOMERStable.
INSERTINTOCUSTOMERS(ID,NAME,AGE,ADDRESS,SALARY)
VALUES(1,'Ramesh',32,'Ahmedabad',2000.00);
10
MaharajaAgrasenInstituteofManagementStudies
AffiliatedtoGGSIPUniversity,Recognizedu/s2(f)ofUGCRecognizedbyBa
rCouncilof India,ISO9001:2015CertifiedInstitutionSector22,
Rohini,Delhi-110086, India;www.maims.ac.in
INSERTINTOCUSTOMERS(ID,NAME,AGE,ADDRESS,SALARY)VALUES
(4,'Chaitali',25,'Mumbai',6500.00),
(5,'Hardik',27,'Bhopal',8500.00),
(6,'Komal',22,'Hyderabad',4500.00);
b. UPDATE: This command is used to modify the existing records in a table. This command is a
partof Data Manipulation Language (DML), as it only modifies the data present in a table without
affecting thetable'sstructure.
Syntax:
UPDATE table_name SET column1 = value1, column2
=value2,..., columnN = valueN WHERE
[condition];Example:
IfyouwanttomodifyalltheAGEandtheSALARYcolumnvaluesintheCUSTOMERStable
Example:
DELETEFROMCUSTOMERSWHEREID=6;
Todeletemultiplerowsfromatable
DELETEFROMCUSTOMERSWHEREAGE>25;
3. DataControlLanguage
DCLcommandsareusedtograntandtakebackauthorityfromanydatabaseuser.Hereare
somecommands that comeunderDCL:
o Grant
o Revoke
a. Grant:
Itisusedtogiveuseraccessprivilegestoadatabase.Example
GRANTSELECT,UPDATEONMY_TABLETOSOME_USER,ANOTHER_USER;
11
MaharajaAgrasenInstituteofManagementStudies
AffiliatedtoGGSIPUniversity,Recognizedu/s2(f)ofUGCRecognizedbyBa
rCouncilof India,ISO9001:2015CertifiedInstitutionSector22,
Rohini,Delhi-110086, India;www.maims.ac.in
b. Revoke:Itisusedtotakebackpermissions
fromtheuser.Example
REVOKESELECT,UPDATEONMY_TABLEFROMUSER1,USER2;
4. TransactionControlLanguage
TCL commands can only use with DML commands like INSERT, DELETE and UPDATE
only.Theseoperationsareautomatically
committedinthedatabasethat'swhytheycannotbeusedwhilecreating tables ordropping
them.Herearesome commandsthat comeunderTCL:
COMMIT
ROLLBACK
SAVEPOINT
a. Commit:Commitcommandisusedtosaveallthetransactions tothedatabase.
Syntax:
COMMIT;
Example:
DELETEFROMCUSTOMERSWHEREAGE=25;
COMMIT;
b. Rollback:Rollbackcommandisusedtoundotransactionsthathave notalreadybeensaved
to thedatabase.
Syntax:
ROLLBACK;
Example:
DELETEFROMCUSTOMERSWHEREAGE=25;
ROLLBACK;
c. SAVEPOINT:Itisusedtorollthetransactionbacktoacertainpointwithoutrollingbacktheentiretran
saction.
Syntax:
SAVEPOINTSAVEPOINT_NAME;
5. DataQueryLanguage:DQLisusedtofetchthedatafromthedatabase.
Syntax
ThebasicsyntaxoftheSELECTQueryisasfollows−
SELECTcolumn1,column2,columnNFROMtable_name;
12
MaharajaAgrasenInstituteofManagementStudies
AffiliatedtoGGSIPUniversity,Recognizedu/s2(f)ofUGCRecognizedbyBa
rCouncilof India,ISO9001:2015CertifiedInstitutionSector22,
Rohini,Delhi-110086, India;www.maims.ac.in
Here,column1,column2...arethefieldsofatablewhosevaluesyouwanttofetch.Ifyouwanttofetchallthecolumnsavailabl
einatable,thenyoucanusethefollowingsyntax−
SELECT*FROMtable_name;
RetrievingSelectedFields/Columns
ThefollowingstatementfetchestheID,NameandSalaryfieldsoftherecordsavailableinCUSTOMERStable.
SELECTID,NAME,SALARYFROMCUSTOMERS;
CopyingSpecificRecords
WecanalsousetheSQLSELECTINTOstatementwithaWHEREclausetocreateanewtableandcopyspecificro
wsfromanexisting tableintoit.
Syntax
FollowingisthesyntaxforusingSELECTINTOstatementwithaWHEREclause−
SELECT*INTOnew_table_nameFROMexisting_table_nameWHEREcondition;
Example
Using the following query we are creating a new table called NameStartsWith_K that includes all
columnsfromtheCUSTOMERStable,butitonlystorestherecordsofthecustomerswhosenamestartswith"k".
SELECT*INTONameStartsWith_KFROMCUSTOMERSWHERENAMELIKE'k%';
TheSQLORDERBYClause
The SQL ORDER BY clause is used to sort the data in either ascending or descending order, based on one
ormorecolumns.
Syntax
ThebasicsyntaxoftheORDERBYclauseisasfollows−
SELECTcolumn-listFROMtable_name[ORDERBYcolumn1,column2,..columnN]
[ASC|DESC];
Where,column-listislistofthecolumnswewanttoretrieve;andASCorDESCspecifiesthesortorder.Example
13
MaharajaAgrasenInstituteofManagementStudies
AffiliatedtoGGSIPUniversity,Recognizedu/s2(f)ofUGCRecognizedbyBa
rCouncilof India,ISO9001:2015CertifiedInstitutionSector22,
Rohini,Delhi-110086, India;www.maims.ac.in
TheSQLGROUP BYClause
TheSQLGROUPBYclauseisused
inconjunctionwiththeSELECTstatementtoarrangeidenticaldataintogroups.
Syntax
SELECTAGE,COUNT(Name)FROMCUSTOMERSGROUPBYAGE;SELECT
ADDRESS,AGE, SUM(SALARY) AS
TOTAL_SALARYFROMCUSTOMERSGROUPBYADDRESS,AGE;
GROUPBYwithORDERBYClause
WecanusetheORDERBYclausewith GROUPBYinSQLtosortthegrouped databyoneormorecolumns.
Syntax
14
MaharajaAgrasenInstituteofManagementStudies
AffiliatedtoGGSIPUniversity,Recognizedu/s2(f)ofUGCRecognizedbyBa
rCouncilof India,ISO9001:2015CertifiedInstitutionSector22,
Rohini,Delhi-110086, India;www.maims.ac.in
Example
SELECTADDRESS,AGE,MIN(SALARY)ASMIN_SUMFROMCUSTOMER
SGROUPBYADDRESS,AGEHAVINGAGE>24;
15
MaharajaAgrasenInstituteofManagementStudies
AffiliatedtoGGSIPUniversity,Recognizedu/s2(f)ofUGCRecognizedbyBa
rCouncilof India,ISO9001:2015CertifiedInstitutionSector22,
Rohini,Delhi-110086, India;www.maims.ac.in
Assignment1onCreateCommand
Q1.CREATETABLE–DEPT (dno,dname&Location).
16