Thursday, February 25, 2010

SQL 2

today's SQL
--this is an inner join
--list all the columns regardless of table
--list one of tables in the From clause
--use the inner join keyword to add another
--table
--use the on keyword to show how the tables join

Select LastName, Firstname,
DonationDate, DonationAmount
From Person p
Inner Join Donation d
ON p.PersonKey=d.PersonKey
Order by Lastname

--matches every record in one table
--with every record in the second table
Select LastName, Firstname,
DonationDate, DonationAmount
From Person p
Cross Join Donation d

--equi join
Select LastName, Firstname,
DonationDate, DonationAmount
From Person p, donation d
Where p.PersonKey=d.PersonKey

Select Firstname, LastName, ContactInfo
From Person p
Inner Join PersonContact pc
on p.PersonKey=pc.PersonKey
Where ContactTypeKey=6

Insert into Person (LastName, Firstname)
Values ('Depp', 'Johnny')

Insert into Donation (DonationDate, DonationAmount, PersonKey, EmployeeKey)
Values('2/25/2010',5,52,1)

Select lastName, firstname, DonationDate, DonationAmount
From Person p
inner join Donation d
on p.Personkey=d.personkey
Where lastname='Depp'

Update Person
Set LastName='Jackson',
Firstname='Janet'
Where PersonKey=1

Select * from Person

Begin tran

Update Person
Set LastName='Jackson'

Commit Tran

Delete from Person
Where PersonKey=52

Rollback tran

Begin tran

Delete from Donation

Select * from Donation

Rollback tran

No comments:

Post a Comment