Presentation is loading. Please wait.

Presentation is loading. Please wait.

1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 Information Systems Spring 2011.

Similar presentations


Presentation on theme: "1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 Information Systems Spring 2011."— Presentation transcript:

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

2 2ISM - © 2010 Houman Younessi Lecture 3 Step 3 BUILDING A LOGICAL SCHEMA (A set of database tables)

3 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.

4 4ISM - © 2010 Houman Younessi Lecture 3 ONE-TO-ONE Relationship Example: AUTOMOBILE and REGISTRATION VIN_NUMMAKEMODELYEAR REG_NUMADRESSSTATUS VIN_UMMAKEMODELYEARREG_NUMADDRESSSTATUS VIN_NUMMAKEMODELYEARREG_NUM ADRESSSTATUS MAKEMODELYEARVIN_NUM REG_NUMADRESSSTATUS May become or

5 5ISM - © 2010 Houman Younessi Lecture 3 ONE-TO-MANY Relationship Example: EMPLOYEE and DEPARTMENT (one department, many employees, one employee, only one department) EMP_NUMNAMEGRADE DEP_NUMNAMEADRESS EMP_NUMNAMEGRADEDEP_NUM NAMEADRESS becomes

6 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_IDNAMEYEAR COURSE_IDNAMELEVEL NAMEYEARSTUD_IDCOURSE_IDNAMELEVEL STUD_IDCOURSE_ID becomes

7 7ISM - © 2010 Houman Younessi Lecture 3 Step 4 BULIDING AN RDBMS the Standard Query Language

8 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!

9 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

10 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.

11 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

12 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

13 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

14 14ISM - © 2010 Houman Younessi Lecture 3 An SQL SELECT Example The "Persons" table: P_Id LastName FirstName Address City 1 Hansen Ola Timoteivn 10 Sandnes 2 Svendson Tove Borgvn 23 Sandnes 3 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 FirstName Hansen Ola Svendson Tove Pettersen Kari

15 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 City 1 Hansen Ola Timoteivn 10 Sandnes 2 Svendson Tove Borgvn 23 Sandnes 3 Pettersen Kari Storgt 20 Stavanger

16 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 true OR - returns a record if either the first or the second condition is true WHERE - returns a record for which the specified criterion holds ORDER BY - returns the results sorted by a specified column Example: SELECT * FROM Persons WHERE LastName='Svendson' AND (FirstName='Tove' OR FirstName='Ola') ORDER BY City

17 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_name VALUES (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,...)

18 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_name SET 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!

19 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_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name

20 20ISM - © 2010 Houman Younessi Lecture 3 Specific JOIN Example: SELECT emp_num, name, grade FROM Employee INNER JOIN Department ON Employee.dep_num=Department.dep_num

21 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_name WHERE 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!

22 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

23 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

24 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) )

25 25ISM - © 2010 Houman Younessi Lecture 3 SQL ALTER TABLE Syntax To add a column in a table, use the following syntax: ALTER TABLE table_name ADD 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_name DROP COLUMN column_name To change the data type of a column in a table, use the following syntax: ALTER TABLE table_name ALTER COLUMN column_name datatype

26 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

27 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

28 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

29 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

30 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.


Download ppt "1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi 1-860-548-7880 Information Systems Spring 2011."

Similar presentations


Ads by Google