Download presentation
Presentation is loading. Please wait.
Published byἈληκτώ Τρικούπη Modified over 5 years ago
1
The University of Akron College of Applied Science & Technology Dept
The University of Akron College of Applied Science & Technology Dept. of Business & Information Technology 2440: 141 Web Site Administration Database Management Using SQL Instructor: Joseph Nattey
2
Database Concepts A database is a collection of data about 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
3
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
4
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
5
Creating Tables Details required to create database tables include:
Table name 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 Fields names Field data types Introduction to SQL
6
Data Types Data type Sample data Description CHAR(length)
Newcastle Dr. For nonnumeric data. Fixed length VARCHAR(length) For nonnumeric data. Variable length (listed length indicates maximum) INTEGER 123456 For whole number data between –231 and SMALLINT 31 For whole number data between –215 and NUMERIC 2.6E+10 Very large or vary small numbers DATE 11/16/2014 For date. Implementations vary in different databases Introduction to SQL
7
Primary Key An integrity constraint that uses a unique identifier to uniquely identify a record in a table Introduction to SQL
8
Using Common Fields (Foreign Keys) to Link Two Tables
productID productName model Price manufacturerID 100 Washer D1 356.99 1 200 TV S2 255.68 2 manufacturerID name address phone 1 Weiwei Co. Edward Rd 123456 2 XYZ Co. Central 654321 Introduction to SQL
9
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): 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): Used to insert, update, delete, and retrieve data in a table Example: Insert into, update, delete, and select Introduction to SQL
10
Creating and Dropping Tables
CREATE TABLE tableName (field1 dataType, field2 dataType, …) (field1 dataType PRIMARY KEY, field2 dataType, …) DROP TABLE tableName Introduction to SQL
11
Example: Student Table
CREATE TABLE student ( id integer PRIMARY KEY, firstName varchar(15), lastName varchar(15)); Introduction to SQL
12
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
13
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
14
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
15
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
16
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
17
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
18
Retrieving Data… Introduction to SQL CREATE TABLE STATION
(ID INTEGER PRIMARY KEY, CITY CHAR(20), STATE CHAR(2), LAT_N REAL, LONG_W REAL); populate the table STATION with a few rows: INSERT INTO STATION VALUES (13, 'Phoenix', 'AZ', 33, 112); INSERT INTO STATION VALUES (44, 'Denver', 'CO', 40, 105); INSERT INTO STATION VALUES (66, 'Caribou', 'ME', 47, 68); query to look at table STATION in undefined order: SELECT * FROM STATION; ID CITY STATE LAT_N LONG_W 13 Phoenix AZ 44 Denver CO 66 Caribou ME 47 68 query to select Northern stations (Northern latitude > 39.7): SELECT * FROM STATION WHERE LAT_N > 39.7; Introduction to SQL
19
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
20
Wildcard Characters Get all employee details who’s names starts with M. SELECT * FROM DEPT FROM EMPLOYEE WHERE EMPNAME LIKE 'M%‘ Result: Introduction to SQL
21
Wildcard Characters Using the SQL _ Wildcard Example
The following SQL statement selects all customers with a City starting with any character, followed by "erlin": Example SELECT * FROM Customers WHERE City LIKE '_erlin'; Introduction to SQL
22
Sorting Retrieved Data
It is possible to control the order in which rows are returned in a result set by using the ORDER BY clause. The ORDER BY clause specifies one or more columns on which the sort is based. Each column used for sorting can be ordered in ascending or descending order by using the ASC or DESC keywords, respectively. Introduction to SQL
23
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
24
Sorting Retrieved Data
SELECT LASTNAME, FIRSTNME, EMPNO FROM EMPLOYEES WHERE EMPNO > '000200' ORDER BY LASTNAME ASC, FIRSTNME ASC LASTNAME FIRSTNME EMPNO GOUNOT JASON JEFFERSON JAMES JOHNSON SYBIL JONES WILLIAM LEE WING LUTZ JENNIFER MARINO SALVATORE MEHTA RAMLAL PARKER JOHN PEREZ MARIA SCHNEIDER ETHEL SETRIGHT MAUDE SMITH DANIEL SMITH PHILIP record(s) selected. Introduction to SQL
25
Steps to Accessing Databases
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.