Upload
hemanta-kumar-dash
View
265
Download
0
Tags:
Embed Size (px)
DESCRIPTION
gg
Citation preview
Session 1
RDBMS Concepts
The Role of Data
to Administer
to Analyze
to Decide
to Explainto Identify
to Implement
to Monitor
to Plan
What is a DBMS??
DBMSRetrieve Data
Insert Data
Update Data
Delete Data
BrianA004
AllenA003
RickyA008
ShaneA007
Database
Related Records
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
Database Models
RDBMS
Applications forinteracting with
data in DB
RelationalDatabase
RDBMSA suite of S/W programs to
create, maintain DB
Users of RDBMS
DatabaseAdministrators
Application Programmers
End Users
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
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. 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.
SQL Server 2000 features (1)
Easy InstallationIntegration with Internet
Supports Client/Server modelOperating 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
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
SQL Server Client/Server system
Network connection softwareusing Inter Process Communication
SQL ServerDatabase Software
Server
Client Software
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
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
Components of E-R Model
Attributes
A descriptionA description
A PropertyA Property
Entity
Relationship
Entity Relationship Diagram
Entity
Attribute
Relationship
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
ERD and Database Design
Database Designer
Identifies
Entities
Attributes
Relations
Tables
Fields
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
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
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 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
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 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
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) 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
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
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
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
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 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
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 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
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
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’)
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
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)
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