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

SQL Bhaskar Sir

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

SQL Bhaskar Sir

SQL Bhaskar Sir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

1&2) Introduction & SQL Server DB Archtechture-1

RDBMS Concept-----(Dr E F Codd------IBM – 1970)

There are 12 Codd Rules

Must satisfy 10 Codd Rules to be RDBMS

RDBMS concept -> Backend Apps

Oracle, SQL Server (1989), MySQL, Tera Data, DB2, Nateeza, PostGre, SQL

Lang (IBM 1970)

SQL Structured Query Lang.

We can install SQL 50 times on a single computer so that we don't need to set up separate
hardware for each client, we can manage 50 clients with a single machine.

Each setup is called as Instance.

In 50 Instances first one is called as Default Instance and the remaining 49 will be called as
Named Instances. Default Instance will be having Windows Name.

SSMS (SQL Server Management Studio)


3)SQL Server DB Archtechture-2

1 Computer  50 Instances

1 Instance  32767 Databases

1 Database  32767 Filegroups

1 Database  1 or more Txn Log Files

*Database is collection of Filegroups and collection of Transaction Log Files*

1 Filegroup  32767 Data File

1 Data File  1 or more Extent

1 Extent  8 Pages (*1 page is 8kb i.e. 1 Extent is 64kb)

1 or more Pages  Table


04 SQL Server Data Types-1:-

*if
we
take 10 as limit and go with the char we have space according to the inputs in other hand varchar
will ignore the extra space and continue to the next inputs.

So we can observe wastage of space in char but we have to update any input it will be the difficult to
do the changes in varchar because of no space left so that it will change the placements of each and
every input in the table while in char we can make update the inputs without disturbing the other
placements.

05 SQL Server - Data Types Part2


*Binary data type is used to store media files to store in the form of Hexa Decimal.

*If we have Fixed length data will use Binary which is up to 8kb.(each media)

*If we do not have Fixed length data can use VarBinary which is from 1kb to 2GB.(each media)
*If we are maintaining the same data in two different servers and we need to update the changes in
server 2 accordingly for this we need to maintain the TimeStamp data type so that it will give us
some random numbering for the data we given and it will change is whenever the data got updated
in server1, so that we can easily make the same updates or changes in the server 2.

*Let’s assume Microsoft and IBM are merging and so they need to merge their employees data
together so in this case GUI generates some unique id to each and every emp so that no emp id will
be mix-up.

*XML: Extensible Markup Language

Invented by W3C- World Wide Web

HTML to Create Websites

XML to remove platform or Language dependency.


06 SQL Server - Business Integrity Part1
Business Integrity means data must be truth (one of the rules in 12 Codd rules)
*Only drawback is the Insertion (data loading) is slow.

07 SQL Server – Keys


Key means which is an attribute or multiple attributes using which any one can retrieve a single
instance from the group of instances.

RDBMS have lot of Entities (Tables)

Entities have lot of Attributes (Columns)

Entity Record means Row or Record.

Key is an attribute which does not any duplicate values.

Purpose: To retrieve the single or particular instance.


*

*Composite Key- A key which has multiple attributes


In this ex, Towe No & House No are two attributes put together we can call it Composite Primary
key. And Null values not allowed bcz not part of the primary key allow Null values. Uniq Key allows
one null value.

08 SQL Server - Relationships


***we can use
UK as FK
instead of
PK***

**Extra
table will
be called
as “BRIDGE
TABLE”
09 SQL Server - FKs, Cascade Features, Check Constraints, Identity

****this error will be FK


Violation error***

***Let us assume In this scenario one or


more Emp’s are working for the same Dep
then we can’t take EID as FK bcoz if we did
that we need to enter the multiple EID
values in a single cell so instead of that we
can take DepID as FK in EMP table***

**Technical person has no idea about how the tables will link we need get this information
from the client or domain person to know the link b/w the tables simply we need to
understand the business to know about the relationship’s b/w the tables**
Can I have duplicate values in FK?
YES
Can I have null values in FK?
YES
Can I have null values in PK?
NO
Can I have null values in PK?
Yes(Only one value per Col)
How many FK’s per table?
Many
How many PK’s per table?
One
How many UK’s per table?
Many
What is self-Referential table?
A FK is referring its own tables PK, Then the table is called as self-Referential table.

Cascade delete and Cascade Update


If we switching ON the Cascade delete and Cascade Update options, the changes we made
in Parent Table will reflect in Child Table.
We can still made changes in Parent table by switching OFF these options but it will not
affect the Child Table

***These Cascade features cannot Implemented on Self-Referential Tables***….


Check Constraints

**Grade A or B & Gender M or F are column level**


**<5000 & >5000 are table level**

Identity Property

Identity will have two strains one is SEED another one is Increment/Decrement

In the above ex we can observe that EID autogenerated with the help of Identity feature. Seed
starts at 1000 and Decrement given -1 so the EID will auto generate the values from 1000 by
decreasing 1 value.

In simple manner by using Identity Property, we can auto generate one column per table.
10 SQL Server - SQL Commands Part-1

SQL Server & SQL


are not same. SQL
is a Language SQL
Server is a tool.

To Open SQL Server

Start—SSMS (SQL Server Management Studio) – Enter PC Name or (.) & connect to database (“*” is
a default instance)

Two types of db’s

1 System DB : Master, Model, Msdb & tempdb

Master (Configuration, User ID Passwords & All other db info)

What if Master Data Deleted??

SQL Server will not start/work.

Model is a Templet db we can use while creating a new db..

Msdb we use this for automate jobs (ex: Backup db)/alerts(sms/email).

tempdb is a temporary db that have global resources. As soon as we come out of the server the
tables & everything will drop permanently.

2 User Defined DB

Given by MS for Learning purpose.


**these two put together we can have 32,767 in a single server/Instance**

Create a New Database

Right click on Databases & Click New Database.

When you create a new database, we will get two files named .mdf & .ldf

.mdf (Data File) -- Data

.ldf (Log File) – Log when something goes wrong with data file we can refer this file, it is like a
tracker of data file.

Databases

But this is not the we create a db we need to write a command to create a new db.
To do that we have click on “New Query”

This is the window where we can write all ‘SQL


Commands’

For writing single line comment we will use

“- -abc..“
For writing multiple lines comment we will use
/* a

*/
Syntax Command for create a New DB

Here we can see by using this command we created a new db called “Company”

Now we need to create Tables in the db we created “Company” for that we need to write a command “Use
Database” and we need to click on “Execute” to pointing the “company db”

Syntax for Create a Table

Ex: Create Department Table

Now need to select the commands


and execute the table will be
created. After refreshing we can
see the table.
Syntax for See the table

after executing we can see the table but there is no data in


the table next step is insert the data into table.

Syntax for Insert the data into a table

after executing this we can have the data as per the commands given

again we need to execute the


syntax for see the table then we can find the data we given under the table
11 SQL Server - SQL Commands Part2 and Transactions
Syntax for Schema of the table

Syntax for Delete all rows from the table

delete from Dep

go
Syntax for Delete single row from the table

*there is no way to delete a


particular cell in SQL Server
we need to delete the entire
row**

Syntax for update/modify


the data

**
we
can use this command for remove a particular cell value by
updating/modifying like this**

Syntax for update/modify the data for all row’s

update from Dep

set DeptName = ‘new value’

we must careful while doing this command because every row value will be changed to new value
and it’s very difficult to get back to the original data.

So We need to use TCL commands to for this kind of modification

**here we changed all


dept names to R&D so
resulting all the 6 rows
got affected here we
need to rollback the
values to original, if I
use Commit these
change will made
permanently so we
don’t use Commit here.
**we must execute
command by using
Rollback only then all the affected rows will be back as old data**

Multiple TCL commands

** here if the data given is perfect then we execute Commit command to save the data once we
use commit, we cannot get back the data by rollback**

12 SQL Server - SQL Commands Part 3


Create Emp Table with the req’s:

Emp Id: not allows repeated values

Emp name: contains up to 100 character’s & not allows null values

Salary: not allows null and negative values

DOB: not allows null value

Phone No: allows null and unique.

Email ID: unique & contains up to 50 characters’, allows null value.

Passport: unique & contains up to 10 characters’, not allows null value.

Dep ID: allows null values, foreign key ref Dep(Dep ID)

By executing schema command, we get


13 SQL Server - Identity and Delete Vs Truncate

You might also like