0% found this document useful (0 votes)
19 views7 pages

Microsoft SQL Server

Uploaded by

Anand sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
19 views7 pages

Microsoft SQL Server

Uploaded by

Anand sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

Microsoft SQL Server

-BY Shivam Kumar

MCP – Microsoft Certified Professional

MCDBA- Microsoft Certified Database Administrator

MCAD- Microsoft Certified Application Developer

MCTS- Microsoft Certified Technology Specialist

MCSA- Microsoft Certified Solution Associate

MCSE- Microsoft Certified Solution Expert

21 Years Industry experience, worked in Wipro Infotech, HCL Technologies. 9 years in MPSeDC.

What is SQL Server?

SQL Server is a relational database management system (RDBMS) developed by Microsoft.

How do we connect and communicate to SQL Server?

SSMS- SQL Server Management Studio tool used to connect to SQL Server.

T-SQL is language used to communicate to the SQL Server

Before we go ahead:

Version list of SQL Server:

https://sqlserverbuilds.blogspot.com/

Editions of SQL Server:

Free editions- Developer edition and Express edition (support only 10 GB database size)

Commercial editions-

Enterprise- high end, larger, and more critical businesses

Standard- lower than enterprise edition, few features not supported

Web- suitable for web hosters

Installation of SQL Server- we need to have SQL Server Installed.


URL to download SQL Server 2019 Express.

https://www.microsoft.com/en-us/download/details.aspx?id=101064

URL to download SQL Server management studio (SSMS) 17.9.1

https://learn.microsoft.com/en-us/sql/ssms/release-notes-ssms?view=sql-server-ver16#1791

URL to download sample database for practice purpose

https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-
ver16&tabs=ssms#download-backup-files

MPSeDC environments

 Development
 Testing
 Staging
 Production

 Using SSMS to connect to SQL Server


 SSMS components
 Authentication modes
o Windows authentication
o SQL Server authentication
 Create a login
 Create database
o Types of databases
 System Databases
 Master
 Model
 Tempdb
 msdb
 User-defined databases
o Create new database
o Restore to a new database
o Database files (.mdf and .ldf)
 Detach and Attach database
 Backup database – why backup required?
 Restoring database
 SQL Server data types
o Exact numerics – bit, tinyint, smallint, int, bigint, decimal, numeric, money, smallmoney
o Approximate numerics- Real, Float
o Date & Time- date, smalldatetime, datetime, datetime2, datetimeoffset, time
o Character strings- char, varchar, text
o Unicode Character strings- nchar, nvarchar, ntext

 Create tables
 Database Schema
o A schema is a logical collection of database objects such as tables, views, stored
procedures, indexes, triggers, functions.
o Create Schema hrdbo
o Alter Schema <new schema> transfer dbo.Employees
o Create new table in a schema

CREATE TABLE hrdbo.Consultant


(
ConsultantID int,
FirstName nvarchar(50) NOT NULL,
LastName nvarchar(50) NOT NULL
);
 Table Relations
o One-to-One

SELECT * FROM Employee


SELECT * FROM EmployeeDetails

o One-to-many

SELECT * FROM Employee


SELECT * FROM Address

o Many-to-many
SELECT * FROM Employee
SELECT * FROM EmployeeSkill
SELECT * FROM SkillDescription

 Primary Keys

In SQL Server, a Primary key is a constraint that uniquely identify each row in the table.
A table can have only one primary key.

Create Primary Keys


Create Primary key on a new table on a single column (using T-SQL)
CREATE TABLE Employee(
EmployeeID int IDENTITY(1,1) NOT NULL,
FirstName nvarchar(50) NOT NULL,
LastName nvarchar(50) NOT NULL,
EMail nvarchar(50) NULL,
Phone nvarchar(20) NULL,
HireDate date NULL,
ManagerID int NULL,
Salary float NULL,
DepartmentID smallint NULL,
CONSTRAINT PK_Employee_EmployeeID PRIMARY KEY (EmployeeID))

Create Primary key on a new table on a multiple columns (using T-SQL)


CREATE TABLE Employee(
EmployeeID int IDENTITY(1,1) NOT NULL,
FirstName nvarchar(50) NOT NULL,
LastName nvarchar(50) NOT NULL,
EMail nvarchar(50) NULL,
Phone nvarchar(20) NULL,
HireDate date NULL,
ManagerID int NOT NULL,
Salary float NULL,
DepartmentID smallint NULL,
CONSTRAINT PK_Employee_EmployeeID PRIMARY KEY (EmployeeID, ManagerID))

Create Primary key on a existing table (using T-SQL)


ALTER TABLE Employee
ADD CONSTRAINT PK_Employee_EmployeeID PRIMARY KEY (EmployeeID)

Creating Primary keys using SSMS

 Foreign Keys

The foreign key establishes the relationship between the two tables
For example, the following Employee table has a foreign key column DepartmentID that links to a
primary key column of the Department table.

Creating Foreign Keys


Creating Foreign key on a new table
CREATE TABLE Employee(
EmployeeID int IDENTITY (1,1) NOT NULL,
FirstName nvarchar (50) NOT NULL,
LastName nvarchar (50) NOT NULL,
DepartmentID int NULL,
CONSTRAINT PK_EmployeeID PRIMARY KEY (EmployeeID),
CONSTRAINT FK_Employee_Department FOREIGN KEY (DepartmentID)
REFERENCES Department (DepartmentID)
ON DELETE CASCADE)

ON DELETE CASCADE: When we create a foreign key using the delete cascade option, it
deletes the referencing columns in the child table whenever the referenced row in the
parent table with the primary key is deleted.

Create a Foreign key in an Existing Table


ALTER TABLE Employee
ADD CONSTRAINT FK_Employee_Department FOREIGN KEY (DepartmentID)
REFERENCES Department (DepartmentID)
ON DELETE CASCADE

Creating Foreign keys using SSMS

 SQL Server Views

In SQL Server, a view is a virtual table. In another word, a view is a name given to a query that
can be used as a table.

 SQL Server Functions

Functions in SQL Server contains SQL statements that perform some specific tasks. Functions can
have input parameters and must return a single value or multiple records.

Types of Functions

System Functions- built-in functions

User Defined Function-

Scalar Function- returns a single data value.


select [dbo].[ufnLeadingZeros](9)

select Month(getdate())

Table-valued Function- returns a result set as a table


select * from [dbo].[ufnGetContactInformation](1)

 SQL Server Stored Procedure

A stored procedure is a set of T-SQL statements which is compiled and stored in the database.
The stored procedure accepts input and output parameters, executes the SQL statements, and
returns a result set if any.
exec [dbo].[uspGetBillOfMaterials] 749, '2010-03-18'

o Creating Stored Procedure


o Modifying Stored Procedure

 Triggers in SQL Server

The trigger is a database object similar to a stored procedure that is executed automatically
when an event occurs in a database

DML triggers are automatically fired when an INSERT, UPDATE or DELETE event occurs on a table.

trigger_name table_schema table_name

iuPerson Person Person


 Insert, Update, Delete, Select in SQL Server
 JOIN

Shivam.kumar@mp.gov.in

9826134081

You might also like