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

SQL Server Notes

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

SQL Server Notes

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

SQL Server:- Database

Database:- Is a place where we can store the data and also retrive ( taking) the
data whenever you want.

Charles Babbage invented computer in 1920 in binary language(1000110)


1940 -----> A language
1950 -----> B Language
1960-1965----> C language
1975-----> C++ language

To Communicate with the computer only to process the data,


In those days the people are using files to store the data the people are facing
tampering of data & there is no security of data.

1970-1975-----> DBMS ( Database Management System) -----> IBM -----> SQL


DBMS----> Is a place where we can store the data & from where we can retrive the
data
SQL-----> is a language where you can communicate with the DBMS
SQL----> Structured Query Language

Types of DBMS:-
1. NDBMS:- Network DBMS ( it got failure that it can't store all types of data)
2. HDBMS:- Hierarchical DBMS ( it also got failure)
3. RDBMS:- Relational Database Management System
It can store any type of data & it is very easy to write & understand the syntax.

Microsoft:- MSSQL
MySQL:-
Oracle SQL:- Oracle
Postgre SQL
T-SQl
NEteza
Teradata

RDBMS:- Relational Database Management System which is used to store & Retrive the
data & Sql is the language used to communicate with the RDBMS.

SQL Server(RDBMS) which is invented by Microsoft & we are going to discuss with MS-
SQL
SQL server:- RDBMS
Ms-SQL:- Language
SSMS:- Application

Microsoft:- Azure, Power BI


Google:- Google Cloud Providers, Google analytics studio
Amazon:- AWS,

1989-----> SQL Server 1.0


3.0
5.0
7.0
2000 ----> SQL server 2000
2005 ----> SQL server 2005( MSBI)
2008----> 2008 R2
2012
2014
2016
2017
2019
2022------> Current Version

Mobile-----> Gallery, Google drive


Gallery ----> STore some data---> Hard disk ---> On premise sources
Google Drive --> Store Some data----> Network ---> Cloud Sources

MS- SQL server----> On - premise Data source

SSMS ---> SQL server Management Studio

Pre-Requisites to install This SSMS:-


1. You Needs to Have Windows Operating system version 10 or Later Version
2. Needs to have min 2GB RAM
3. Any Processor will supports
4. Min 100GB Hard Disc

Where we can get this installation?


https://learn.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-
ssms?view=sql-server-ver16

Topics Of SQL server:-


1. Intro of DBMS & RDBMS
2. SQL server History
3. Database Design Features
4. Datatypes
5. Business Integrity Rules( PK, FK, UK, NULL, check constraints)
6. SQL Commands ( DDL, DML, TCL, DCL)
7. Write a SQL query
8. To store the data
9. To Retrive the data
10. Views & Indexed Views
11. Stored Procedures
12. SQL Functions
13. Case Studies

In a single object they can install & maintain upto 50 servers. Each server is
like a each business.
Each server installation is called as 'Instance' & one instance won't spill the
data for another instance.

50 Instances ----> 1 st instance is called as default instance


49 instances-----> Named Instances

For Named instances we needs to name each instance by ourself,


we are not naming the default instance then what name it will take for this
instance?
it will take your Laptop Username as a default instance name.
To view the instance name :-
SELECT @@SERVERNAME

The data will be storing inside the database but not in the form of the database
Then how?
The data inside the SQL server instance database in the form of Rows & Columns ==>
Table

To create a database:-
Create database "Data Analyst Training"

To Create a Table:-
First you needs to mention in which database the tables needs to be created so, for
this:-
Use Databasename
Create Table Tablename
(
Table structure:- Column names, Column Datatype
)

How to create the table along with the table structure?

In RDBMS each character occupies 1 byte


Datatypes:-
Text
1. Char:- is only for fixed , small size of characters (8000)- 8kb
2. Varchar:- is used to allow values of different sizes (8000)-8kb
3. Varchar(Max):- is used when there is no limit of text size (>8000)>8kb -
<=2GB

Number
1.Whole Number:- without decimals
a.tinyint:- o to 255(1kb)
b.smallint:- (2kb)
c.int(4kb)
d.bigint
2.Decimal number:- with decimals
a.decimal:- 38 digits ---> 2111122474.93
b.Numeric:- > 38
3.Approximate:- exact values
a.Float---> 4bytes --> 23.133333333333
b.real----> 8bytes ---> 23.13333333333333333333333333333333333

Date & Time:- Accepts the Calendar details


1.Smalldatetime:-
2.Datetime:- jan 1 1753 00:00:00 to 31 dec 2079 11:59:59
3.date:- jan 1 0001 to dec 31 9999
4.Time:- 11:59:45

Monetary datatype:- used for currency or money


1.smallmoney:- 4b ----> 1, 10, 100, 10000, lakhs
2.Money:- 8b ----> billions, trillions

Binary:- hexadecimal(1,0)
If you are updating/inserting any file format data(image, video, audio), the Sql
engine will converts this files and stores these files in Binary datatype. ---->
2GB

Business Integrity Rules in Database:-


To maintain the correct business data to maintain in a right way means if the user
itself or the data itself is wrong the engine needs to notify us.

1. any ID column needs to be unique ( Passport, AAdhar, Emp id)


2. transaction amount cannot be negative.

Constraints:- these constraints will/ used to validate the data in Sql server
1. NULL, NOT NULL
2. Default
3. Primary Key
4. Foreign Key
5. Unique Key
6. Check

Key:- Column with unique identity is called as key


Primary Key :- if the key column is maintaining the data without duplicates and
null values is called as a primary key.
In Every table there must be one Primary Key Column only.

If there is already one primary key column exists there will be no second primary
key column,
But if there is another unique data maintaining columns those all columns will
called as Unique Keys.

Foreign Key:- if the primary key in one table is passing from that table to
another/ Opposite table the same primary key will change its name as a foreign key.

If you are connecting two tables using Primary key and foreign key is called as
relationships.
any database is supporting Relationships that database is called as Relational
Database management system.

3 types of Relationship architecture( Data Modeling):--


1. Entity Model RA :- RDBMS
2. Dimensional RA :- BI:- Tableau, Power BI, Cognos
3. Object Oriented RA :- App development

Mysql
Oraclesql
Neteza
Teradata
MsSQL

NoSQL -----> Non-Relationships DBMS

In Entity Model Relationship Archtecture:-


3 types of relationships/ Cardinalties:-
1. 1-1:-
If one table PK is acting as a Foreign key in Opposite table and the same
opposite table Primary key is acting as a Foreign key in th first table.
:- 2 Tables, 2Pk's, 2 Fk's

2. 1-M:-
If One table Primary Key acts as a foreign key in opposite table but the same
opposite table primary key can't act as a foreign key in the first table.

:- 2 Tables, 2 Pk's, 1 FK

and this single passing direction of relationship is called as referential


integrity.

3. M-M:-
To Build The M-to- M relationship we needs to maintain the Foreign keys from
both the tables in a seperate table and by using this seperate table we can build
m-to-m relationship and this seperate table is called as a bridge Table.

:- 3 Tables, 2 PK's, 2 Fk's.

Syntax :-

Create Table <table name>


(
Columnname datatype Constraint
)

Create tabel Emp


(
EmpID Int Primary Key,
EmpName Varchar(100) Not Null,
EmpSalary Money

Cascade Features:-
Cascade Features is one of the Option BY SSMS to make sure the Data in a Primary
key will be updating in Foreign Key everytime.

Cascading Feature is 'ON':-

Automatically the data will be updated and deleted the same information in FK also
be reflected.

Self Referential Tables:-


In a Table if the Primary Key is Describing/ Directing The foreign Key in a same
Taable then the table is called as a Self Referential Table.

Is it Required PK in every table?


Yes.

How Many PK's We can Have in a Single Table?


only 1.

How Many UK's we can have in a Single Table?


Many.

Is Primary Key Accepts the Null values?


No.
What is The Diff b/w PK & UK?
PK cannot accept the Null records but, UK can accept null Record but Only 1 Null
record.

How Many FK we can have in a single table?


Many

Is Fk Can Accept Null Values?


Yes, it can accept many null records.

What is The Name of This Procedure?


Data Modeling:- Establishing relationships between different different tables using
Primary key and foreign keys is called as Data Modeling.

Data Warehousing:-

SQL Commands:-

1. DDL:- Data Definition Language:- To create Database, Table structure


a. CREATE
b. ALTER
c. DROP
d. TRUNCATE

2. DML :- Data Manipulation Language


a. SELECT
b. INSERT
c. UPDATE
d. DELETE

3. TCL:- Transaction Control Language


a. COMMIT
b. ROLLBACK

4. DCL:- Data Control Language -----> DBA( Database Administrators)


a. GRANT
b. DENY

Database:- it is a place where it will be holding all the transactions we are doing
in SQL server.
There are two types of databases in SQL Server:-
1. SYSTEM Databases:- created by SSMS by default
a. Master:- Every Password, Usernames, Instance details
b. Model:- Template of the SSMS
c. msdb:- Alerts & Backups
4. Tempdb:- Calculations, Creations at the time will be stored temporily.

2. USER DEFINED Databases:- is created by developer/ user.

While Creating The Database by User the SSMS will be saving this Database data in
two File Types:-

1--> .mdf (Metadata file) ---> Every data will be stored in this file
2--> .ldf (Log Data File) ----> every time while creating the data will be stored
in this file.

Comments:- While writing SQL queries if you want to store some decriptions/
Comments you needs to use:--

-- (SIngle Line Comment)

-- Hello Humans (Single Line Comment)

/*
This is rubicon infotech Data analyst Sessions
By Durga Rao
Classes availabe here:- EXCEL, SQL, Power BI
*/

-- Creation of database for sample training


-- Syntax
-- Create Database <database name>

Create database sample_Training


go

-- Before creating table check in which database you are in


-- If you want to navigate into your database just use
-- Use <database name>

Use sample_Training
go

/*
If you wants to create a table first you needs to maintain
some syntax means you needs to create table structure along
with the table,
Table structure:--- Column names, Datatypes of column
*/

/*
Syntax of table creation:-
Create Table <Table Name>
(
Columnname datatype constraint,
Columnname2 datatype Constraint
)
*/

Create Table emp_table


(
Emp_Id Int primary key,
EMP_Name Char(50) Not Null,
EMP_salary Money
)
Constraints:-

You might also like