SlideShare a Scribd company logo
SQL SERVER – BASICS
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
d. SQL Server Table: CREATE, ALTER, DROP
e. SQL Server DML (Insert, Update, Delete)
f. SQL Server Keys, Constraints and Indexes
SQL Server
a. Introduction
• What is SQL Server ?
SQL SERVER is a relational database
management system (RDBMS) developed by
Microsoft. It is primarily designed and
developed to compete with MySQL and Oracle
database.
SQL Server supports ANSI SQL, which is the
standard SQL (Structured Query Language)
language. However, SQL Server comes with its
own implementation of the SQL language, T-
SQL (Transact-SQL).
T-SQL is a Microsoft propriety Language
known as Transact-SQL. It provides further
capabilities of declaring variable, exception
handling, stored procedure, etc.
• Version History SQL Server
Microsoft and Sybase released version 1.0 in
1989.
However, the partnership between these two
ended in the early 1990s.
Microsoft maintained ownership rights to the
name SQL Server.
Since the 1990s, subsequent versions of SQL
Server have been released including SQL
Server 2000, 2005, 2008, 2012, 2014, 2016,
2017, and 2019
a. Introduction - Conts
• SQL Server Editions
SQL Server Enterprise: It is used in the high
end, large scale and mission Critical
It provides High-end security, Advanced
Analytics, Machine Learning, etc.
SQL Server Standard: It is suitable for Mid-
Tier Application and Data marts. It includes
basic reporting and analytics.
SQL Server WEB: It is designed for a low total-
cost-of-ownership option for Web hosters. It
provides scalability, affordability, and
manageability capabilities for small to large
scale Web properties.
SQL Server Developer: It is similar to an
enterprise edition for the non-production
environment. It is mainly used for build, test,
and demo.
• Importance of SQL Server
Instances
The following are the advantages of SQL
Server instances:
1. For installation of different versions on
one machine
2. For cost reduction
3. For maintenance of development,
production and test environments separately
4. For reducing temporary database
problems
5. For separating security privileges
6. For maintaining a standby server
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
First, launch the Microsoft SQL Server
Management Studio from the Start menu
Next, from the Connect menu under
the Object Explorer, choose
the Database Engine…
[1]
[2]
a. Connect to SQL Server
b. SQL Server Database - Conts
[3]
If the connection is established
successfully, then you will see the
following Object Explorer panel:
[4]
b. SQL Server Database - Conts
b. Create Database
There are 2 ways to create Database in
SQL server.
1.SQL Server Management Studio
2.Transact-SQL
[1.1]
1.SQL Server Management Studio
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
CREATE DATABASE <Database_name>
Syntax:
Query:
CREATE DATABASE
[Edu_TSQL_file]
[2.1]
[2.2]
[2.3]
b. SQL Server Database - Conts
c. Alter Database
There are 2 ways to alter Database in
SQL server.
1.SQL Server Management Studio
2.Transact-SQL
1.SQL Server Management Studio
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
d. Delete Database
There are 2 ways to Delete Database in SQL
server.
1.SQL Server Management Studio
2.Transact-SQL.
1.SQL Server Management
Studio
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
DECLARE @Datatype_Char VARCHAR(30) = 'This is
Character Datatype' PRINT @Datatype_Char
Example :
DECLARE @Datatype_Float FLOAT(24) = 22.1234
PRINT @Datatype_Float
DECLARE @Datatype_Decimal DECIMAL (3,2) = 2.31
PRINT @Datatype_Decimal - (P,S) : P : precision , S :
scale
DECLARE @Datatype_Date DATE = '2030-01-01' PRINT
@Datatype_Date
DECLARE @Datatype_Binary BINARY(2) = 12; PRINT
@Datatype_Binary
d. SQL Server Table (Create, Alter,
Drop)
We can Create a table in the following
ways:
1.T-SQL: Create a New Table by defining
all columns and its data type.
2.T-SQL: Create New Table using an
existing table
3.Using Table Designer
1. T-SQL: Create a New Table by defining all
Syntax:
CREATE TABLE tableName ( column_1 datatype [ NULL | NOT NULL ],
column_2 datatype [ NULL | NOT NULL ], ... );
Query:
CREATE TABLE COURSE ( Course_ID Int, Course_Name Varchar(10) )
a. Create Table
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 existing
Syntax:
SELECT (Column 1, …) INTO <New Table name>
FROM <Old Table name>;
Query:
SELECT COURSE_NAME INTO
COURSE_NAMES FROM COURSE;
3. Using Table Designer
d. SQL Server Table - Conts
b. Alter Table
Syntax:
Alter TABLE <Table name> ADD Column1 datatype,
Column2 datatype;
Example:
ALTER TABLE dbo.Course_Title ADD Course_Duration
VARCHAR(20);
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.
2. Using Table designer
d. SQL Server Table - Conts
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.
1. Using SQL Server Management Studio.
2. T-SQL: Delete Table.
Syntax:
DROP TABLE
<tableName>;
Query:
DROP TABLE
COURSE_NAMES;
e. SQL Server DML (Insert,
Update, Delete)
DML (Data Manipulation Language) :
a. Insert
b. Update
c. Delete
Additional Information :
You can turn auto commit ON by setting
implicit_transactions OFF (Tools>Options>Query
Execution>SQL Server>ANSI) :
SET IMPLICIT_TRANSACTIONS OFF
Commit :
SET IMPLICIT_TRANSACTIONS ON
--Syntax DML
COMMIT TRANSACTION
Rollback :
SET IMPLICIT_TRANSACTIONS ON
--Syntax DML
ROLLBACK TRANSACTION
a. Insert
Syntax
INSERT INTO tableName (column_1, column_2, ... ) VALUES
(expression_1, expression_2, ... ), (expression_1, expression_2, ... ),
...;
Insert command.
Insert into COURSE values (1,'SQL’);
b. Update
UPDATE table_name SET c1 = v1, c2 = v2, ... cn = vn [WHERE
condition]
UPDATE sales.taxes SET updated_at = GETDATE();
Syntax
Update command.
c. Delete
DELETE FROM
target_table;
DELETE FROM
COURSE;
Syntax Update comman
d.
f. SQL Server : Keys, Constraint,
Indexes
a. Keys
• Primary
Key
• Foreign
Key
b. Constraint
• Unique
Constraint
• Check
Constraint
c. Indexes
a. Keys
• Primary Key
T-SQL: Create a Primary key while creating a New Table.
CREATE TABLE <Table_Name> ( Column1 datatype, Column2
datatype,CONSTRAINT <Name> PRIMARY KEY (Column name) . );
Syntax:
Example:
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);
Interesting Facts!
•The Primary key can be a
combination of multiple
columns. This
combination is known as
the Composite primary
key.
•The Primary key can
have a maximum of 16
columns.
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
SQL Server Management Studio
[1]
[2]
[3] [4]
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.
f. SQL Server : Keys, Constraint,
Indexes - Conts
• Foreign Key
[5]
[6]
[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
ALTER TABLE childTable ADD CONSTRAINT fkey_name
FOREIGN KEY (child_column1, child_column2, ...
child_column_n) REFERENCES parentTable (parent_column1,
parent_column2, ... parent_column_n);
Syntax: Query:
ALTER TABLE department ADD CONSTRAINT
fkey_student_admission FOREIGN KEY (admission) REFERENCES
students (admission);
f. SQL Server : Keys, Constraint,
Indexes - Conts
b. Constraint
Create unique Contraint - Using a CREATE TABLE statement
CREATE TABLE table_name ( column1 datatype [ NULL |
NOT NULL ], column2 datatype [ NULL | NOT NULL ], ...
CONSTRAINT constraint_name UNIQUE (uc_col1, uc_col2,
... uc_col_n) );
CREATE TABLE employees ( employee_id INT PRIMARY
KEY, employee_number INT NOT NULL, last_name
VARCHAR(50) NOT NULL, first_name VARCHAR(50),
salary MONEY, CONSTRAINT employees_unique UNIQUE
(employee_number) );
Create unique contraint - Using an ALTER TABLE statement
ALTER TABLE table_name ADD CONSTRAINT
constraint_name UNIQUE (column1, column2, ... column_n);
ALTER TABLE employees ADD CONSTRAINT
employees_unique UNIQUE (employee_number);
• Unique Constraint
Drop Unique Constraint
ALTER TABLE table_name DROP CONSTRAINT
constraint_name;
• Check Constraint
Using a CREATE TABLE statement
CREATE TABLE table_name ( column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ], ... CONSTRAINT
constraint_name CHECK [ NOT FOR REPLICATION ] (column_name
condition) );
CREATE TABLE employees ( employee_id INT NOT NULL, last_name
VARCHAR(50) NOT NULL, first_name VARCHAR(50), salary MONEY,
CONSTRAINT check_employee_id CHECK (employee_id BETWEEN 1
and 10000) );
Using an ALTER TABLE statement
ALTER TABLE table_name ADD CONSTRAINT constraint_name CHECK
(column_name condition);
ALTER TABLE employees ADD CONSTRAINT check_last_name
CHECK (last_name IN ('Smith', 'Anderson', 'Jones'));
Drop a Check Constraint
ALTER TABLE table_name DROP CONSTRAINT constraint_name;
f. SQL Server : Keys, Constraint,
Indexes - Conts
c. Indexes
CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX
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 | SORT_IN_TEMPDB = { ON | OFF } | IGNORE_DUP_KEY = {
ON | OFF } | STATISTICS_NORECOMPUTE = { ON | OFF } |
STATISTICS_INCREMENTAL = { ON | OFF } | DROP_EXISTING = {
ON | OFF } | ONLINE = { ON | OFF } | ALLOW_ROW_LOCKS = { ON |
OFF } | ALLOW_PAGE_LOCKS = { ON | OFF } | MAXDOP =
max_degree | DATA_COMPRESSION = { NONE | PAGE | ROW } [
ON PARTITIONS ( { number | range } ] [ ON partition_scheme (
column ) | ON filegroup | ON default_filegroup ] [ FILESTREAM_ON {
filegroup | partition_scheme };
CREATE INDEX contacts_idx ON contacts (last_name);
CREATE INDEX contacts_idx ON contacts (last_name, first_name);
CREATE INDEX contacts_idx ON contacts (last_name DESC, first_name
DESC);
CREATE UNIQUE INDEX contacts_uidx ON contacts (last_name,
first_name);
Unique Constraint
Rename an Index
sp_rename 'table_name.old_index_name', 'new_index_name', 'INDEX';
Drop an Index
DROP INDEX table_name.index_name;
Thanks.
REFERENCE :
• H T T P S : / / W W W . T E C H O N T H E N E T . C O M / S Q L _ S E R V E R /
• H T T P S : / / W W W . G U R U 9 9 . C O M / M S - S Q L - S E R V E R - T U T O R I A L . H T M L
• H T T P S : / / W W W . S Q L S E R V E R T U T O R I A L . N E T / ? S = C O N S T R A I N T

More Related Content

PPTX
SQL_SERVER_BASIC_1_Training.pptx
DOCX
SQL Tutorial for BCA-2
PDF
SQL-Server Database.pdf
PPTX
Using Basic Structured Query Language lo1.pptx
PPTX
SQL.pptx for the begineers and good know
PPT
Presentation1
PPTX
MS SQL - Database Programming Concepts by RSolutions
PPTX
SQL UNIT FOUR.pptxDiscDiscoverabilitDiscoverability Scorey Scoreoverability S...
SQL_SERVER_BASIC_1_Training.pptx
SQL Tutorial for BCA-2
SQL-Server Database.pdf
Using Basic Structured Query Language lo1.pptx
SQL.pptx for the begineers and good know
Presentation1
MS SQL - Database Programming Concepts by RSolutions
SQL UNIT FOUR.pptxDiscDiscoverabilitDiscoverability Scorey Scoreoverability S...

Similar to SQL_SERVER_BASIC_1_Training.pptx (20)

PPT
Sql Server 2000
PDF
SQL Server Interview Questions PDF By ScholarHat
PPTX
SQL SERVER Training in Pune Slides
PPTX
ms-sql-server-150223140402-conversion-gate02.pptx
ODP
Ms sql-server
PPT
database.ppt
PPTX
Lec-w9-SQL.pptx Introduction to SQL in basics
PPSX
MS SQL Server
PPTX
Query Analyser , SQL Server Groups, Transact –SQL
PPTX
PPT
chapter 8 SQL.ppt
PDF
14 22 size sql book(1)
PDF
Introduction to SQL..pdf
PPT
Creating a database
PPT
Implementing the Databese Server session 02
PDF
SQL for data scientist And data analysist Advanced
PPTX
20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx
PPT
PO WER - Piotr Mariat - Sql
PPT
Sql presentation 1 by chandan
PPT
Supporting SQLserver
Sql Server 2000
SQL Server Interview Questions PDF By ScholarHat
SQL SERVER Training in Pune Slides
ms-sql-server-150223140402-conversion-gate02.pptx
Ms sql-server
database.ppt
Lec-w9-SQL.pptx Introduction to SQL in basics
MS SQL Server
Query Analyser , SQL Server Groups, Transact –SQL
chapter 8 SQL.ppt
14 22 size sql book(1)
Introduction to SQL..pdf
Creating a database
Implementing the Databese Server session 02
SQL for data scientist And data analysist Advanced
20190326165338_ISYS6508-PPT5-W5-S6-R0.pptx
PO WER - Piotr Mariat - Sql
Sql presentation 1 by chandan
Supporting SQLserver
Ad

Recently uploaded (20)

PPTX
Database Infoormation System (DBIS).pptx
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PPT
ISS -ESG Data flows What is ESG and HowHow
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PPTX
IB Computer Science - Internal Assessment.pptx
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PDF
annual-report-2024-2025 original latest.
PPTX
climate analysis of Dhaka ,Banglades.pptx
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PPT
Quality review (1)_presentation of this 21
PPTX
Introduction to Knowledge Engineering Part 1
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PDF
Business Analytics and business intelligence.pdf
PDF
Mega Projects Data Mega Projects Data
Database Infoormation System (DBIS).pptx
oil_refinery_comprehensive_20250804084928 (1).pptx
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
ISS -ESG Data flows What is ESG and HowHow
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
IB Computer Science - Internal Assessment.pptx
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
Miokarditis (Inflamasi pada Otot Jantung)
annual-report-2024-2025 original latest.
climate analysis of Dhaka ,Banglades.pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Introduction-to-Cloud-ComputingFinal.pptx
Quality review (1)_presentation of this 21
Introduction to Knowledge Engineering Part 1
.pdf is not working space design for the following data for the following dat...
STUDY DESIGN details- Lt Col Maksud (21).pptx
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Business Ppt On Nestle.pptx huunnnhhgfvu
Business Analytics and business intelligence.pdf
Mega Projects Data Mega Projects Data
Ad

SQL_SERVER_BASIC_1_Training.pptx

  • 1. SQL SERVER – BASICS
  • 2. 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 d. SQL Server Table: CREATE, ALTER, DROP e. SQL Server DML (Insert, Update, Delete) f. SQL Server Keys, Constraints and Indexes SQL Server
  • 3. a. Introduction • What is SQL Server ? SQL SERVER is a relational database management system (RDBMS) developed by Microsoft. It is primarily designed and developed to compete with MySQL and Oracle database. SQL Server supports ANSI SQL, which is the standard SQL (Structured Query Language) language. However, SQL Server comes with its own implementation of the SQL language, T- SQL (Transact-SQL). T-SQL is a Microsoft propriety Language known as Transact-SQL. It provides further capabilities of declaring variable, exception handling, stored procedure, etc. • Version History SQL Server Microsoft and Sybase released version 1.0 in 1989. However, the partnership between these two ended in the early 1990s. Microsoft maintained ownership rights to the name SQL Server. Since the 1990s, subsequent versions of SQL Server have been released including SQL Server 2000, 2005, 2008, 2012, 2014, 2016, 2017, and 2019
  • 4. a. Introduction - Conts • SQL Server Editions SQL Server Enterprise: It is used in the high end, large scale and mission Critical It provides High-end security, Advanced Analytics, Machine Learning, etc. SQL Server Standard: It is suitable for Mid- Tier Application and Data marts. It includes basic reporting and analytics. SQL Server WEB: It is designed for a low total- cost-of-ownership option for Web hosters. It provides scalability, affordability, and manageability capabilities for small to large scale Web properties. SQL Server Developer: It is similar to an enterprise edition for the non-production environment. It is mainly used for build, test, and demo. • Importance of SQL Server Instances The following are the advantages of SQL Server instances: 1. For installation of different versions on one machine 2. For cost reduction 3. For maintenance of development, production and test environments separately 4. For reducing temporary database problems 5. For separating security privileges 6. For maintaining a standby server
  • 5. 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.
  • 6. b. SQL Server Database First, launch the Microsoft SQL Server Management Studio from the Start menu Next, from the Connect menu under the Object Explorer, choose the Database Engine… [1] [2] a. Connect to SQL Server
  • 7. b. SQL Server Database - Conts [3] If the connection is established successfully, then you will see the following Object Explorer panel: [4]
  • 8. b. SQL Server Database - Conts b. Create Database There are 2 ways to create Database in SQL server. 1.SQL Server Management Studio 2.Transact-SQL [1.1] 1.SQL Server Management Studio
  • 9. b. SQL Server Database - Conts [1.2]
  • 10. b. SQL Server Database - Conts [1.3] [Optional – For Advance Setting] [1.4]
  • 11. b. SQL Server Database - Conts 2. Transact-SQL CREATE DATABASE <Database_name> Syntax: Query: CREATE DATABASE [Edu_TSQL_file] [2.1] [2.2] [2.3]
  • 12. b. SQL Server Database - Conts c. Alter Database There are 2 ways to alter Database in SQL server. 1.SQL Server Management Studio 2.Transact-SQL 1.SQL Server Management Studio
  • 13. 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>
  • 14. b. SQL Server Database - Conts d. Delete Database There are 2 ways to Delete Database in SQL server. 1.SQL Server Management Studio 2.Transact-SQL. 1.SQL Server Management Studio
  • 15. b. SQL Server Database - Conts 2. Transact-SQL. Syntax: DROP DATABASE <Databse_name> Query: USE master; GO DROP DATABASE Edu_TSQL_Alter; GO
  • 16. b. SQL Server Database - Conts e. Backup / Restore Database
  • 17. c. SQL Server Data Type DECLARE @Datatype_Char VARCHAR(30) = 'This is Character Datatype' PRINT @Datatype_Char Example : DECLARE @Datatype_Float FLOAT(24) = 22.1234 PRINT @Datatype_Float DECLARE @Datatype_Decimal DECIMAL (3,2) = 2.31 PRINT @Datatype_Decimal - (P,S) : P : precision , S : scale DECLARE @Datatype_Date DATE = '2030-01-01' PRINT @Datatype_Date DECLARE @Datatype_Binary BINARY(2) = 12; PRINT @Datatype_Binary
  • 18. d. SQL Server Table (Create, Alter, Drop) We can Create a table in the following ways: 1.T-SQL: Create a New Table by defining all columns and its data type. 2.T-SQL: Create New Table using an existing table 3.Using Table Designer 1. T-SQL: Create a New Table by defining all Syntax: CREATE TABLE tableName ( column_1 datatype [ NULL | NOT NULL ], column_2 datatype [ NULL | NOT NULL ], ... ); Query: CREATE TABLE COURSE ( Course_ID Int, Course_Name Varchar(10) ) a. Create Table 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.
  • 19. d. SQL Server Table - Conts 2. T-SQL: Create New Table using an existing Syntax: SELECT (Column 1, …) INTO <New Table name> FROM <Old Table name>; Query: SELECT COURSE_NAME INTO COURSE_NAMES FROM COURSE; 3. Using Table Designer
  • 20. d. SQL Server Table - Conts b. Alter Table Syntax: Alter TABLE <Table name> ADD Column1 datatype, Column2 datatype; Example: ALTER TABLE dbo.Course_Title ADD Course_Duration VARCHAR(20); 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. 2. Using Table designer
  • 21. d. SQL Server Table - Conts 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. 1. Using SQL Server Management Studio. 2. T-SQL: Delete Table. Syntax: DROP TABLE <tableName>; Query: DROP TABLE COURSE_NAMES;
  • 22. e. SQL Server DML (Insert, Update, Delete) DML (Data Manipulation Language) : a. Insert b. Update c. Delete Additional Information : You can turn auto commit ON by setting implicit_transactions OFF (Tools>Options>Query Execution>SQL Server>ANSI) : SET IMPLICIT_TRANSACTIONS OFF Commit : SET IMPLICIT_TRANSACTIONS ON --Syntax DML COMMIT TRANSACTION Rollback : SET IMPLICIT_TRANSACTIONS ON --Syntax DML ROLLBACK TRANSACTION a. Insert Syntax INSERT INTO tableName (column_1, column_2, ... ) VALUES (expression_1, expression_2, ... ), (expression_1, expression_2, ... ), ...; Insert command. Insert into COURSE values (1,'SQL’); b. Update UPDATE table_name SET c1 = v1, c2 = v2, ... cn = vn [WHERE condition] UPDATE sales.taxes SET updated_at = GETDATE(); Syntax Update command. c. Delete DELETE FROM target_table; DELETE FROM COURSE; Syntax Update comman d.
  • 23. f. SQL Server : Keys, Constraint, Indexes a. Keys • Primary Key • Foreign Key b. Constraint • Unique Constraint • Check Constraint c. Indexes a. Keys • Primary Key T-SQL: Create a Primary key while creating a New Table. CREATE TABLE <Table_Name> ( Column1 datatype, Column2 datatype,CONSTRAINT <Name> PRIMARY KEY (Column name) . ); Syntax: Example: 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); Interesting Facts! •The Primary key can be a combination of multiple columns. This combination is known as the Composite primary key. •The Primary key can have a maximum of 16 columns.
  • 24. 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 SQL Server Management Studio [1] [2] [3] [4] 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.
  • 25. f. SQL Server : Keys, Constraint, Indexes - Conts • Foreign Key [5] [6] [7] [8] [9]
  • 26. 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 ALTER TABLE childTable ADD CONSTRAINT fkey_name FOREIGN KEY (child_column1, child_column2, ... child_column_n) REFERENCES parentTable (parent_column1, parent_column2, ... parent_column_n); Syntax: Query: ALTER TABLE department ADD CONSTRAINT fkey_student_admission FOREIGN KEY (admission) REFERENCES students (admission);
  • 27. f. SQL Server : Keys, Constraint, Indexes - Conts b. Constraint Create unique Contraint - Using a CREATE TABLE statement CREATE TABLE table_name ( column1 datatype [ NULL | NOT NULL ], column2 datatype [ NULL | NOT NULL ], ... CONSTRAINT constraint_name UNIQUE (uc_col1, uc_col2, ... uc_col_n) ); CREATE TABLE employees ( employee_id INT PRIMARY KEY, employee_number INT NOT NULL, last_name VARCHAR(50) NOT NULL, first_name VARCHAR(50), salary MONEY, CONSTRAINT employees_unique UNIQUE (employee_number) ); Create unique contraint - Using an ALTER TABLE statement ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column1, column2, ... column_n); ALTER TABLE employees ADD CONSTRAINT employees_unique UNIQUE (employee_number); • Unique Constraint Drop Unique Constraint ALTER TABLE table_name DROP CONSTRAINT constraint_name; • Check Constraint Using a CREATE TABLE statement CREATE TABLE table_name ( column1 datatype [ NULL | NOT NULL ], column2 datatype [ NULL | NOT NULL ], ... CONSTRAINT constraint_name CHECK [ NOT FOR REPLICATION ] (column_name condition) ); CREATE TABLE employees ( employee_id INT NOT NULL, last_name VARCHAR(50) NOT NULL, first_name VARCHAR(50), salary MONEY, CONSTRAINT check_employee_id CHECK (employee_id BETWEEN 1 and 10000) ); Using an ALTER TABLE statement ALTER TABLE table_name ADD CONSTRAINT constraint_name CHECK (column_name condition); ALTER TABLE employees ADD CONSTRAINT check_last_name CHECK (last_name IN ('Smith', 'Anderson', 'Jones')); Drop a Check Constraint ALTER TABLE table_name DROP CONSTRAINT constraint_name;
  • 28. f. SQL Server : Keys, Constraint, Indexes - Conts c. Indexes CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX 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 | SORT_IN_TEMPDB = { ON | OFF } | IGNORE_DUP_KEY = { ON | OFF } | STATISTICS_NORECOMPUTE = { ON | OFF } | STATISTICS_INCREMENTAL = { ON | OFF } | DROP_EXISTING = { ON | OFF } | ONLINE = { ON | OFF } | ALLOW_ROW_LOCKS = { ON | OFF } | ALLOW_PAGE_LOCKS = { ON | OFF } | MAXDOP = max_degree | DATA_COMPRESSION = { NONE | PAGE | ROW } [ ON PARTITIONS ( { number | range } ] [ ON partition_scheme ( column ) | ON filegroup | ON default_filegroup ] [ FILESTREAM_ON { filegroup | partition_scheme }; CREATE INDEX contacts_idx ON contacts (last_name); CREATE INDEX contacts_idx ON contacts (last_name, first_name); CREATE INDEX contacts_idx ON contacts (last_name DESC, first_name DESC); CREATE UNIQUE INDEX contacts_uidx ON contacts (last_name, first_name); Unique Constraint Rename an Index sp_rename 'table_name.old_index_name', 'new_index_name', 'INDEX'; Drop an Index DROP INDEX table_name.index_name;
  • 29. Thanks. REFERENCE : • H T T P S : / / W W W . T E C H O N T H E N E T . C O M / S Q L _ S E R V E R / • H T T P S : / / W W W . G U R U 9 9 . C O M / M S - S Q L - S E R V E R - T U T O R I A L . H T M L • H T T P S : / / W W W . S Q L S E R V E R T U T O R I A L . N E T / ? S = C O N S T R A I N T