it 390 business database administration
DESCRIPTION
IT 390 Business Database Administration. Unit 4 : Using MS SQL Server 2000 Key Features. Objectives. Review installation options Review how to create a database Create and Manage logins with Transact SQL and Enterprise Manager. Explain how to create and manage logins. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/1.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 1
IT 390 Business Database Administration
Unit 4 :
Using MS SQL Server
2000 Key Features
![Page 2: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/2.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 2
Objectives
• Review installation options• Review how to create a database• Create and Manage logins with Transact SQL and Enterprise
Manager.• Explain how to create and manage logins.• Apply Key commands and features of Microsoft SQL Database
Server and Transact SQL• Create and manage tables, views, indexes, stored procedures
and triggers.• Describe basic concurrency control features and techniques.
![Page 3: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/3.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 3
A Quick Review of a couple of Key Concepts!!
• SQL Server 2000 can be installed on: Windows 2000 and Windows XP workstations Windows 2000 Server and Windows Server 2003
• There are two ways to create database, to insert data, and to modify its structure: Use the GUI SQL Server Enterprise Manager Write SQL statements and submit them to SQL Server via
the SQL Query Analyzer utility
• Many SQL Server professionals choose to create structures via SQL then modify them with the graphical tools
![Page 4: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/4.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 4
View Ridge Gallery
• View Ridge Gallery is a small art gallery that has been in business for 30 years
• It sells contemporary European and North American fine art
• View Ridge has one owner, three salespeople, and two workers
• View Ridge owns all of the art that it sells; it holds no items on a consignment basis
![Page 5: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/5.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 5
Application Requirements
• View Ridge application requirements:
Track customers and their artist interests Record gallery's purchases Record customers' art purchases List the artists and works that have appeared in the
gallery Report how fast an artist's works have sold and at
what margin Show current inventory in a Web page
![Page 6: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/6.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 6
View Ridge Gallery Database Design
![Page 7: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/7.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 7
The IDENTITY Keyword
• SQL Server supports the SQL-92 standard• The IDENTITY keyword implements a new constraint for
surrogate keys: IDENTITY (m, n) creates a surrogate key with an
Identity Seed of m and an Identity Increment of n:CREATE TABLE CUSTOMER(
CustomerID int NOT NULL IDENTITY (1000,1),
Name char(25)NOT NULL,
CONSTRAINT CustomerPK RIMARY KEY (CustomerID),
CONSTRAINT CustomerAK1 UNIQUE (Name)
);
![Page 8: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/8.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 8
SQL Server Enterprise Manager
Right-Click Databases, then use New Database… to create a new database
![Page 9: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/9.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 9
Creating a Database
Type in the new database name, and
then click the OK button.
![Page 10: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/10.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 10
SQL Query Analyzer:Starting SQL Query Analyzer from Enterprise Manager
Use the menu command
Tools | SQL Query
Analyzer.
![Page 11: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/11.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 11
•You can use the CREATE TABLE command to do the following:
Create a new table. Create a new table with columns.
• You use the CREATE TABLE command to create a new table. The syntax for creating a new table is:
CREATE TABLE <table name>
DDL: Using the CREATE TABLE Command
![Page 12: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/12.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 12
Creating a Table Clearly
• The clear syntax to create a new table is:
CREATE TABLE <table name>( <column name1> <data type>, <column name2> <data type>)
![Page 13: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/13.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 13
Activity • Write the SQL statement to create a table named
BookIssue. The column descriptions of the table are:
Field name
Data type Description
iIssueID int Primary key
iBookID int Foreign key to iBookID column of the Books table
iMemberID
int Foreign key to iMemberID column of the Members table
dIssueDate
DateTime Not Null
dReturnDate
DateTime Null
mFine int Null
![Page 14: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/14.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 14
Solution
CREATE TABLE BookIssue (iIssueID int PRIMARY KEY,iBookID int FOREIGN KEY REFERENCES Books(iBookid),
iMemberID int FOREIGN KEY REFERENCES Members(iMemberID), dIssueDate DateTime NOT NULL, dReturnDate DateTime NULL, mFine int NULL)
![Page 15: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/15.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 15
SQL Query Analyzer:Creating the TRANS table in SQL Query Analyzer
Use the Execute Query button after the SQL command
is entered.
Enter the SQL command in this window.
Results appear in this
window.
![Page 16: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/16.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 16
SQL Server CREATE TABLE Statementsfor the View Ridge Schema
![Page 17: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/17.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 17
SQL Server CREATE TABLE Statementsfor the View Ridge Schema
![Page 18: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/18.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 18
SQL Server CREATE TABLE Statementsfor the View Ridge Schema
![Page 19: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/19.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 19
SQL Server CREATE TABLE Statementsfor the View Ridge Schema
![Page 20: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/20.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 20
PQR is a software consultancy group involved in deploying projects that require back-end storage in a DBMS. The organization has recently bid on a project involving a large number of tables and records that requires multiple user access.
Pedro is responsible for choosing a database to deploy the project. Which back-end application can he use in this situation?
Class Activity
![Page 21: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/21.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 21
• A login in SQL Server 2000 is a user account with a predefined set of permissions and privileges on databases and database objects.
• For basic logins in SQL Server 2000 you can:
Add a login
Delete a login
Answer: Logins
![Page 22: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/22.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 22
• The syntax for adding a login is:
sp_addlogin [ @login_name = ] 'login_name' [ , [ @pass = ] 'pass' ] [ , [ @default_db = ] 'db_name' ] [ , [ @default_language = ] 'default_language' ] [ , [ @sid = ] sid ] [ , [ @encryptoption = ] 'encrypt_option' ]
-- Remember the ‘@’ symbol is just a placeholder.
Logins (cont.)
![Page 23: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/23.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 23
sp_addlogin example for Microsoft T-SQL
EXEC sp_addlogin ‘jonesa’, ‘ilikecocoa’, ‘northwind’, ‘us_english’
![Page 24: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/24.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 24
Logins with SQL Server Enterprise Manager
![Page 25: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/25.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 25
Logins with SQL Server Enterprise Manager
![Page 26: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/26.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 26
• What is the basic syntax (keywords) for dropping and granting logins using T-SQL?
Activity
![Page 27: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/27.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 27
• What is the basic syntax (keywords) for dropping and granting logins using T-SQL?
Answer
sp_droploginsp_grantdbaccess
![Page 28: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/28.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 28
XYZ Corp. needs to deploy a large project with a Visual Basic front end and SQL Server 2000 back end. Polly, the DBA, wants to grant different types of permissions to different sets of users. For example, for the group that needs student details, she wants to give access only to the student database and not any other database.
What should Polly do in this situation?
Class Activity
![Page 29: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/29.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 29
• Roles allow you to collect several users into a single unit for which you can apply similar permissions.
• You can assign a role to a login in a database using the following syntax:
sp_addrole [ @rolename = ] 'rolename'
[ , [ @ownername = ] 'ownername' ]
Roles (Polly should use a “role”)
![Page 30: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/30.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 30
• A database is a location in the memory where you can store your data.
• Objects of a database are: Tables
Views
Stored Procedures
Triggers
Databases
![Page 31: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/31.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 31
Views• Views are abstraction of a table, similar to a general view of
an object. In a view only select components need to be retrieved.
![Page 32: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/32.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 32
Facts about Views• Views are nothing but a query block.• Views get the data from underlying base tables.
![Page 33: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/33.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 33
Advantages of Views
• Views helps in applying security policy.• Views provide data independence to users.
![Page 34: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/34.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 34
More about Views• Views are used to implement column and row level
security.• Views can be encrypted to enable enhanced level security
to tables.
![Page 35: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/35.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 35
Activity
• The HR manager asks the DBA to alter the structure of the aggregates view to add department name. Write a query to alter the view.
![Page 36: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/36.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 36
Solution
ALTER VIEW aggregates (deptno, dep_name, average, maximum, minimum, sum, no_sals)
ASselect dep_name, dept_no, avg(sal),Max(sal), Min(sal), count(sal) FROM EMPLOYEE
group by dept_no;
![Page 37: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/37.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 37
Viewing Tablesin Enterprise Manager
Right-Click the table
name, then click
Design Table to
view table columns
and properties.
![Page 38: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/38.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 38
Viewing Table Columns and Properties in Enterprise Manager
Right-click the white
space and then click Properties
to see table constraints.
![Page 39: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/39.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 39
Viewing Table Constraints in Enterprise Manager
![Page 40: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/40.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 40
Creating a Relationship Diagram in Enterprise Manager
Right-click Diagrams, then click
New Database
Diagram to create a
relationshipsdiagram.
![Page 41: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/41.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 41
Table Relationship Diagram
Right-click a
relationship line, then
click Properties to see the properties.
![Page 42: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/42.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 42
WORK – TRANS Relationship Properties
![Page 43: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/43.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 43
Creating Views:By SQL in SQL Query Analyzer
Do NOT put a semi-
colon (;) at the end of a
CREATE VIEW
statement in SQL Query Analyzer!
![Page 44: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/44.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 44
Creating Views:By GUI in Enterprise Manager
![Page 45: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/45.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 45
Indexes
• Database Indexes are conceptually similar to indexes found in books.
![Page 46: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/46.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 46
More about Indexes (Why do we need them?)
• The need for indexes is: To enforce integrity constraints. For faster retrieval of data from tables.
![Page 47: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/47.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 47
Advantages of Indexes
• Index improves the performance of join queries.• Index increases the performance of query retrieval.
![Page 48: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/48.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 48
More about Indexes
• For optimizing a database, every table should have a clustered index.
• Only one clustered index can be created per table.
![Page 49: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/49.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 49
Think about it…
• Is the usage of Indexes appropriate keeping in mind the database optimization factor?
![Page 50: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/50.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 50
Clustered Index
![Page 51: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/51.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 51
NonClustered Index
![Page 52: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/52.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 52
CREATE [UNIQUE] [CLUSTERED | NONCLUSTERED]INDEX index_name ON table (column [,…n])[WITH[PAD_INDEX][[,] FILLFACTOR = fillfactor][[,] IGNORE_DUP_KEY][[,] DROP_EXISTING][[,] STATISTICS_NORECOMPUTE]][ON file group]DROP INDEX USER_DB.STUDENT.DEMO_IDX, USER_DB.GRADE.DEMO_IDX
![Page 53: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/53.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 53
Activity
• The HR manager of Ethnic Blend Inc. asks the DBA to create index on the vEmployeeID column of Employee table. Write a query to create a clustered index on Employee table.
![Page 54: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/54.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 54
Solution
CREATE CLUSTERED INDEX emp_indxON Employee(vEmployeeID);
![Page 55: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/55.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 55
Activity
• The HR manager of Ethnic Blend Inc. asks the DBA to create a non-clustered index on Employee table because Employee table is very frequently updated. Write a query to create a non-clustered index based on vEmployeeID on Employee table.
![Page 56: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/56.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 56
Solution
CREATE NONCLUSTERED INDEX emp_indxon Employee(vEmployeeID);
![Page 57: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/57.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 57
Stored Procedure
• A stored procedure is a manageable group of SQL commands that is executed as a single block. You use the following syntax to create a stored procedure:
CREATE PROCEDURE <procedure name>
<parameter list>
WITH RECOMPILE or ENCRYPTION
AS <programming/SQL code>
GO
![Page 58: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/58.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 58
More about Stored Procedures• The advantages of stored procedures are:
Memory savings
Application integrity
Database
Stored Procedures
![Page 59: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/59.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 59
Stored Procedures (another advantage is reducing Network traffic)
![Page 60: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/60.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 60
Of Course, it IS always possible to get the same result without a Stored Procedure, but you will use a lot more Network Bandwidth and Network Resources!
![Page 61: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/61.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 61
In SQL, stored procedures are classified into three categories. They are:
• System-stored procedures
• Catalog-stored procedures
• Additional stored procedures
![Page 62: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/62.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 62
Some of the predefined system procedures:
System Procedure Used To:sp_addtype create user-defined data type.sp_foreignkeys view the foreign keysp_helpsql get the syntax help from sql server
onlinehelp.
sp_primarykeys return the primary key column of the table
specified.sp_rename rename the user created object.
sp_dropkey drop a key constraint of a table.
sp_droptype delete a user-defined data type.sp_bindrule bind a rule to a column or to a user-
defined data type.sp_unbindrule unbind a rule from a column
or a user- defined data type.sp_help view information about any
database objects.
![Page 63: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/63.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 63
sp_addtype Create user-defined data type.
EXEC sp_addtype SSN, 'VARCHAR(11)', 'NOT NULL'
GO
CREATE TABLE ShowSSNUsage (EmployeeID INT PRIMARY KEY, EmployeeSSN SSN, CONSTRAINT CheckSSN CHECK ( EmployeeSSN LIKE '[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]' ) )
GO
![Page 64: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/64.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 64
More Predefined system procedures for system administration:
System Procedure Used To:
sp_addgroup create groups in the database.
sp_dropgroup drop groups from the database.
sp_addlogin create a login id to connect to the sql server.
sp_adduser create database user account.
sp_droplogin drop a login id from sql server.
sp_lock view details of the lock held on the database objects.
sp_dropuser drop a database user account.
sp_who view the details of the processes and users
running on sql server.
sp_configure view and modify the current settings of sql
server.
sp_defaultdb modify the settings for default database for a login id.
![Page 65: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/65.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 65
Some of the catalog procedures
Catalog Procedures Used to:sp_column_privileges view the column privilege information of a
particular table.
sp_fkeys view the foreign key information for the current database settings, which includes
foreign key references and disabled foreign keys.
sp_columns view the information of a particular column of
a table.
sp_pkeys view the primary key details of a particular table.
sp_statistics view all the indexes available for a particular
table.
sp_databases view all the databases accessible in the sql
server.
sp_tables view the tables that can be accessed in the
current database.
sp_stored_procedures view the stored procedures accessible in the
sql server.
sp_datatype_info view the details of data type available in the
current database.
![Page 66: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/66.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 66
EXEC sp_helpuser
What does this do?
Brief Example of a Catalog Procedure
![Page 67: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/67.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 67
Additional Stored Procedures
Local-stored procedures
Temporary-stored procedures
Extended-stored procedures
Remote-stored procedures
(The main use of remote stored procedures
is to provide backward compatibility)
![Page 68: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/68.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 68
State Whether each Assertion is True or False
Stored procedures are: a. Stored inside tables.
b. Return values.
c. Are Executed automatically during any database operation.
d. Loaded into memory when they are called last.
![Page 69: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/69.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 69
Solutions: True or False
a. TRUE
b. TRUE
c. FALSE
d. FALSE
![Page 70: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/70.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 70
Think about it…
• How are stored procedures processed by the SQL Server?
![Page 71: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/71.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 71
Solution (Think about it… )
• Stored procedures are created in the current database. They are compiled and if no errors are found then are successfully saved in the database server. When a stored procedure is executed, an application call is made to the database server and the stored procedure is loaded into the server memory. Next time when you again use the same stored procedure, instead of an application call, the stored procedure is accessed directly from the memory.
![Page 72: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/72.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 72
Class Activity 2
• Identify the system stored procedures from the following.
a. sp_addtype
b. sp_pkeys
c. sp_columns
d. sp_column_privileges
e. sp_foreignkeys
![Page 73: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/73.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 73
Solution 2
• The system stored procedures are:
sp_addtype
sp_foreignkeys
![Page 74: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/74.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 74
Can you recognize Stored Procedures from their name?… sp_*
• For Example, the functions of the following system stored procedures are:
sp_help: Enable you to view information about any database objects.
sp_defaultdb: Enable you to modify the settings for default database for a login ID.
sp_dropkey: Enable you to drop a key constraint of a table.
sp_helpsql : Enable you get the syntax help from SQL Server online help.
sp_rename: Enable you to rename the user created object.
![Page 75: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/75.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 75
Class Activity
• What is the function of the following command?
CREATE PROCEDURE sales_list_procedureWITH RECOMPILEAS
SELECT * FROM sales
![Page 76: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/76.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 76
Solution
• This procedure displays all the records from the sales table and gets recompiled every time it gets executed.
![Page 77: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/77.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 77
Class Activity • Create a stored procedure using parameters, which inserts
employee id, first name, last name, designation, and salary in the employee table.
EmpID Fname Lname Desg Salary Available Senior Dept_no
0120 Ron Shin Sales Manager
5000 Yes 40
0121 Nancy Jones Team Leader
3000 Yes 0120 30
0122 Don Alien Team Member
2500 No 0121 30
0123 Jane Wane Team Member
2500 Yes 0124 20
0124 Shelley Mathew Team Leader
3000 Yes 0120 20
![Page 78: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/78.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 78
Solution
CREATE PROCEDURE emp_insert @empid number(5), @fname varchar(20),@lname varchar(20), @desg varchar(20),@salary number(10)
ASINSERT employee VALUES
(@empid, @fname, @lname, @desg, @salary)
![Page 79: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/79.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 79
Class Activity • Create a stored procedure using parameters, which lists all
the employees from employee table where the designation is team leader.
EmpID Fname Lname Desg Salary Available
Senior Dept_no
0120 Ron Shin Sales Manager
5000 Yes 40
0121 Nancy Jones Team Leader
3000 Yes 0120 30
0122 Don Alien Team Member
2500 No 0121 30
0123 Jane Wane Team Member
2500 Yes 0124 20
0124 Shelley Mathew Team Leader
3000 Yes 0120 20
![Page 80: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/80.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 80
Solution
CREATE PROCEDURE emp_desg_query@desg char(10) = 'TEAM LEADER'
ASSELECT * FROM employee
WHERE desg = @desgORDER BY @desg
![Page 81: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/81.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 81
Triggers
•Triggers are activated automatically in response to certain actions, such as data manipulation. A trigger is executed whenever the table on which it is created is modified using an INSERT, UPDATE, or DELETE command. • The syntax for creating triggers is:
CREATE TRIGGER <trigger name>
ON <table name> <view name>
FOR <INSERT or UPDATE or DELETE>
AS
< BEGIN {SQL commands} END>
![Page 82: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/82.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 82
More about Triggers
• Some more uses of triggers are:
Trigger can enforce referential integrity.
Triggers can control updates to a table.
Use of trigger minimize the memory usage.
• Triggers can: Be implemented in business rules. Be associated with tables only.
![Page 83: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/83.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 83
Activity
• Create an INSERT trigger on employee table, which does not allow the insertion of a duplicate row in the table.
![Page 84: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/84.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 84
Solution
CREATE TRIGGER emp_insert_triggerON employeeFOR insertASif(select count(*) from employee e, insert I where e.empid=I.pub_id) = 1BEGIN rollback tran raiseerror ('Duplicate employee id',
12,6)END
![Page 85: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/85.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 85
MORE on Triggers
• An SQL Server trigger is a T/SQL procedure that is invoked when a specified database activity occurs
• Triggers can be used to: Enforce business rules Set complex default values Update views Implement referential integrity actions
• SQL Server only supports INSTEAD OF and AFTER triggers: A table may have one or more AFTER triggers AFTER triggers may not be assigned to views A view or table may have only one INSTEAD OF
trigger for each triggering action• Triggers can roll back the transactions that caused them
to be fired
![Page 86: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/86.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 86
This is an AFTER trigger on INSERT on the table TRANS.It is will set a default value on AskingPrice.
![Page 87: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/87.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 87
Triggers:Enforcing a Required Child Constraint
There is an M-M relationship between WORK and TRANSACTION:
![Page 88: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/88.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 88
Triggers:Enforcing a Required Child Constraint
• The hard way using two triggers – this one enforces the required child:
![Page 89: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/89.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 89
Triggers:Enforcing a Required Child Constraint
• The hard way using two triggers – this one deletes any duplicate transaction:
![Page 90: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/90.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 90
Triggers:Enforcing a Required Child Constraint
• A better way using one trigger – this one works with the Work_Trans view:
![Page 91: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/91.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 91
Concurrency Control
• Three factors determine the concurrency control behavior of SQL Server: Transaction isolation level Cursor concurrency setting Locking hints provided in the SELECT clause
• Locking behavior also changes, depending on whether actions occur in the context of transactions or cursors independently Therefore, SQL Server places locks on behalf of the
developer Locks may be placed at many levels of granularity and may
be promoted or demoted as work progresses
![Page 92: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/92.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 92
SQL Server Concurrency Options
![Page 93: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/93.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 93
Summary
• Creating and Managing logins with Transact SQL and Enterprise Manager is a key skill
• There are many key commands and features of Microsoft SQL Database Server and Transact SQL
• Some of these key commands are to create and manage tables, views, indexes, stored procedures and triggers
• Concurrency control ensures that only one user can change a data item at any one time in order to maintain data consistency
![Page 94: IT 390 Business Database Administration](https://reader036.vdocument.in/reader036/viewer/2022081603/56814f19550346895dbcaacd/html5/thumbnails/94.jpg)
© 2006 ITT Educational Services Inc. Course Name: Business Database Administration Unit 4 Slide 94
Did you understand the key points from the Lesson?
Do you have any questions?
Summary