Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

SQL - Weishan Wang

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

SQL

 This presentation will cover:

A Brief History of DBMS


Viewin database
MySQL installation
SQL – DBMS History
 DBMS History
 The IBM’s System/R was not the first DBMS. The fir
st to market was Relational Software's product name
d Oracle
 The second was Relational Technology's Ingres.
 IBM then released improved products in 1982 name
d SQL/DS and DB2. Oracle and DB2 in nth generati
on forms while the Ingres technology was bought by
Computer Associates.
SQL - Standards
 SQL is a open language without corporate ownership.
 The ANSI-SQL (American National Standards Institute)
group has published three standards over the years:
SQL89 (SQL1)
SQL92 (SQL2)
SQL99 (SQL3)
 The majority of the language has not changed through
these updates.
 The SQL standard from ANSI is considered the "pure"
SQL and called ANSI-SQL.
SQL – Enhanced features
 Every DBMS vendor wants their products to be
different. So most products offers extra features, these
additions are generally not compatible with competitor's
SQL products.
 It is always safest to stick with pure SQL
 The enhancements are not all bad because these
extensions are very useful.
 For example, most DBMS sold today have an
automatic way to assign a serial number feature
since serial numbering is so common. However, the
method of implementation is not uniform.
What is a View?
 In SQL, a VIEW is a virtual relation based on the res
ult-set of a SELECT statement.
 A view contains rows and columns, just like a real tab
le. The fields in a view are fields from one or more re
al tables in the database. In some cases, we can mo
dify a view and present the data as if the data were c
oming from a single table.
 Syntax:
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
SQL – Relations, Tables & Views
 When we say Relation, it could be a Table or a View.
There are three kind of relations:

1. Stored relations tables


We sometimes use the term “base relation” or “base
table”
2. Virtual relations views
3. Temporary results
SQL – Create View
 Example: Create a view with title and year and made by
Paramount studio.

Movie (title, year, length, inColor, studioName,


producerC#)

CREATE VIEW ParamountMovie AS


SELECT title,year
FROM Movie
WHERE studioName = ‘Paramount’;
SQL – Querying View
 A view could be used from inside a query, a stored proc
edure, or from inside another view. By adding functions,
joins, etc., to a view, it allows us to present exactly the
data we want to the user.
View
SELECT title
FROM ParamountMovie
WHERE year = ‘1979’;
 Have same result as
SELECT title Table
FROM Movie
WHERE studioName = ‘Paramount’ AND year = ‘1
979’;
SQL - Querying View con’t
 Query involving both view and table

SELECT DISTINCT starName View

FROM ParamountMovie, StarsIn Table

WHERE title = movieTitle AND year = movieY


ear;
SQL - Querying View example
Movie (title, year, length, inColor, studioName, producerC#)
MovieExec (name, address, cert#, netWorth)

CREATE VIEW MovieProd AS


SELECT title, name
FROM Movie, MovieExec
WHERE producerC# = cert#;
SELECT name
FROM MovieProd
WHERE title = ‘Gone With the Wind’;
 Same result as query from tables
SELECT name
FROM Movie, MovieExec
WHERE producerC# = cert# AND title = ‘The War Of the World’;
SQL - Renaming Attributes in View
 Sometime, we might want to distinguish at
tributes by giving the different name.

CREATE VIEW MovieProd (movieTitle, prodName) AS


SELECT title, name
FROM Movie, MovieExec
WHERE producerC# = cert#;
SQL - Modifying View
When we modify a view, we actually modify a table thr
ough a view. Many views are not updateable. Here ar
e rules have been defined in SQL for updateable view
s:
 selecting (SELECT not SELECT DISTINCT) some attr
ibutes from one relation R (which may itself be an upd
ateable view)
 The WHERE clause must not involve R in a subque
ry.
 The list in the SELECT clause must include enough
attributes that will allow us to insert tuples into the v
iew as well as table. All other attributes will be filled
out with NULL or the proper default values.
SQL – Modifying View (INSERT)
INSERT INTO ParamountMovie
VALUES (‘Star Trek’, 1979);
To make the view ParamountMovie updateable, we need to add a
ttribute studioName to it’s SELECT clause because it makes
more sense if the studioName is Paramount instead of NULL.

CREATE VIEW ParamountMovie AS


SELECT studioName, title, year
FROM Movie
WHERE studioName = ‘Paramount’;
Then
INSERT INTO ParamountMovie
VALUES (‘Paramount’, ‘Star Trek’, 1979);

Title year length inColor studioName producerC#


‘Star Trek’ 1979 0 NULL ‘Paramount’ NULL
SQL - Modifying View (DELETE)
 Suppose we wish to delete all movies with “Tre
k” in their title from the updateable view Paramo
untMovie.

DELETE FROM ParamountMovie


WHERE title LIKE ‘%Trek%’;

It is turned into the base table delete

DELETE FROM Movie


WHERE title LIKE ‘%Trek%’ AND studioName = ‘Para
mount’;
SQL - Modifying View (UPDATE)
 UPDATE from an updateable view
UPDATE ParamountMovie
SET year = 1979
WHERE title = ‘Star Trek the Movie’;

It is turned into the base table update


UPDATE Movie
SET year = 1979
WHERE title = ‘Star Trek the Movie’ AND studioName
= ‘Paramount’;
SQL – View (DROP)
 DROP view: All views can be dropped, whether or not t
he view is updateable.

DROP VIEW ParamountMovie;

 DROP VIEW does not affect any tuples of the underlyin


g relation (table) Movie.
 However, DROP TABLE will delete the table and also
make the view ParamountMovie unusable.

DROP TABLE Movie


SQL - Download MySQL
 Go to http://dev.mysql.com/downloads/ and do
wnload:
 MySQL (Windows User / version 4.1.10a, 5.0.2-
alpha has bug that keep shutting down the serv
ice)
 MySQL Administrator
 MySQL Query Browser
SQL – Install MySQL
 During the installation –
 you can <“Skip Sing-Up”> to fast installation
 Will run Configuration Wizard right after installation automaticall
y
 If the service won’t start, press <cancel>, then run Configuratio
n Wizard manually again
 Run MySQL Server Instance Config Wizard from windo
ws menu –
 use default setting unless you know what you are doing
 Modify Security Setting (option: you can start service without d
oing this)
 Execute
 If there is an error, try press <back> to go back, then press <next>
to <execute> again
 TCP / IP Networking –
 Try different port number if you are using networking.
SQL - MySQL Administrator
 MySQL Administrator come with MySQL Syste
m Tray Monitor allow you to configure your serv
er
 Run MySQL Administrator –
 For the first time, you may not have “Stored c

onnection,” you can <Add new Connection>


and give the Connection name.
 Server Host: type “localhost” If you don’t use

networking
SQL - MySQL Query Browser
 Run MySQL Query Browser
 Give a name for the Default Schema
 Schema means Database instance
 You can type command line into the top box
or right Click on schemata to create new tabl
e
SQL - Bibliography
 First Course In Database Systems, Jeffery D. U
llman and Jennifer Widom,1997 Prentice Hall, I
nc.
 http://mysql.com

You might also like