Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

1-RDBMS - SQL

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 58

Session 1

RDBMS Concepts

The Role of Data

to Identify

to Explain

to Administer

to Plan

to Monitor
to Decide

to Analyze

to Implement

What is a DBMS??
Database
s
d
r
o Shane
A003 Allen
A007
c
e
R
d
A004 Brian
te A008 Ricky
a
l
Re
Retrieve Data

Update Data

Insert Data

Delete Data

DBMS

Benefits of DBMS

The amount of redundancy in the stored


data can be reduced
No more inconsistencies in data
The stored data can be shared
Standards can be set and followed
Data integrity can be maintained
Security of data can be implemented

Database Models

RDBMS
Relational
Database

Applications for
interacting with
data in DB

RDBMS
A suite of S/W
programs to
create, maintain
DB

Users of RDBMS
Database
Administrators
End Users

Application Programmers

Introduction to SQL Server 2000

SQL Server 2000 comes in various


editions like:

SQL
SQL
SQL
SQL
SQL
SQL

Server
Server
Server
Server
Server
Server

2000
2000
2000
2000
2000
2000

Enterprise Edition
Standard Edition
Personal Edition
Developer Edition
Desktop Engine
Windows CE Edition

Relational features of SQL Server


2000

Relational features of SQL Server


2000
SQL - Structured Query Language
SQL is probably one of the simplest languages you will ever
learn. It is also very simple to underestimate. DONT!!! This
is arguably the most powerful language you will learn.
SQL is a set oriented language. It was designed and built to
manage groups of data.
ER Diagram - Entity Relationship diagram
An ER Diagram, also known as a database schema, gives
you a graphical depiction of the database you are working
with.

SQL Server 2000 features (1)

Easy Installation

Integration with Internet

Supports Client/Server model


Operating System Compatibility

SQL Server 2000 features (2)

SQL Server 2000 OS support


Operating System

Enterprise
Edition

Standard
Edition

Personal
Edition

Developer
Edition

Desktop
Engine

SQL
Server
CE

Enterprise
Evaluation Edition

Microsoft Windows
98
Windows NT 4.0
Workstation

N/A

N/A

Supported

N/A

Supported

N/A

N/A

N/A

N/A

Supported

Supported

Supported

N/A

Supported

Windows NT 4.0
Server
Microsoft Windows
NT 4.0 Server,
Enterprise Edition

Supported

Supported

Supported

Supported

Supported

N/A

Supported

Supported

Supported

Supported

Supported

Supported

N/A

Supported

Windows 2000
Professional

N/A

N/A

Supported

Supported

Supported

N/A

Supported

Windows 2000
Server
Windows 2000
Advanced Server

Supported

Supported

Supported

Supported

Supported

N/A

Supported

Supported

Supported

Supported

Supported

Supported

N/A

Supported

Microsoft Windows
2000 DataCenter

Supported

Supported

Supported

Supported

Supported

N/A

Supported

Microsoft Windows
CE

N/A

N/A

N/A

N/A

N/A

SQL Server Client/Server system


e
v
r
Se
Client Software

SQL Server
Database Software

Network connection software


using Inter Process Communication

Client/Server mechanism

ODBC

ODS

SQL Server as a Desktop


Database System

SQL Server Databases

SQL Server Database Objects

SQL Server Security

Permission Validation Process

The E-R Model(1)

It is a conceptual model of the database


Here, a system is composed of related
entities

Database
Designer

Tables
identified
End User

Components of E-R Model


Entity

Attributes
A description
A Property

Relationship

Entity Relationship Diagram

Entity
Attribute
Relationship

Types of Relationships

1
Ha
s1
C_Code

One to One
1:1
One to Many 1: N
Many to Many N:M
Customer

Place
s

Item

N
Contain
s

Order

ERD and Database Design


Entities
Tables

Identifies
Database
Designer

Attributes
Fields
Relations

Creating Tables - Normalization


Points to Note

Well defined relationship between tables


Unnecessary or redundant data kept
away.
Group related data

Achieved through a technique


called Normalization

Characteristics of a normalized
database

PUBS DATABASE
stor_id
ord_num
ord_date
qty
payterms
title_id

sales
c har(4)
varc har(20)
datetime
smallint
varc har(12)
varc har(6)

stor_id = stor_id

stores
stor_id
c har(4)
stor_name
varc har(40)
stor_address varc har(40)
city
varc har(20)
state
c har(2)
zip
c har(5)

stor_id = stor_id

title_id = title_id

titleauthor
au_id
varc har(11)
title_id
varc har(6)
au_ord
tinyint
royaltyper int
au_id = au_id

authors
au_id
varc har(11)
au_lname varc har(40)
au_fname varc har(20)
phone
c har(12)
address
varc har(40)
c ity
varc har(20)
state
c har(2)
zip
c har(5)
c ontrac t
bit

title_id = title_id

title_id
title
type
pub_id
pric e
advanc e
royalty
ytd_sales
notes
pubdate

titles
varc har(6)
varc har(80)
c har(12)
c har(4)
money
money
int
int
varc har(200)
datetime

title_id = title_id

roysched
title_id
varc har(6)
lorange int
hirange int
royalty
int

pub_id = pub_id

publishers
pub_id
char(4)
pub_name varc har(40)
c ity
varc har(20)
state
char(2)
c ountry
varc har(30)

discounts
disc ounttype varc har(40)
stor_id
c har(4)
lowqty
smallint
highqty
smallint
disc ount
decimal

pub_id = pub_id

employee
emp_id
c har(9)
fname
varc har(20)
minit
c har
lname
varc har(30)
job_id
smallint
job_lvl
tinyint
pub_id
c har(4)
hire_date datetime
job_id = job_id

job_id
job_desc
min_lvl
max_lvl

jobs
smallint
varc har(50)
tinyint
tinyint

pub_id = pub_id

pub_info
pub_id c har(4)
logo
image
pr_info text

authors_tmp
au_lname varc har(40)
au_fname varc har(20)
phone
char(12)
address
varc har(40)
c ity
varc har(20)
state
char(2)
zip
char(5)

DATABASE
At the most basic level a database is really just a file.

Databases come in all shapes and sizes. Some are large and
some are small. But each database generally serves a particular
purpose.

Examples: Tracking employee payroll, sales data on a particular


sales line, stock data for a particular industry

All databases are made up of objects. The most important


object (and the one we will learn how to use in this class) is a
table.

When to use Normalization??

Features of tables

Each table must have unique name


Each row in the table must be unique and
describe one set of related information about the
subject of the table.
Each column or field describes a single
characteristic of an entity.
Column name must be unique within the table.
The order of rows or columns is not significant.

TABLES
A table is a storage structure made up of rows and columns.
(Sort of like a spreadsheet.)
Due to the differing terminologies, there are interchangeable
sets of terms:
Database
Table
Row
Column

Mathematical Data Processing


Relation
Tuple
Attribute

File
Record
Field

These terms are used interchangeably, but we will generally use


the table row column terminology

TABLES
You will also hear a table referred to as an
entity. (Hence the name Entity
Relationship Diagram)
In the most basic sense, an entity is a
person, place, thing, or idea.
Entities usually become tables
Example: books, publishers, titles, authors

Design SQL Server Tables

What tables are needed


Type of data in each table
Restrictions on data

Keys
Default values
Indexes
Null values

Creating Tables

NAMING A TABLE
Up to 30 characters
No blanks
Underscore is permitted
Must be unique within the database
Keep abbreviations to a minimum
Names should reflect the contents of the table

NAMING A COLUMN
Up to 30 characters
No blanks
Underscore is permitted
Must be unique within the table
Keep abbreviations to a minimum
Names should reflect the contents of the column
Names should be readily recognizable

Data types

Determine what type of data will be stored


Find the range of possible values
Determine the accuracy of numeric
columns
Efficiency
Utilize user defined datatypes to enforce
consistency

Data types

System defined data types

DATATYPES

Exact Numeric
Stores data with a specific accuracy
Approximate numeric
Stores data with accuracy dependent upon
calculations performed
Money
:
Monetary data
Date and time :
Storage of dates and times
Character
:
Alphanumeric data
Binary
:
Images, byte and bit values

NUMERIC DATATYPES

Tinyint
Whole numbers between 0 and 255
1 byte of storage

Smallint
Whole numbers between -32678 and
32677
2 bytes of storage

NUMERIC DATATYPES

numeric(p,s)

decimal(p,s)

Decimals between -1038 and 1038-1


2 to 17 bytes
Decimals between -1038 and 1038-1
2 to 17 bytes

s = number of digits to the right of the


decimal
p = total number of digits

Float(p)

Floating point numbers


4 or 8 bytes of storage

double precision

floating point numbers


8 bytes of storage

real

floating point numbers


4 bytes of storage

Money
-922,337,203,685,477.5808 to
922,337,203,685,477.5807
8 bytes of storage
SmallMoney
-214,748.3648 to 214,748.3647
4 bytes of storage
Accurate up to 4 decimal places, but
are rounded to 2 places when
displaying

1/1/1753 to 1/31/9999
8 bytes of storage

Smalldatetime

1/1/1900 to 6/6/2079
4 bytes of storage
There is no separate datatype for just times
Time is stored along with a date with an accuracy of
1/300th of a second
Due to the limitation on the range of data, a
smalldatetime should no longer be used.
Dates and times can be entered into these columns
using a wide variety of date and time formats
The default display type is determined from the default
language for the server

Datetime

Due to the limitation on the range of


data, a smalldatetime should no
longer be used.
Dates and times can be entered into
these columns using a wide variety
of date and time formats
The default display type is
determined from the default
language for the server

char(n) and nchar(n)


Fixed length alphanumeric data
n bytes of storage

varchar(n) and nvarchar(n)


Variable length alphanumeric data
actual length of data
Char datatypes provide a small performance
benefit over varchar.
If the data size is predictable or 8 characters or
less, use a char
nchar and nvarchar are used to store multi-byte
characters such as Chinese and Japanese

Text

Unbounded alphanumeric data


16 bytes for an address + multiples
of 2K

Bit

Binary(n)

up to 255 bytes
n bytes of storage

Varbinary

0 or 1
1 byte of storage

up to 255 bytes
actual length of data

Image

up to 231-1 bytes
16 bytes address + multiples of 2K bytes of
storage

The text and image datatypes are used to


store large amounts of data
Text is used for alphanumeric data
Image is used for binary data

It is highly recommended that you


avoid this two datatypes if at all
possible
They can cause a serious performance
problem as well as a space problem.
Support for them is very limited
(specialized functions are required to
manipulate them)

User Defined data types

Example: You can create a user-defined data type named PI


that would consist data for the six digit pin number.

This data data type can be used whenever there is need for
storing pin numbers

You add new types with sp_addtype

exec sp_addtype MyInt int not


null
This says to create a new type called MyInt
that is based upon an integer and can not
be null
exec sp_addtype MyChar char(6) not
null
This is a new type called MyChar that can
contain up to 6 characters and can not be null
Use sp_droptype and sp_helptype to drop and
return information about user datatypes

Modifying Tables
Add Rows
INSERT [INTO] <Table_name> VALUES <values>

Insert into Room values(R001,10,AC)


Insert into Room(RNO,Capacity) values(R001,10)
-If The Type=Null( Defined Previously) OR
- If the Type Defined as Some Default Value(AC)

Modifying Tables
Update Rows
UPDATE <Table_name> SET <Column_name = Value>
WHERE <Search_Condition>

Update Room set capacity=12 (The Capacity of All


the Record will be 12)
Update Room set capacity=12 Where
Rno=R001(The Capacity of the Record will be 12
where Room No=R001)
Multiple Fields/Columns Can be updated using one
Update Command

Modifying Tables
Delete Rows
DELETE FROM <Table_name> WHERE <Search_Condition>

Delete From ROOM ( All the Rows will be deleted)


Delete From ROOM where RNO=R001 ( Only
R001 Row will be deleted)

Viewing the Tables

Syntax: To view the information on a table using a stored


procedure.
Sp_help <Table_name>
Sp_help Student
Syntax: To view the data in the table.
SELECT <Select_list> FROM <Table_Name>

Select Roll, Name, City FROM Student

Removing tables

You might also like