Databases in Web Devolvement
By Andy Larson
Using Microsoft Sever 2000 with ASP• Database Integrity• Inserting data• Stored procedures• Views• Triggers• Tips and ticks
Integrity
• Steadfast adherence to a strict moral or ethical code.
• The state of being unimpaired; soundness.
• The quality or condition of being whole or undivided; completeness.
Source: The American Heritage. Dictionary of the English Language, Fourth Edition Copyright 2000 by Houghton Mifflin Company.
Database Integrityor
Referential Integrity
Is a feature provided by Relational Database Management Systems (RDBMS) that prevents users or applications from entering inconsistent data. Most RDBMS's have various referential integrity rules that you can apply when you create a relationship between two tables.
Types
• 1st Normal Form (1NF)– All the key attributes are defined– There is no repeating data.– All attributes are dependent on the primary key
• 2NF– It is in 1NF– It does not include partial dependencies
• 3NF– It is in 2NF– It does not contain transitive dependencies
• There is also Boyce Codd NF, 4NF, 5NF.
• These normal forms help eliminate data anomalies. – Update - Having to update the same data in
more than one place.– Insert - Cannot insert due to no primary key.– Delete - When information is deleted it may
delete more than it should.
Inserting Data
• To put data into a database you have to insert it.
• This is done by using the insert, or update commands.
Inserting
insert into pet
values('Spot','Cat','4','');
Pet table
PetID, PetName, PetType, OwenerId, Updated
Updating
update Pet
set petName = 'Katie'
where OwnerId = 1;
Stored Procedures
• Is a query that is stored on the server.
• It can be called from an ASP page.
• Values can be passed into it and you can get values from it.
• One query can be used for many purposes depending on what values are passed into the procedure.
Advantages
• Stored procedures are modular.
• Stored procedures abstract or separate server-side functions from the client-side.
• Stored procedures are usually written by database developers or administrators.
Creating a Stored ProcedureCREATE PROCEDURE PETPROC1
@FName varchar(50),
@LName varchar(50)
AS
SELECT Pet.PetName, PetOwner.First_Name, PetOwner.Last_Name
FROM PetOwner, Pet
WHERE PetOwner.First_Name = @FName
AND PetOwner.Last_Name = @LName
AND Pet.OwnerId = PetOwner.OwnerId
Views
A view is a logical (virtual) table that exists only in memory, yet can be viewed as a real table.
• A view can be attributes from many tables that are placed into a new logical table.
• It is very helpful if you have to access the same data from different tables.
Creating a View
CREATE VIEW Owner_Pet_NamesASSELECT PetOwner.OwnerId,
PetOwner.First_Name, PetOwner.Last_Name, Pet.PetName
FROM Pet, PetOwner Where Pet.OwnerId = PetOwner.OwnerId
Triggers
• The advantage of a trigger is that it invokes automatically whenever the command, for which it is defined, is issued.
• A trigger is a kind of stored procedure that executes automatically when you modify a table by using insert, update, or delete.
Types of triggers
• After triggers– Insert– Update– Delete
• Instead of trigger– Insert– Delete– Update
Creating a Trigger
create trigger PetTrigger
on Pet
after update
as
update Pet
set Updated = getdate()
where OwnerId = 1;
Tips and Tricks
• Make sure you set up database integrity.
• Keep attribute names consistent.
• Create your connection string in a separate ASP file and then import it.
• Make sure you try your SQL statements before you put them into your asp code.
The End
Any Questions?