* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Database Management using SQL - gozips.uakron.edu
Survey
Document related concepts
Oracle Database wikipedia , lookup
Microsoft Access wikipedia , lookup
Relational algebra wikipedia , lookup
Functional Database Model wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Ingres (database) wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Clusterpoint wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Transcript
2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson Database Concepts A database is a collection of data about an entities For example, a database might contain checking account information in a bank, product items information in an on-line store, students’ records in a college, and so on Relational databases store information in simple structures called tables Most commercial databases today are relational databases Introduction to SQL 2 Database Concepts An entity is a distinct object, for example, a person's name, a product, or an event, to be presented in the table The columns in the table contain fields A field contains a single, specific piece of information within a record. So, a record is a group of related fields The data in a table is organized into rows and columns The rows in the table are called records A record contains information about a given entity Introduction to SQL 3 Sample Item Database Table Item Number Item Name Unit Price Inventory 100 Camera $267.99 13 101 Washer $489.56 8 102 TV $189.99 29 Introduction to SQL 4 Creating Tables Details required to create database tables include: Table name 1. The table name identifies the table A database can consist of many tables You use the name of a table to reference the table and to manipulate data in that table 2. Fields names 3. Field data types Introduction to SQL 5 Data Types Data type Sample data Description CHAR(length) Newcastle Dr. For nonnumeric data. Fixed length VARCHAR(length) Newcastle Dr. INTEGER 123456 For nonnumeric data. Variable length (listed length indicates maximum) For whole number data between –231 and +231-1 SMALLINT 31 For whole number data between –215 and +215-1 NUMERIC 2.6E+10 DATE 11/16/2001 Very large or vary small numbers For date. Implementations vary in different databases Introduction to SQL 6 Primary Key An integrity constraint that uses a unique identifier to uniquely identify a record in a table Introduction to SQL 7 Using Common Fields (Foreign Keys) to Link Two Tables productI D 100 200 productNa me Washer TV mode l D1 S2 manufacturerID name 1 2 Price 356.9 9 255.6 8 manufacture rID 1 2 phone Weiwei Co. addres s Edward Rd XYZ Co. Central 654321 Introduction to SQL 123456 8 An Introduction to SQL The Structured Query Language (SQL) is a language embedded in relational DBMSs to process relational databases SQL can be used as a: Data definition language (DDL): 1. Used to define a table’s column, add or delete columns, and delete unneeded tables Example: Create table, alter table, drop table, etc Data manipulation language(DML): 2. Used to insert, update, delete, and retrieve data in a table Example: Insert into, update, delete, and select Introduction to SQL 9 Creating and Dropping Tables CREATE TABLE tableName (field1 dataType, field2 dataType, …) CREATE TABLE tableName (field1 dataType PRIMARY KEY, field2 dataType, …) DROP TABLE tableName Introduction to SQL 10 Example: Student Table CREATE TABLE student ( id integer PRIMARY KEY, firstName varchar(15), lastName varchar(15)); Introduction to SQL 11 Inserting Data Into a Table INSERT INTO TABLE tableName VALUES (value1, value2, …) or INSERT INTO TABLE tableName (field1, field2, …) VALUES ( value1, value2, …) Introduction to SQL 12 Updating Table Data UPDATE tableName SET field1=value1, fiedl2=value2, … WHERE conditions The WHERE clause gives the condition for selecting which rows (records) are to be updated in the table identified as tableName The SET keyword is followed by the field list to be updated If the WHERE clause is omitted, all rows in the table are updated Introduction to SQL 13 Updating Table Data Conditions in a WHERE clause are similar to conditional statements in JSP Conditions can be constructed with comparison operators and logical operators You can use the six comparison operators ( =, <> for not equal, <, >, <=, >=) as well as the three logical operators (AND, OR, and NOT) to create compound conditions or to negate a condition Introduction to SQL 14 Deleting Records from a Table DELETE FROM tableName WHERE conditions This deletes all rows that satisfy the WHERE clause in the statement If there is no WHERE clause, then all rows in the table are deleted Introduction to SQL 15 Retrieving Data SELECT field1, field2, … FROM tableName WHERE conditions The SELECT clause lists the fields retrieved in the query result, separated by commas The FROM clause lists one or more table names to be used by the query All fields listed in the SELECT or WHERE clauses must be found in one and only one of the tables listed in the FROM clause Introduction to SQL 16 Retrieving Data… The WHERE clause contains conditions for selecting rows from the tables listed in the FROM clause The data retrieved are from the rows that satisfy the condition (and are therefore selected) If the WHERE clause is omitted, all rows are selected Introduction to SQL 17 Wildcard Characters Special symbols that represent any character or combination of characters Make it easier to use inexact spelling in a query The percent symbol % represents any collection of characters, including zero characters The underscore _ represents any individual character Introduction to SQL 18 Sorting Retrieved Data SELECT field1, field2, … FROM tableName WHERE conditions ORDER BY sort_field1 DESC, sort_field2, … Sort data retrieved in a particular order Introduction to SQL 19 Steps to Accessing Databases 1. 2. 3. 4. 5. 6. 7. Load the database driver Define the connection URL Establish the connection Create the statement object Execute a query or update Process the results Close the connection Introduction to SQL 20 Loading a DBMS Driver The DBMS driver acts as the bridge between the server-side programming environment and the database itself The driver is a piece of software that knows how to talk to the DBMS To load a driver, all you need to do is to load the appropriate class Introduction to SQL 21 Client-side Scripts and Browser Dependency Client-side scripts are executed in the client browser The browser must provide an appropriate interpreter All browsers come with built-in engines to support certain client-side scripting languages Introduction to SQL 22