Beruflich Dokumente
Kultur Dokumente
4
Data Manipulation Commands
5
Data Definition Commands
• Examine the simple database model
(Figure 6.1) and the database tables that
will form the basis for the many SQL
examples
6
The Database Model
7
Creating the Database
• Two tasks must be completed
– create the database structure
– create the tables that will hold the end-user
data
• First task
– RDBMS creates the physical files that will
hold the database
– Tends to differ substantially from one
RDBMS to another
8
Creating Database Structures
• Example:
– CREATE DATABASE SAMPLE1;
9
Data Types
• Data type selection is usually dictated by
the nature of the data and by the intended
use
10
Some Common SQL Data Types
Data Type Format Description
Numeric INTEGER -2 147 483 648
+2 147 483 647
SMALLINT -32 768 32 767
DECIMAL DECIMAL(5,2) 123.45
(L,D)
Characte CHAR(L) CHAR(5)
r If ‘Ali’, database keeps ‘Ali__’ 2
space wasted.
VARCHAR( VARCHAR(5)
L) If ‘Ali’, database keeps 3 space
only.
Date DATE Year 0001 – 9999
Month 01 –12 11
Day 01 - 31
Some Common SQL Data Types
12
Creating Table Structures
• Example:
CREATE TABLE VENDOR
(v_code varchar(5) not null primary key,
v_name varchar(20),
v_contact varchar(15),
v_areacode integer,
v_phone varchar(10),
v_state char(2),
v_order Char(1));
16
Creating Table Structures
• Example:
CREATE TABLE PRODUCT(
(p_code varchar(10) not null primary key,
p_descript varchar(50),
p_indate date,
p_onhand integer,
p_min integer,
p_price decimal(5,2),
p_discount decimal (3,2),
v_code varchar(5),
foreign key (v_code) references vendor
on delete restrict);
17
Data Manipulation Commands
• Adding table rows
• Saving table changes
• Listing table rows
• Updating table rows
• Restoring table contents
• Deleting table rows
• Inserting table rows with a select
subquery 18
Common SQL Data
Manipulation Commands
19
A Data View and Entry Form
20
Inserting Table Rows
• Data Entry
INSERT INTO <table name> VALUES (attribute 1 value, attribute 2
value, … etc.);
• Examples:
– INSERT INTO VENDOR
VALUES(‘21225, ’Bryson, Inc.’, ’Smithson’, ’615’, ’223-3234’, ’TN’,
’Y’);
21
Saving Table Changes
• Changes made to table contents are not
physically saved on disk until
– Database is closed
– Program is closed
– COMMIT command is used
• Syntax
– COMMIT [table names]
– Example: COMMIT PRODUCT;
• Will permanently save any changes made to
any table in the database. 22
Listing Table Rows
• SELECT
– Used to list contents of table
• Syntax
– SELECT columnlist
FROM tablename
• Columnlist represents one or more attributes,
separated by commas
• Asterisk can be used as wildcard character to
list all attributes
23
Listing Table Rows
• Examples:
SELECT * FROM PRODUCT;
24
Updating Table Rows
• UPDATE
– Modify data in a table
• Syntax
– UPDATE tablename
SET columnname = expression [, columname = expression]
[WHERE conditionlist];
– Examples:
UPDATE PRODUCT SET P_INDATE = ‘12/11/96’
WHERE P_CODE = ‘13-Q2/P2’;
• ROLLBACK
– Used restore the database to its previous
condition
– Only applicable if COMMIT command has not
been used to permanently store the changes in
the database
• Syntax
– ROLLBACK;
• COMMIT and ROLLBACK only work with data
manipulation commands that are used to add,
modify, or delete table rows 26
Deleting Table Rows
• DELETE
– Deletes a table row
• Syntax
– DELETE FROM tablename
[WHERE conditionlist ];
– Examples:
• DELETE FROM PRODUCT WHERE P_CODE = ‘2238/QPD’;
• DELETE FROM PRODUCT WHERE P_MIN = 5;
30
Comparison Operators
31
Selected PRODUCT Table
Attributes for VENDOR Codes
Other than 21344
32
Selected PRODUCT Table
Attributes
with a P_PRICE Restriction
33
Selected PRODUCT Table
Attributes:
The ASCII Code Effect
34
Selected PRODUCT Table
Attributes: Date Restriction
35
SELECT Statement
with a Computed Column
36
SELECT Statement with a
Computed Column and an Alias
37
Arithmetic Operators:
The Rule of Precedence
Arithmetic Operator Description
+ Add
- Substract
* Multiply
/ Divide
^ Raise to the power of
(Some applications use **
instead of ^.
Table 6.9 The Arithmetic Operators
38
Arithmetic Operators:
The Rule of Precedence
• Perform operations within parentheses
39
Selected PRODUCT Table
Attributes:
The Logical OR
SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE
FROM PRODUCT
WHERE V_CODE = 21344 OR V_CODE = 24288;
40
Selected PRODUCT Table
Attributes:
The Logical AND
SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE
FROM PRODUCT
WHERE P_PRICE < 50
AND P_INDATE > ’15-Jan-2004’;
41
Selected PRODUCT Table
Attributes:
The Logical AND and OR
SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE
FROM PRODUCT
WHERE (P_PRICE < 50 AND P_DATE > ’15-Jan-2004’)
OR V_CODE = 24288;
42
Special Operators
• BETWEEN
– Used to check whether attribute value is within
a range
• IS NULL
– Used to check whether attribute value is null
• LIKE
– Used to check whether attribute value
matches a given string pattern
• IN
– Used to check whether attribute value
matches any value within a value list
• EXISTS
– Used to check if a subquery returns any rows 43
Special Operators
• BETWEEN is used to define range limits.
• Examples:
SELECT *
FROM PRODUCT
WHERE P_PRICE BETWEEN 50.00 AND 100.00;
SELECT *
FROM PRODUCT
WHERE P_PRICE > 50.00 AND P_PRICE < 100.00;
44
Special Operators
• IS NULL is used to check whether an attribute
value is null.
• Examples:
SELECT P_CODE, P_DESCRIPT, V_CODE
FROM PRODUCT
FROM PRODUCT
45
Special Operators
• LIKE is used to check for similar character
strings.
• Examples:
SELECT V_NAME, V_CONTACT, V_AREACODE, V_PHONE
FROM VENDOR
FROM VENDOR
46
Special Operators
49
Changing a Column’s Data
Characteristics
• Examples:
– Changing a Column’s Data Type
ALTER TABLE PRODUCT MODIFY (V_CODE CHAR(5));
UPDATE PRODUCT
SET P_SALECODE = ‘2’
WHERE P_CODE = ‘1546-QQ2’;
51
Update of the P_SALECODE
Column in Multiple Data Rows
52
The Effect of Multiple Data
Updates in the PRODUCT
Table (MS Access)
UPDATE PRODUCT
SET P_SALECODE = ‘2’
WHERE P_INDATE < ’25-Dec-2003’;
UPDATE PRODUCT
SET P_SALECODE = ‘1’
WHERE P_INDATE >= 16-Jan-2004’ AND
P_INDATE < ’10-Feb-2004’;
53
The Effect of Multiple Data
Updates in the PRODUCT
Table (MS Access)
54
Copying Parts of Tables
• SQL permits copying contents of selected
table columns so that the data need not
be reentered manually into newly created
table(s)
56
PART Attributes Copied
from the PRODUCT Table
57
Adding or Dropping a Column
• Use ALTER to add a column
– Examples:
ALTER TABLE PRODUCT ADD PRIMARY KEY (P_CODE);
ALTER TABLE PRODUCT ADD PRIMARY KEY (P_CODE) ADD FOREIGN KEY
(V_CODE) REFERENCES VENDOR;
58
Adding or Dropping a Column
• Use ALTER to drop a column
• Example:
DROP TABLE PART;
59
References
60