1-RDBMS- SQL

58
Session 1 RDBMS Concepts

description

gg

Transcript of 1-RDBMS- SQL

Page 1: 1-RDBMS- SQL

Session 1

RDBMS Concepts

Page 2: 1-RDBMS- SQL

The Role of Data

to Administer

to Analyze

to Decide

to Explainto Identify

to Implement

to Monitor

to Plan

Page 3: 1-RDBMS- SQL

What is a DBMS??

DBMSRetrieve Data

Insert Data

Update Data

Delete Data

BrianA004

AllenA003

RickyA008

ShaneA007

Database

Related Records

Page 4: 1-RDBMS- SQL

Benefits of DBMS

The amount of redundancyredundancy in the stored data can be reducedreduced

No more inconsistenciesNo more inconsistencies in data The stored datadata can be sharedshared StandardsStandards can be set and followed Data integrityData integrity can be maintained SecuritySecurity of data can be implemented

Page 5: 1-RDBMS- SQL

Database Models

Page 6: 1-RDBMS- SQL

RDBMS

Applications forinteracting with

data in DB

RelationalDatabase

RDBMSA suite of S/W programs to

create, maintain DB

Page 7: 1-RDBMS- SQL

Users of RDBMS

DatabaseAdministrators

Application Programmers

End Users

Page 8: 1-RDBMS- SQL

Introduction to SQL Server 2000

SQL Server 2000 comes in various editions like: SQL Server 2000 Enterprise Edition SQL Server 2000 Standard Edition SQL Server 2000 Personal Edition SQL Server 2000 Developer Edition SQL Server 2000 Desktop Engine SQL Server 2000 Windows CE Edition

Page 9: 1-RDBMS- SQL

Relational features of SQL Server 2000

Page 10: 1-RDBMS- SQL

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. DON’T!!! 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. 

Page 11: 1-RDBMS- SQL

SQL Server 2000 features (1)

Easy InstallationIntegration with Internet

Supports Client/Server modelOperating System Compatibility

Page 12: 1-RDBMS- SQL

SQL Server 2000 features (2)

Page 13: 1-RDBMS- SQL

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

N/A N/A Supported N/A Supported N/A N/A

Windows NT 4.0 Workstation

N/A N/A Supported Supported Supported N/A Supported

Windows NT 4.0 Server

Supported Supported Supported Supported Supported N/A Supported

Microsoft Windows NT 4.0 Server, Enterprise Edition

Supported Supported Supported Supported Supported N/A Supported

Windows 2000 Professional

N/A N/A Supported Supported Supported N/A Supported

Windows 2000 Server

Supported Supported Supported Supported Supported N/A Supported

Windows 2000 Advanced Server

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    

Page 14: 1-RDBMS- SQL

SQL Server Client/Server system

Network connection softwareusing Inter Process Communication

SQL ServerDatabase Software

Server

Client Software

Page 15: 1-RDBMS- SQL

Client/Server mechanism

ODBC ODS

Page 16: 1-RDBMS- SQL

SQL Server as a Desktop Database System

Page 17: 1-RDBMS- SQL

SQL Server Databases

Page 18: 1-RDBMS- SQL

SQL Server Database Objects

Page 19: 1-RDBMS- SQL

SQL Server Security

Page 20: 1-RDBMS- SQL

Permission Validation Process

Page 21: 1-RDBMS- SQL

It is a ‘conceptual model’ of the database Here, a system is composed of related

entities

The E-R Model(1)

Database Designer End User

Tables identified

Page 22: 1-RDBMS- SQL

Components of E-R Model

Attributes

A descriptionA description

A PropertyA Property

Entity

Relationship

Page 23: 1-RDBMS- SQL

Entity Relationship Diagram

Entity

Attribute

Relationship

Page 24: 1-RDBMS- SQL

Types of Relationships

One to One 1:1

Places

Order1 N

M

Item

Contains

N

Customer

Has

C_Code

1

1

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

Page 25: 1-RDBMS- SQL

ERD and Database Design

Database Designer

Identifies

Entities

Attributes

Relations

Tables

Fields

Page 26: 1-RDBMS- SQL

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’

Page 27: 1-RDBMS- SQL

Characteristics of a normalized database

Page 28: 1-RDBMS- SQL

PUBS DATABASE

title_id = title_id

title_id = title_id

title_id = title_id

stor_id = stor_id

stor_id = stor_id

pub_id = pub_id

pub_id = pub_id

pub_id = pub_id

job_id = job_id

au_id = au_id

authors

au_id varchar(11)au_lname varchar(40)au_fname varchar(20)phone char(12)address varchar(40)city varchar(20)state char(2)zip char(5)contract bit

authors_tmp

au_lname varchar(40)au_fname varchar(20)phone char(12)address varchar(40)city varchar(20)state char(2)zip char(5)

discounts

discounttype varchar(40)stor_id char(4)lowqty smallinthighqty smallintdiscount decimal

employee

emp_id char(9)fname varchar(20)minit charlname varchar(30)job_id smallintjob_lvl tinyintpub_id char(4)hire_date datetime

jobs

job_id smallintjob_desc varchar(50)min_lvl tinyintmax_lvl tinyint

pub_info

pub_id char(4)logo imagepr_info text

publishers

pub_id char(4)pub_name varchar(40)city varchar(20)state char(2)country varchar(30)

roysched

title_id varchar(6)lorange inthirange introyalty int

sales

stor_id char(4)ord_num varchar(20)ord_date datetimeqty smallintpayterms varchar(12)title_id varchar(6)

stores

stor_id char(4)stor_name varchar(40)stor_address varchar(40)city varchar(20)state char(2)zip char(5)

titleauthor

au_id varchar(11)title_id varchar(6)au_ord tinyintroyaltyper int

titles

title_id varchar(6)title varchar(80)type char(12)pub_id char(4)price moneyadvance moneyroyalty intytd_sales intnotes varchar(200)pubdate datetime

Page 29: 1-RDBMS- SQL

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.

Page 30: 1-RDBMS- SQL

When to use Normalization??

Page 31: 1-RDBMS- SQL

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.

Page 32: 1-RDBMS- SQL

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 Mathematical Data Processing

Table Relation File

Row Tuple Record

Column Attribute Field

•These terms are used interchangeably, but we will generally use the table – row – column terminology

Page 33: 1-RDBMS- SQL

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

Page 34: 1-RDBMS- SQL

Design SQL Server Tables

What tables are needed Type of data in each table Restrictions on data

Keys Default values Indexes Null values

Page 35: 1-RDBMS- SQL

Creating Tables

Page 36: 1-RDBMS- SQL

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

Page 37: 1-RDBMS- SQL

NAMING A COLUMNUp 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

Page 38: 1-RDBMS- SQL

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

Page 39: 1-RDBMS- SQL

Data types

Page 40: 1-RDBMS- SQL

System defined data types

Page 41: 1-RDBMS- SQL

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

Page 42: 1-RDBMS- SQL

NUMERIC DATATYPES

Tinyint Whole numbers between 0 and 255 1 byte of storage

Smallint Whole numbers between -32678 and

32677 2 bytes of storage

Page 43: 1-RDBMS- SQL

NUMERIC DATATYPES

numeric(p,s) Decimals between -1038 and 1038-1 2 to 17 bytes

decimal(p,s) 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

Page 44: 1-RDBMS- SQL

Float(p) Floating point numbers 4 or 8 bytes of storagedouble precision floating point numbers 8 bytes of storagereal floating point numbers 4 bytes of storage

Page 45: 1-RDBMS- SQL

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

Page 46: 1-RDBMS- SQL

Datetime 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

Page 47: 1-RDBMS- SQL

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

Page 48: 1-RDBMS- SQL

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

Page 49: 1-RDBMS- SQL

Text

Unbounded alphanumeric data 16 bytes for an address + multiples

of 2K

Page 50: 1-RDBMS- SQL

Bit 0 or 1 1 byte of storage

Binary(n) up to 255 bytes n bytes of storage

Varbinary up to 255 bytes actual length of data

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

storage

Page 51: 1-RDBMS- SQL

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)

Page 52: 1-RDBMS- SQL

User Defined data types

Example: You can create a user-defined data type named PIN that would consist data for the six digit pin number.

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

Page 53: 1-RDBMS- SQL

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

Page 54: 1-RDBMS- SQL

Modifying Tables

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

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’)

Page 55: 1-RDBMS- SQL

Modifying Tables

Update RowsUPDATE <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

Page 56: 1-RDBMS- SQL

Modifying Tables

DELETE FROM <Table_name> WHERE <Search_Condition>

Delete Rows

Delete From ROOM ( All the Rows will be deleted)

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

Page 57: 1-RDBMS- SQL

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

Page 58: 1-RDBMS- SQL

Removing tables