SQL A C P K Siriwardhana MSc, BSc in Computer Science FIRST COURSE
Organizing Data New Perspectives on Microsoft Office 2007: Windows Edition 2
Databases and Relationships A collection of related tables is called a database, or a relational database You connect the records in the separate tables through a common field A primary key is a field, or a collection of fields, whose values uniquely identify each record in a table When you include the primary key from one table as a field in a second table to form a relationship between the two tables, it is called a foreign key in the second table New Perspectives on Microsoft Office 2007: Windows Edition 3
Databases and Relationships New Perspectives on Microsoft Office 2007: Windows Edition 4
Creating a Table in SQL Mode New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 5
Creating a Table in SQL Mode Create table staff ( id number, name char(20), title char(15), h_rate number, manager char(10), primary key (id) ); New Perspectives on Microsoft Office 2007: Windows Edition 6
Execute SQL Click the Design button on the Quick Access Toolbar. Click the RUN button Then Save the query New Perspectives on Microsoft Office 2007: Windows Edition 7
Insert data in to a table using SQL INSERT Insert into staff values(2, "Namal", "Mr", 1000, "Vijethung"); Insert Data Only in Specified Columns INSERT INTO staff (P_Id, LastName, FirstName) VALUES (5, 'Tjessem', 'Jakob') New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 8
Retrieve data from a table using Select SQL Select * from staff; Select id,name,h_rate from staff; Select id, name, h_rate from staff where id =2; Select id, name, h_rate from staff where id =2 or id = 4; New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 9
Retrieve data from a table using SQL Select count(name) from staff; SELECT COUNT(name) FROM staff WHERE name='kamal'; Select id, name, h_rate from staff where id =2 or id = 4; New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 10
SQL DDL and DML DDL CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 11
SQL DML and DDL DML The query and update commands form the DML part of SQL: SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT INTO - inserts new data into a database New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 12
The INSERT INTO Statement Person 3 Pettersen Kari Storgt 20 Stavanger INSERT INTO Persons VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger') Insert Data Only in Specified Columns INSERT INTO Persons (P_Id, LastName, FirstName) VALUES (5, 'Tjessem', 'Jakob') New Perspectives on Microsoft Office 2007: Windows Edition 13
The SQL SELECT DISTINCT Statement SELECT DISTINCT column_name(s) FROM table_name Person 3 Pettersen Kari Storgt 20 Stavanger SELECT DISTINCT City FROM Persons City Sandnes Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 14
The WHERE Clause The SQL WHERE clause is used to specify a condition while fetching the data from single table or joining with multiple tables. If the given condition is satisfied then only it returns specific value from the table. SELECT column_name(s) FROM table_name WHERE column_name operator value SELECT * FROM Persons WHERE City='Sandnes' New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 15
The WHERE Clause SELECT column_name(s) FROM table_name WHERE column_name operator value person 3 Pettersen Kari Storgt 20 Stavanger SELECT * FROM Persons WHERE City='Sandnes' New Perspectives on Microsoft Office 2007: Windows Edition 16
The WHERE Clause (Cont..) Operators Allowed in the WHERE Clause Operator Description = Equal <> Not equal > Greater than < Less than >= Greater than or equal <= Less than or equal BETWEEN Between an inclusive range LIKE Search for a pattern IN If you know the exact value you want to return for at least one of the columns New Perspectives on Microsoft Office 2007: Windows Edition 17
AND & OR Operators AND Operator SELECT * FROM Persons WHERE FirstName='Tove' AND LastName='Svendson' Operartor 1 Svendson Ola Timoteivn 10 Sandnes 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 18
AND & OR Operators OR Operator SELECT * FROM Persons WHERE FirstName='Tove' OR FirstName='Ola' Operartor 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 19
AND & OR Operators Combining AND & OR SELECT * FROM Persons WHERE LastName='Svendson'AND (FirstName='Tove' OR FirstName='Ola') Operartor 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 20
SQL ORDER BY Keyword SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC DESC Person 3 Pettersen Kari Storgt 20 Stavanger 4 Nilsen Tom Vingvn 23 Stavanger SELECT * FROM Persons ORDER BY LastName 4 Nilsen Tom Vingvn 23 Stavanger 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 21
The UPDATE Statement Person 3 Pettersen Kari Storgt 20 Stavanger 4 Nilsen Johan Bakken 2 Stavanger 5 Tjessem Jakob UPDATE Persons SET Address='Nissestien 67', City='Sandnes' WHERE p_id = 5 Person 3 Pettersen Kari Storgt 20 Stavanger 4 Nilsen Johan Bakken 2 Stavanger 5 Tjessem Jakob Nissestien 67 Sandnes New Perspectives on Microsoft Office 2007: Windows Edition 22
SQL DELETE Statement Persons 3 Pettersen Kari Storgt 20 Stavanger 4 Nilsen Johan Bakken 2 Stavanger 5 Tjessem Jakob Nissestien 67 Sandnes DELETE FROM Persons WHERE LastName='Tjessem' AND FirstName='Jakob' Persons 3 Pettersen Kari Storgt 20 Stavanger 4 Nilsen Johan Bakken 2 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 23
SQL ALTER TABLE Statement ADD Column Person ALTER TABLE table_name ADD COLUMN column_name datatype 3 Pettersen Kari Storgt 20 Stavanger ALTER TABLE Person ADD COLUMN DateOfBirth date DateOfBirth 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 24
SQL ALTER TABLE Statement Change Data Type Example ALTER TABLE table_name ALTER column_name datatype ALTER TABLE Persons ALTER COLUMN DateOfBirth year DateOfBirth 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 25
SQL ALTER TABLE Statement DROP Column ALTER TABLE table_name drop column_name DateOfBirth 3 Pettersen Kari Storgt 20 Stavanger ALTER TABLE Persons DROP DateOfBirth Person 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 26
Databases and Relationships Create table customer ( cus_id char(15) primary key, F_name char(20), l_name char(55), phone char(20) ); Create table contract ( con_num number primary key, cus_id char(20) references customer(cus_id), con_amt currency, sig_date date ); New Perspectives on Microsoft Office 2007: Windows Edition 27
Create table customer ( cus_id char(15) primary key, F_name char(20), l_name char(55), phone char(20) ); Create table contract ( con_num number primary key, cus_id char(15) references customer(cus_id), con_amt currency, sig_date date ); New Perspectives on Microsoft Office 2007: Windows Edition 28
Add foreign key - 2 nd method CREATE TABLE Orders ( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, PRIMARY KEY (O_Id), FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) ) New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 29
Add foreign key - 3 nd method CREATE TABLE Orders ( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, PRIMARY KEY (O_Id), CONSTRAINT fk_perorders FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) ) New Perspectives on Microsoft Office 2007: Windows Edition 30
Add foreign key 4 th method ALTER TABLE Orders ADD FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) New Perspectives on Microsoft Office 2007: Windows Edition 31
Add foreign key 5 th method ALTER TABLE Orders ADD CONSTRAINT fk_perorders FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) New Perspectives on Microsoft Office 2007: Windows Edition 32
Drop Foreign key ALTER TABLE Orders DROP FOREIGN KEY fk_perorders New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 33
Drop Foreign key ALTER TABLE Orders DROP CONSTRAINT fk_perorders New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 34
Person 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 35
Working with Where Customer : cus_id, f_name, l_name, phone Contract : con_id, cus_id, con_amt, sig_date Select Customer.cus_id, Customer.f_name, Customer.l_name, Contract.con_amt, Contract.sig_date from Customer,Contract where Customer.cus_id = Contract.cus_id; New Perspectives on Microsoft Office 2007: Windows Edition 36