WhatisSQL?
SQLstandsforStructuredQueryLanguagewhichisusedtooperatedatabases.SQLisalanguage
whichisusedforstoring,retrievingandmanipulatingdatastoredinrelationaldatabases.ItisANSI
AmericanNationalStandardsInstitutestandard.Thebasicconstructwhichhelpsinfetchingor
manipulatingrecordsstoredinRDBMSRelationalDatabaseManagementSystemiscalledSQL
Query.CommonlyalsoknownasSQLcommandsorSQLQueryhelpsincommunicatingwiththe
databasetoperformcertaintasksandfunctionswithdata.SQLiscaseinsensitive.
TheSQLrelatedDataManipulationLanguagecommandsare:
SELECT
INSERT
UPDATE
DELETE
WhydoweneedSQL?
SQListhemostpopularRDBMSrelatedlanguageandcomesinwithseveraladvantagessuchas:
HasflexibleinbuiltconstructsmakingiteasytoaccessdatafromRDBMS
UsercandescribethedataaswellasthedatatypeforthedatatobestoredintoRDBMS
UsercanretrieveandmanipulatethedatastoredinRDBMS
Userscandroporcreatetables,schemas,databases
Hasfeaturesenablingeasycreationoffunction,views,storedprocedures,packages
CanbeeasilyembeddedwithinotherlanguagesusingSQLlibrariessuchasJDBCusedfor
Java
Providesvariouslevelsofsecurityfeaturesbyrestrictingorgrantingprivilegestoonly
authorizedusers.ThisiscommonlyreferredasDCLDataControlLanguagehaving
commandssuchasREVOKEandGRANT
HasDDLDataDefinitionLanguageoptionssuchasCREATE,ALTER,DROP,TRUNCATE
UnderstandingtheBasicsofSQLStructuring:
ThemostbasicstructuringinSQLiscalledTable.TablesinRDBMShelptostoredata.Tables
containoneormorecolumns.Eachcolumnwillhaveadatatype.Itcancontainzeroormorerows.
1. CreatetableCommand:
AnexampleofCreateTable
CREATETABLESTUDENTS(
Student_Idint,
FirstNamevarchar(200),
LastNamevarchar(200)
);
HeretableNameisSTUDENTSwithcolumnsasStudent_Id,FirstNameandLastName.Data
Typesofthesecolumnsaregivenalongwiththecolumnnamesasintandvarchar.SQLalso
hasseveralotherdatatypessuchasdate,timestamp,blob,bigint,clobetc.
2. InsertTableCommand:
AnexampleofInsertTable
INSERTINTOSTUDENTS(Student_Id,FirstName,LastName)VALUES(1,Anne,Berry);
INSERTINTOSTUDENTS(Student_Id,FirstName,LastName)VALUES(2,Rob,Nicolas);
COMMIT;
IntheaboveinsertquerieswehaveinsertedtworowsintheStudentstable.
PleasenoteattheendyourequireaCOMMITstatementtoallowchangestobereflected
intoyourRDBMS.COMMITisrequiredforDMLstatementssuchasInsert,update,delete.
Incaseyouhaveruntheinsertcommandbutdonotwishtocommitthechanges,thenyou
canuseROLLBACK.ROLLBACKallowschangestobenotreflectedinthedatabaseprovided
itstriggeredpriortoanyCOMMIT.
3. SelectTableCommand:
AnexampleofSelectTable
SELECT*FROMSTUDENTS;
SELECT*FROMSTUDENTSWHEREStudent_Id=1;
Thefirstcommandwillselectandretrievealltherowsfromthetable.Inthiscase2rows
whichwehaveinserted.
ThesecondcommandhasaWHEREclausewhichisusedasafilteringcondition.Wecanuse
WHEREclausetofilterdatafromthetablebasedoncertainconditionsuchas
Student_Id=1
4. UpdateTableCommand:
AnexampleofUpdateTable
UPDATESTUDENTSSETFIRSTNAME=AnnieWHEREStudent_Id=1;
COMMIT;
HerewehavechangedtheFirstNamefromAnnetoAnnieforrecordhavingStudent_Id=1.
AgainCOMMITisusedtoreflectthechangesintodatabase.
5. DeleteTableCommand:
AnexampleofDeleteTable
DELETESTUDENTSWHEREStudent_Id=1;
COMMIT;
HerewearedeletingtherowwithStudent_Id=1.Sothetablewillcontainonly1rownow
withStudent_Id=2.
6. AlterTableCommand:
AnexampleofAlterTable
ALTERTABLESTUDENTSADDCITYVARCHAR(200);
HereweareaddinganadditionalcolumnCITYtoSTUDENTStable.Youcanalsoaddmultiple
columns,modifycolumn,dropcolumn,renamecolumnname,renametablenameusing
altercommands.
7. TruncateTableCommand:
AnexampleofTruncateTable
TRUNCATETABLESTUDENTS;
Thiswillemptyalltherowsbutnotdeletethetable.
8. DropTableCommand:
AnexampleofDropTable
DROPTABLESTUDENTS;
ThiscommandwillremovethetableSTUDENTSfromthedatabase.
CONCLUSION:
AboveexplainedisanIntroductiontoSQL.Furthertothis,thereareseveralflexibleconstructsin
SQLsuchasdifferenttypesoftablejoins,sequences,views,procedures,functionsandpackages.
Examples: https://www.tsql.info/