+ All Categories
Home > Documents > 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 [email protected]...

1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 [email protected]...

Date post: 22-Dec-2015
Category:
View: 221 times
Download: 1 times
Share this document with a friend
Popular Tags:
30
1 ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 [email protected] Information Systems Spring 2011
Transcript
Page 1: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

1ISM - © 2010 Houman Younessi

Lecture 3

Convener:

Houman Younessi

1-860-548-7880

[email protected]

Information SystemsSpring 2011

Page 2: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

2ISM - © 2010 Houman Younessi

Lecture 3

Step 3

BUILDING A LOGICAL SCHEMA(A set of database tables)

Page 3: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

3ISM - © 2010 Houman Younessi

Lecture 3

Rules to extract tables:

1.Tables in a ONE-TO-ONE relationship translate to one or two tables (two is better). The PRIMARY –KEY of either can become the FOREIGN-KEY of both.2.Tables in a ONE-TO-MANY relationship become two tables. The key on the ONE side becomes the FOREIGN-KEY of the table on the MANY side.3.Tables in a MANY-TO-MANY relationship translate to two tables. A third table is formed of the PRIMARY-KEYs of both.

Page 4: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

4ISM - © 2010 Houman Younessi

Lecture 3

ONE-TO-ONE Relationship

Example: AUTOMOBILE and REGISTRATION

VIN_NUM MAKE MODEL YEAR REG_NUM ADRESS STATUS

VIN_UM MAKE MODEL YEAR REG_NUM ADDRESS STATUS

VIN_NUM MAKE MODEL YEAR REG_NUM

REG_NUM ADRESS STATUS

MAKE MODEL YEAR VIN_NUM

VIN_NUM REG_NUM ADRESS STATUS

May become

or

or

Page 5: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

5ISM - © 2010 Houman Younessi

Lecture 3

ONE-TO-MANY Relationship

Example: EMPLOYEE and DEPARTMENT (one department, many employees, one employee, only one department)

EMP_NUM NAME GRADE DEP_NUM NAME ADRESS

EMP_NUM NAME GRADE DEP_NUM

DEP_NUM NAME ADRESS

becomes

Page 6: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

6ISM - © 2010 Houman Younessi

Lecture 3

MANY-TO-MANY Relationship

Example: STUDENT and COURSE (many students in one course, many courses, per one student)

STUD_ID NAME YEAR COURSE_ID NAME LEVEL

NAME YEAR STUD_ID COURSE_ID NAME LEVEL

STUD_ID COURSE_ID

becomes

Page 7: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

7ISM - © 2010 Houman Younessi

Lecture 3

Step 4

BULIDING AN RDBMS

the Standard Query Language

Page 8: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

8ISM - © 2010 Houman Younessi

Lecture 3

What is SQL?

* SQL stands for Structured Query Language * SQL is a standard language for accessing and manipulating databases * SQL is an ANSI (American National Standards Institute) standard

SQL is a Standard - BUT....

Although SQL is an ANSI (American National Standards Institute) standard, there are many different versions of the SQL language.

However, to be compliant with the ANSI standard, they all support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.

Most of the SQL database programs also have their own proprietary extensions in addition to the SQL standard!

Page 9: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

9ISM - © 2010 Houman Younessi

Lecture 3

What Can SQL do?

* SQL can create new databases * SQL can create new tables in a database * SQL can create stored procedures in a database * SQL can create views in a database * SQL can execute queries against a database * SQL can retrieve data from a database * SQL can insert records in a database * SQL can update records in a database * SQL can delete records from a database * SQL can set permissions on tables, procedures, and views

Data Definition Language DDL

Data Manipulation Language DML

Page 10: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

10ISM - © 2010 Houman Younessi

Lecture 3

SQL Statements

Most of the actions you need to perform on a database are done with SQL statements. Example:

SELECT * FROM Persons

SQL is not case sensitive

Semicolon after SQL Statements?

Some database systems require a semicolon at the end of each SQL statement.

Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.

We are using MS Access and SQL Server 2000 and we do not have to put a semicolon after each SQL statement, but some database programs force you to use it.

Page 11: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

11ISM - © 2010 Houman Younessi

Lecture 3

The most important DDL statements in SQL are:

* 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 * DROP DATABASE – deletes database * CREATE INDEX - creates an index (search key) * DROP INDEX - deletes an index

Page 12: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

12ISM - © 2010 Houman Younessi

Lecture 3

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 * JOIN - joins two (or more) tables

Page 13: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

13ISM - © 2010 Houman Younessi

Lecture 3

The SQL SELECT Statement

The SELECT statement is used to select data from a database.

The result is stored in a result table, called the result-set.

SQL SELECT Syntax:

SELECT column_name(s)FROM table_name

and

SELECT * FROM table_name

Page 14: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

14ISM - © 2010 Houman Younessi

Lecture 3

An SQL SELECT Example

The "Persons" table:

P_Id LastName FirstName Address City1 Hansen Ola Timoteivn 10 Sandnes2 Svendson Tove Borgvn 23 Sandnes3 Pettersen Kari Storgt 20 Stavanger

Now we want to select the content of the columns named "LastName" and "FirstName" from the table above.

We use the following SELECT statement:

SELECT LastName, FirstName FROM Persons

The result-set will look like this:

LastName FirstNameHansen OlaSvendson TovePettersen Kari

Page 15: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

15ISM - © 2010 Houman Younessi

Lecture 3

SELECT * Example

Now we want to select all the columns from the "Persons" table.

We use the following SELECT statement:

SELECT * FROM Persons

Tip: The asterisk (*) is a quick way of selecting all columns!

The result-set will look like this:

P_Id LastName FirstName Address City1 Hansen Ola Timoteivn 10 Sandnes2 Svendson Tove Borgvn 23 Sandnes3 Pettersen Kari Storgt 20 Stavanger

Page 16: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

16ISM - © 2010 Houman Younessi

Lecture 3

SQL Modifiers

DISTINCT - returns only distinct values (no duplicates) AND - returns a record if both the first and the second condition is trueOR - returns a record if either the first or the second condition is trueWHERE - returns a record for which the specified criterion holdsORDER BY - returns the results sorted by a specified column

Example:

SELECT * FROM Persons WHERELastName='Svendson'AND (FirstName='Tove' OR FirstName='Ola')ORDER BY City

Page 17: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

17ISM - © 2010 Houman Younessi

Lecture 3

SQL INSERT INTO Syntax

It is possible to write the INSERT INTO statement in two forms.

The first form doesn't specify the column names where the data will be inserted, only their values:

INSERT INTO table_nameVALUES (value1, value2, value3,...)

The second form specifies both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3,...)VALUES (value1, value2, value3,...)

Page 18: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

18ISM - © 2010 Houman Younessi

Lecture 3

The UPDATE Statement

The UPDATE statement is used to update existing records in a table.

SQL UPDATE Syntax

UPDATE table_nameSET column1=value, column2=value2,...WHERE some_column=some_value

Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

Page 19: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

19ISM - © 2010 Houman Younessi

Lecture 3

The SQL JOIN Statement

* INNER JOIN: Return rows when there is at least one match in both tables * LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table * RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table * FULL JOIN: Return rows when there is a match in one of the tables

Syntax Example:

SELECT column_name(s)FROM table_name1INNER JOIN table_name2ON table_name1.column_name=table_name2.column_name

Page 20: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

20ISM - © 2010 Houman Younessi

Lecture 3

Specific JOIN Example:

SELECT emp_num, name, gradeFROM EmployeeINNER JOIN DepartmentON Employee.dep_num=Department.dep_num

Page 21: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

21ISM - © 2010 Houman Younessi

Lecture 3

The DELETE Statement

The DELETE statement is used to delete rows in a table.

SQL DELETE Syntax

DELETE FROM table_nameWHERE some_column=some_value

Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

Page 22: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

22ISM - © 2010 Houman Younessi

Lecture 3

The most dangerous command in SQL

It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes, and indexes will be intact:

DELETE FROM table_name

or

DELETE * FROM table_name

Be very careful when deleting records. You cannot undo this statement!

Note the annoying but at times life saving FROM

Page 23: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

23ISM - © 2010 Houman Younessi

Lecture 3

The Data Definition Language

The CREATE DATABASE Statement

The CREATE DATABASE statement is used to create a database.

SQL CREATE DATABASE Syntax

CREATE DATABASE database_name

Example:

CREATE DATABASE my_db

Page 24: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

24ISM - © 2010 Houman Younessi

Lecture 3

The CREATE TABLE Statement

The CREATE TABLE statement is used to create a table in a database.

SQL CREATE TABLE Syntax

CREATE TABLE table_name(column_name1 data_type,column_name2 data_type,column_name3 data_type,....)

Example:

CREATE TABLE Persons(P_Id int,LastName varchar(255),FirstName varchar(255),Address varchar(255),City varchar(255))

Page 25: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

25ISM - © 2010 Houman Younessi

Lecture 3

SQL ALTER TABLE Syntax

To add a column in a table, use the following syntax:

ALTER TABLE table_nameADD column_name datatype

To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column):

ALTER TABLE table_nameDROP COLUMN column_name

To change the data type of a column in a table, use the following syntax:

ALTER TABLE table_nameALTER COLUMN column_name datatype

Page 26: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

26ISM - © 2010 Houman Younessi

Lecture 3

The DROP TABLE Statement

The DROP TABLE statement is used to delete a table.

DROP TABLE table_name

The DROP DATABASE Statement

The DROP DATABASE statement is used to delete a database.

DROP DATABASE database_name

Page 27: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

27ISM - © 2010 Houman Younessi

Lecture 3

SQL Functions

SQL has many built-in functions for performing calculations on data.

SQL Aggregate Functions

SQL aggregate functions return a single value, calculated from values in a column.

Useful aggregate functions:

* AVG() - Returns the average value * COUNT() - Returns the number of rows * FIRST() - Returns the first value * LAST() - Returns the last value * MAX() - Returns the largest value * MIN() - Returns the smallest value * SUM() - Returns the sum

Page 28: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

28ISM - © 2010 Houman Younessi

Lecture 3

SQL Scalar functions

SQL scalar functions return a single value, based on the input value.

Useful scalar functions:

* UCASE() - Converts a field to upper case * LCASE() - Converts a field to lower case * MID() - Extract characters from a text field * LEN() - Returns the length of a text field * ROUND() - Rounds a numeric field to the number of decimals specified * NOW() - Returns the current system date and time * FORMAT() - Formats how a field is to be displayed

Page 29: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

29ISM - © 2010 Houman Younessi

Lecture 3

Examples of SQL Functions

SELECT AVG(column_name) FROM table_name

SELECT COUNT(column_name) FROM table_name

SELECT SUM(column_name) FROM table_name

SELECT MID(column_name,start[,length]) FROM table_name

SELECT ROUND(column_name,decimals) FROM table_name

SELECT NOW() FROM table_name

Page 30: 1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 youneh@rpi.edu Information Systems Spring 2011.

30ISM - © 2010 Houman Younessi

Lecture 3

SQL Hosting

If you want your web site to be able to store and display data from a database, your web server should have access to a database system that uses the SQL language.

If your web server will be hosted by an Internet Service Provider (ISP), you will have to look for SQL hosting plans.

The most common SQL hosting databases are Oracle, MySQL, MS SQL Server, and MS Access.

You can have SQL databases on both Windows and Linux/UNIX operating systems.

Oracle runs on most operating systems, very robust, appropriate for very high-throughput applications.

MS SQL runs only on Windows OS.

MySQL runs on both Windows and Linux/UNIX operating systems.

MS Access (recommended only for small websites) runs only on Windows OS.


Recommended