0% found this document useful (0 votes)
33 views

SQL SERVER BASIC 1 Training

The document discusses SQL Server including an introduction to SQL Server, creating and managing SQL Server databases and tables, SQL data types, and basic data manipulation operations like insert, update, and delete. It provides details on SQL Server editions and instances and how to perform tasks through SQL queries and the SQL Server Management Studio user interface.

Uploaded by

Vamsi Chowdary
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

SQL SERVER BASIC 1 Training

The document discusses SQL Server including an introduction to SQL Server, creating and managing SQL Server databases and tables, SQL data types, and basic data manipulation operations like insert, update, and delete. It provides details on SQL Server editions and instances and how to perform tasks through SQL queries and the SQL Server Management Studio user interface.

Uploaded by

Vamsi Chowdary
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

SQL SERVER DBA

BY CHRIST KANKEU
Overview
a. Introduction
• What is SQL Server
• History SQL Server
• SQL Server Edition
• Importance of SQL Server Instance
• Summary
b. SQL Server Database : Create, Alter, Drop, Backup /
Restore
c. SQL Server Data Types
SQL Server
d. SQL Server Table: CREATE, ALTER, DROP
e. SQL Server DML (Insert, Update, Delete)
f. SQL Server Keys, Constraints and Indexes
a. Introduction
• What is SQL Server ? • Version History SQL Server
SQL SERVER is a relational database management Microsoft and Sybase released version 1.0 in 1989.
system (RDBMS) developed by Microsoft. It is primarily
designed and developed to compete with MySQL and However, the partnership between these two
Oracle database. ended in the early 1990s.
SQL Server supports ANSI SQL, which is the standard Microsoft maintained ownership rights to the
SQL (Structured Query Language) language. However, name SQL Server.
SQL Server comes with its own implementation of the
SQL language, T-SQL (Transact-SQL). Since the 1990s, subsequent versions of SQL
T-SQL is a Microsoft propriety Language known
Server have been released including SQL Server
as Transact-SQL. It provides further capabilities of 2000, 2005, 2008, 2012, 2014, 2016, 2017, and
declaring variable, exception handling, stored 2019
procedure, etc.
a. Introduction - Conts
• SQL Server Editions • Importance of SQL Server
SQL Server Enterprise: It is used in the high end, large Instances
scale and mission Critical business. It provides High-end
security, Advanced Analytics, Machine Learning, etc. The following are the advantages of SQL Server
SQL Server Standard: It is suitable for Mid-Tier instances:
Application and Data marts. It includes basic reporting and 1. For installation of different versions on one
analytics. machine
SQL Server WEB: It is designed for a low total-cost-of- 2. For cost reduction
ownership option for Web hosters. It provides scalability,
3. For maintenance of development, production and
affordability, and manageability capabilities for small to
test environments separately
large scale Web properties.
4. For reducing temporary database problems
SQL Server Developer: It is similar to an enterprise
edition for the non-production environment. It is mainly 5. For separating security privileges
used for build, test, and demo. 6. For maintaining a standby server
SQL Server Express: It is for small scale applications and
free to use.
a. Introduction - Conts
• Summary
• SQL Server is defined as a relational database management system (RDBMS) developed by
Microsoft

• T-SQL means Transact-SQL, a propriety Language by Microsoft

• Microsoft and Sybase released version 1.0 in 1989

• Various Editions of SQL Server are Enterprise, Standard, Web, Developer, and Express

• You can run multiple instances of SQL Server the same on the same machine.
b. SQL Server Database
a. Connect to SQL Server Next, from the Connect menu under
First, launch the Microsoft SQL Server the Object Explorer, choose the Database
Management Studio from the Start menu Engine…

[1]

[2]
b. SQL Server Database - Conts
If the connection is established successfully,
then you will see the following Object
Explorer panel:

[3]

[4]
b. SQL Server Database - Conts
1. SQL Server Management Studio
b. Create Database
There are 2 ways to create Database in SQL
server.
1. SQL Server Management Studio
2. Transact-SQL
[1.1]
b. SQL Server Database - Conts

[1.2]
b. SQL Server Database - Conts

[1.3]

[Optional – For Advance Setting] [1.4]


b. SQL Server Database - Conts
2. Transact-SQL

Syntax:
CREATE DATABASE <Database_name>

Query:
CREATE DATABASE [2.1]
[Edu_TSQL_file]

[2.3]
[2.2]
b. SQL Server Database - Conts
1. SQL Server Management Studio
c. Alter Database
There are 2 ways to alter Database in SQL
server.
1.SQL Server Management Studio
2.Transact-SQL
b. SQL Server Database - Conts
2. Transact-SQL
Syntax:
ALTER DATABASE <Databse_name> MODIFY
NAME = <New Name>

Query:
ALTER DATABASE <Databse_name> MODIFY NAME =
<New Name>
b. SQL Server Database - Conts
1.SQL Server Management Studio
d. Delete Database

There are 2 ways to Delete Database in SQL server.


1.SQL Server Management Studio
2.Transact-SQL.
b. SQL Server Database - Conts
2. Transact-SQL.

Syntax:
DROP DATABASE <Databse_name>

Query:
USE master; GO DROP DATABASE
Edu_TSQL_Alter; GO
b. SQL Server Database - Conts
e. Backup /
Restore Database
c. SQL
Server
Data
Type
Example :
DECLARE @Datatype_Char VARCHAR(30) = 'This is
Character Datatype' PRINT @Datatype_Char

DECLARE @Datatype_Float FLOAT(24) = 22.1234


PRINT @Datatype_Float
DECLARE @Datatype_Date DATE = '2030-01-01' PRINT @Datatype_Date DECLARE @Datatype_Decimal DECIMAL (3,2) = 2.31
DECLARE @Datatype_Binary BINARY(2) = 12; PRINT PRINT @Datatype_Decimal - (P,S) : P : precision , S :
@Datatype_Binary scale
d. SQL Server Table (Create, Alter,
Drop)
a. Create Table
We can Create a table in the following ways: 1. T-SQL: Create a New Table by defining all
1.T-SQL: Create a New Table by defining all Syntax:
columns and its data type. CREATE TABLE tableName ( column_1 datatype [ NULL | NOT NULL ], column_2
datatype [ NULL | NOT NULL ], ... );
2.T-SQL: Create New Table using an existing
Query:
table
CREATE TABLE COURSE ( Course_ID Int, Course_Name Varchar(10) )
3.Using Table Designer
Interesting Facts!
•We can also store big files like .xml in a column as BLOB, CLOB
datatype.
•Delete can roll back, but Drop cannot be rollback.
d. SQL Server Table - Conts
2. T-SQL: Create New Table using an 3. Using Table Designer
existing

Syntax:
SELECT (Column 1, …) INTO <New Table name>
FROM <Old Table name>;

Query:
SELECT COURSE_NAME INTO COURSE_NAMES
FROM COURSE;
d. SQL Server Table - Conts
b. Alter Table 2. Using Table designer
There are two ways to Alter Table in SQL server.
1. T-SQL: Alter Table by adding new columns.
2. Using Table designer

Below is the syntax to Alter table

1. T-SQL: Alter Table by adding new columns.


Syntax:
Alter TABLE <Table name> ADD Column1 datatype, Column2
datatype;

Example:
ALTER TABLE dbo.Course_Title ADD Course_Duration
VARCHAR(20);
d. SQL Server Table - Conts
1. Using SQL Server Management Studio.
c. Drop Table

We delete the table when it is not required anymore.


There are two ways to Drop Table in SQL server.
1. Using SQL Server Management Studio.
2. T-SQL: Delete Table.

2. T-SQL: Delete Table.


Syntax: Query:
DROP TABLE DROP TABLE
<tableName>; COURSE_NAMES;
e. SQL Server DML (Insert,
Update, Delete)
DML (Data Manipulation Language) : a. Insert
a. Insert Syntax
b. Update INSERT INTO tableName (column_1, column_2, ... ) VALUES
(expression_1, expression_2, ... ), (expression_1, expression_2, ... ), ...;
c. Delete
Insert command.
Insert into COURSE values (1,'SQL’);
Additional Information :
You can turn auto commit ON by setting implicit_transactions OFF b. Update
(Tools>Options>Query Execution>SQL Server>ANSI) : Syntax
UPDATE table_name SET c1 = v1, c2 = v2, ... cn = vn [WHERE condition]
SET IMPLICIT_TRANSACTIONS OFF
Update command.
Commit : Rollback : UPDATE sales.taxes SET updated_at = GETDATE();

SET IMPLICIT_TRANSACTIONS ON SET IMPLICIT_TRANSACTIONS ON c. Delete


--Syntax DML --Syntax DML
COMMIT TRANSACTION ROLLBACK TRANSACTION Syntax Update command.
DELETE FROM DELETE FROM
target_table; COURSE;
f. SQL Server : Keys, Constraint,
Indexes
a. Keys a. Keys Interesting Facts!
• Primary Key •The Primary key can be a
• Primary Key combination of multiple
• Foreign Key
T-SQL: Create a Primary key while creating a New Table. columns. This combination is
b. Constraint known as the Composite
• Unique Syntax: primary key.
Constraint CREATE TABLE <Table_Name> ( Column1 datatype, Column2 •The Primary key can have a
• Check datatype,CONSTRAINT <Name> PRIMARY KEY (Column name) . ); maximum of 16 columns.

Constraint Example:

c. Indexes CREATE TABLE COURSE_TSQL_PK (Course_ID Int not Null, Course_name


Varchar(20) CONSTRAINT PK PRIMARY KEY (Course_ID) )

T-SQL: Add a Primary key to existing table using Alter Table


Syntax:
ALTER TABLE tableName ADD CONSTRAINT constraintName PRIMARY KEY (column_1,
column_2, ... column_n);

Example :

ALTER TABLE students ADD CONSTRAINT students_pk PRIMARY KEY (admission);


f. SQL Server : Keys, Constraint,
Indexes - Conts
• Foreign Key
How to Create FOREIGN KEY in SQL
We can Create a Foreign Key in SQL server in 2 ways:
1.SQL Server Management Studio
2.T-SQL
Parent Table: Say, we have an existing Parent table as 'Course.' Course_ID
and Course_name are two columns with Course_Id as Primary Key.

Child Table: We need to create the second table as a child table.


'Course_ID' and 'Course_Strength' as two columns. However,
'Course_ID' shall be Foreign Key.

[2]
SQL Server Management Studio

[1] [3] [4]


f. SQL Server : Keys, Constraint,
Indexes - Conts
• Foreign Key [6]

[5]

[7] [8]

[9]
f. SQL Server : Keys, Constraint,
Indexes - Conts
• Foreign Key
Syntax:

CREATE TABLE childTable ( column_1 datatype [ NULL |


NOT NULL ], column_2 datatype [ NULL |NOT NULL ], ...
CONSTRAINT fkey_name FOREIGN KEY (child_column1,
child_column2, ... child_column_n) REFERENCES
parentTable (parent_column1, parent_column2, ...
parent_column_n) [ ON DELETE { NO ACTION |CASCADE |
SET NULL |SET DEFAULT } ] [ ON UPDATE { NO ACTION
|CASCADE |SET NULL |SET DEFAULT } ] );

Query:
CREATE TABLE Course_Strength_TSQL ( Course_ID Int,
Course_Strength Varchar(20) CONSTRAINT FK FOREIGN
KEY (Course_ID) REFERENCES COURSE (Course_ID) )

Using ALTER TABLE


Syntax: Query:
ALTER TABLE childTable ADD CONSTRAINT fkey_name FOREIGN
KEY (child_column1, child_column2, ... child_column_n) ALTER TABLE department ADD CONSTRAINT fkey_student_admission
REFERENCES parentTable (parent_column1, parent_column2, ... FOREIGN KEY (admission) REFERENCES students (admission);
parent_column_n);
f. SQL Server : Keys, Constraint,
Indexes - Conts
b. Constraint
• Unique Constraint • Check Constraint
Create unique Contraint - Using a CREATE TABLE statement Using a CREATE TABLE statement
CREATE TABLE table_name ( column1 datatype [ NULL | NOT
NULL ], column2 datatype [ NULL | NOT NULL ], ... CREATE TABLE table_name ( column1 datatype [ NULL | NOT NULL ],
CONSTRAINT constraint_name UNIQUE (uc_col1, uc_col2, ... column2 datatype [ NULL | NOT NULL ], ... CONSTRAINT constraint_name
uc_col_n) ); CHECK [ NOT FOR REPLICATION ] (column_name condition) );
CREATE TABLE employees ( employee_id INT PRIMARY KEY, CREATE TABLE employees ( employee_id INT NOT NULL, last_name
employee_number INT NOT NULL, last_name VARCHAR(50) VARCHAR(50) NOT NULL, first_name VARCHAR(50), salary MONEY,
NOT NULL, first_name VARCHAR(50), salary MONEY, CONSTRAINT check_employee_id CHECK (employee_id BETWEEN 1 and
CONSTRAINT employees_unique UNIQUE 10000) );
(employee_number) );
Using an ALTER TABLE statement
Create unique contraint - Using an ALTER TABLE statement
ALTER TABLE table_name ADD CONSTRAINT constraint_name CHECK
ALTER TABLE table_name ADD CONSTRAINT constraint_name
(column_name condition);
UNIQUE (column1, column2, ... column_n);

ALTER TABLE employees ADD CONSTRAINT ALTER TABLE employees ADD CONSTRAINT check_last_name CHECK
employees_unique UNIQUE (employee_number); (last_name IN ('Smith', 'Anderson', 'Jones'));

Drop Unique Constraint Drop a Check Constraint


ALTER TABLE table_name DROP CONSTRAINT ALTER TABLE table_name DROP CONSTRAINT constraint_name;
constraint_name;
f. SQL Server : Keys, Constraint,
Indexes - Conts
c. Indexes
CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX
CREATE INDEX contacts_idx ON contacts (last_name DESC, first_name DESC);
index_name ON table_name ( column1 [ASC | DESC ], ... column_n [ ASC |
DESC ] ) [ INCLUDE ( column1, ... column_n ) ] [ WHERE condition ]
[ WITH ( PAD_INDEX = { ON | OFF } | FILLFACTOR = fillfactor | Unique Constraint
SORT_IN_TEMPDB = { ON | OFF } | IGNORE_DUP_KEY = { ON | OFF } CREATE UNIQUE INDEX contacts_uidx ON contacts (last_name, first_name);
| STATISTICS_NORECOMPUTE = { ON | OFF } |
STATISTICS_INCREMENTAL = { ON | OFF } | DROP_EXISTING = { ON
| OFF } | ONLINE = { ON | OFF } | ALLOW_ROW_LOCKS = { ON | OFF } Rename an Index
| ALLOW_PAGE_LOCKS = { ON | OFF } | MAXDOP = max_degree | sp_rename 'table_name.old_index_name', 'new_index_name', 'INDEX';
DATA_COMPRESSION = { NONE | PAGE | ROW } [ ON PARTITIONS
( { number | range } ] [ ON partition_scheme ( column ) | ON filegroup | ON
Drop an Index
default_filegroup ] [ FILESTREAM_ON { filegroup | partition_scheme };
DROP INDEX table_name.index_name;
CREATE INDEX contacts_idx ON contacts (last_name);

CREATE INDEX contacts_idx ON contacts (last_name, first_name);


Main Responsibilities
 Installing and upgrading the database server tools
 Planning future storage requirements for the database systems
 Modifying the database structure based on information given by application developers
 Enrolling users and maintaining system security
 Ensuring compliance with database vendor license agreement
 Controlling and monitoring user access to the databases
 Monitoring and optimizing the performance of the databases
 Generating various reports by querying from database as per need

You might also like