SQL Server 2016 - The Technical Guy...SQL Server SQL Server Database Engine Management Studio Data...
Transcript of SQL Server 2016 - The Technical Guy...SQL Server SQL Server Database Engine Management Studio Data...
DatabaseSystems• ADatabaseisastructuredwaytostorelotsofinformation.
Theinformationisstoredindifferenttables.• - “Everything”todayisstoredindatabases!
Examples:• Bank/Accountsystems• InformationinWebpagessuchasFacebook,Wikipedia,YouTube,etc.
• …lotsofotherexamples!(Giveme5examples)
DatabaseManagementSystems(DBMS)
Herearesomeexamples:• Oracle• MySQL• MariaDB• Sybase• MicrosoftAccess• MicrosoftSQLServer• ... (wehavehundredsdifferentDBMS)
SQLServerSQL
Server
DatabaseEngine
ManagementStudio
DataStorage AUserInterfaceforinteractingwiththeDataintheDatabase
TheDatabaseEngineisthecoreserviceforstoring,processing,andsecuringdata.
TheDatabaseEngineandtheManagementStudiocanbeonthesamecomputerorondifferentcomputers.FromtheManagementStudioyoucanconnecttoalocalDatabaseEngineoraDatabaseEnginelocatedinanetwork.
MicrosoftSQLServerSQLServerconsistsofaDatabaseEngineandaManagementStudio.TheDatabaseEnginehasnographicalinterface-itisjustaservicerunninginthebackgroundofyourcomputer(preferableontheserver).TheManagementStudioisgraphicaltoolforconfiguringandviewingtheinformationinthedatabase.Itcanbeinstalledontheserverorontheclient(orboth).
SoftwareSoftware
SQLServer2016ExpressEdition
Youneedtoinstallthefollowingcomponents:• Step1:DatabaseEngine• Step2:ManagementStudio
DatabaseEngine
Adatabaseengine(orstorageengine)istheunderlyingsoftwarecomponentthatadatabasemanagementsystem(DBMS)usestocreate,read,updateanddelete(CRUD)datafromadatabase.TheDatabaseEngineisthecoreserviceforstoring,processing,andsecuringdata.
SQLServerManagementStudio(SSMS)
WriteyourQueryhere
TheResultsfromyourQuery
YourDatabase
YourTables
YourSQLServer
1
2
3
4
5
WheredoyoufindtheInstallationFiles?SQLServer2016ExpressEditionhttps://www.microsoft.com/en-us/download/details.aspx?id=52679
Instance Configuration
Don'tchangetheDefaultInstanceID,whichis“MSSQLSERVER”
Normallyyouselect“Defaultinstance”
Install SQLServerManagementStudio
Alsoavailablefrom:https://msdn.microsoft.com/en-us/library/mt238290.aspx
SQLServerManagementStudio(SSMS)
WriteyourQueryhere
TheResultsfromyourQuery
YourDatabase
YourTables
YourSQLServer
1
2
3
4
5
CreateTableswiththeManagementStudioEvenifyoucando“everything”usingtheSQLlanguage,itissometimeseasiertodosomethinginthedesignertoolsintheManagementStudioinSQLServer.Insteadofcreatingascriptyoumayaswelleasilyusethedesignerforcreatingtables,constraints,insertingdata,etc.
Select“NewTable…”: Next,thetabledesignerpopsupwhereyoucanaddcolumns,datatypes,etc.
1 2
Inthisdesignerwemayalsospecifyconstraints,suchasprimarykeys,unique,foreignkeys,etc.
StructuredQueryLanguage
Tutorial:StructuredQueryLanguage(SQL):http://home.hit.no/~hansha/?tutorial=sql
SQL(StructuredQueryLanguage)isadatabasecomputerlanguagedesignedformanagingdatainrelationaldatabasemanagementsystems(RDBMS).
• insert into STUDENT (Name , Number, SchoolId)values ('John Smith', '100005', 1)
• select SchoolId, Name from SCHOOL
• select * from SCHOOL where SchoolId > 100
• update STUDENT set Name='John Wayne' where StudentId=2
• delete from STUDENT where SchoolId=3
Wehave4differentQueryTypes:INSERT,SELECT,UPDATEand DELETE
InsertDataintotheTablesusingSQL
insert into STUDENT (Name , Number, SchoolId)values ('John Smith', '100005', 1)
...
Example:
CreateViewsusingManagementStudio
1
2
3
4AddnecessarytablesSavetheView
GraphicalInterfacewhereyoucanselectcolumnsyouneed
IF EXISTS (SELECT nameFROM sysobjectsWHERE name = 'CourseData' AND type = 'V')
DROP VIEW CourseDataGO
CREATE VIEW CourseDataAS
SELECTSCHOOL.SchoolId, SCHOOL.SchoolName, COURSE.CourseId, COURSE.CourseName,COURSE.Description
FROMSCHOOL INNER JOIN COURSE ON SCHOOL.SchoolId = COURSE.SchoolIdGO
select * from CourseData
YoucanUsetheViewasanordinarytableinQueries:
AViewisa“virtual”tablethatcancontaindatafrommultipletables
InsidetheViewyoujointhedifferenttablestogetherusingtheJOIN operator
TheNameoftheView
CreateView:
UsingtheView:
Thispartisnotnecessary– butifyoumakeanychanges,youneedtodeletetheoldversionbeforeyoucanupdateit
CreateViewsusingSQL1
2
CreateStoredProceduresIFEXISTS(SELECTname
FROMsysobjectsWHEREname ='StudentGrade'AND type='P')
DROPPROCEDUREStudentGradeOG
CREATEPROCEDUREStudentGrade@Studentvarchar(50),@Coursevarchar(10),@Gradevarchar(1)
AS
DECLARE@StudentId int,@CourseId int
selectStudentIdfromSTUDENTwhereStudentName =@Student
selectCourseId fromCOURSEwhereCourseName =@Course
insertintoGRADE(StudentId,CourseId,Grade)values (@StudentId,@CourseId,@Grade)GO
AStoredProcedureislikeMethodinC#- itisapieceofcodewithSQLcommandsthatdoaspecifictask– andyoureuseit
InputArguments
Internal/LocalVariables
ProcedureName
SQLCode(the“body”oftheStoredProcedure)
Note!Eachvariablestartswith@
CreateStoredProcedure:
Thispartisnotnecessary– butifyoumakeanychanges,youneedtodeletetheoldversionbeforeyoucanupdateit
execute StudentGrade 'John Wayne', 'SCE2006', 'B'
UsingtheStoredProcedure:
StartusingSQLServer
1. Tutorial:SQLServerManagementStudiohttps://msdn.microsoft.com/en-us/library/bb934498.aspx
2. Tutorial:GettingStartedwiththeDatabaseEnginehttps://msdn.microsoft.com/en-us/library/ms345318.aspx
3. Tutorial:WritingTransact-SQLStatementshttps://msdn.microsoft.com/en-us/library/ms365303.aspx
Videos• IntroductiontoDatabaseSystemshttps://www.youtube.com/watch?v=n75iPNrzN-o
• IntroductiontoERwinhttps://www.youtube.com/watch?v=P3n6hRNup8Y
• IntroductiontoSQLServerhttps://www.youtube.com/watch?v=SlR4KOhAG1U
• DatabaseViewsandStoredProcedureshttps://www.youtube.com/watch?v=SHELF_iQUeU
• DatabaseDevelopmenthttps://www.youtube.com/watch?v=Pu9TgTZ9Y5c
https://www.youtube.com/playlist?list=PLdb-TcK6Aqj0PedGwO7CUI6WBRyia7EQh
Hans-PetterHalvorsen,M.Sc.
UniversityCollegeofSoutheastNorwaywww.usn.no
E-mail:[email protected]:http://home.hit.no/~hansha/