Top Banner
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
26

Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Dec 26, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Chapter 9 SQL and RDBMS

Part C. SQL

Copyright 2005 Radian Publishing Co.

Page 2: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.2/26

Contents

Chapter 9 SQL and RDBMS

9.1 What is SQL

• 9.1 A. DDL

• 9.1 B. DML

• 9.1 C. DCL

9.2 SQL and RDBMS

• 9.2 A. MS Access 97, 2003 and Visual FoxPro

• 9.2 B. MySQL

9.3 Creating a Database

9.4 Creating a Table

9.5 Creating a Table with Constraints

9.6 Filling Table with Data

9.7 Other DDL Commands

Page 3: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.3/26

Chapter 9 SQL and RDBMS

SQL stands for Structured Query Language.

A relational database management system (RDBMS) fully supports

SQL.

Page 4: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.4/26

9.1 What is SQL

Set-oriented means that SQL processes sets of data in groups.

Non-procedural means that SQL does not care about how the result is

obtained, but it stresses on what result will be obtained.

SQL commands can be classified into three types:

• Data Definition Language (DDL) commands• Data Manipulation Language (DML) commands• Data Control Language (DCL) commands

Page 5: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.5/26

9.1 A. DDL

Data Definition Language (DDL) commands are used to create, alter

and delete tables, views and indexes, including

• CREATE DATABASE• DROP DATABASE• CREATE TABLE• DROP TABLE• ALTER TABLE• CREATE INDEX• DROP INDEX• CREATE VIEW• DROP VIEW

Page 6: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.6/26

9.1 B. DML (1/2)

Data Manipulation Language (DML) commands are used to update,

insert, modify and query the data in the database, including

• SELECT• INSERT• UPDATE• DELETE

Page 7: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.7/26

9.1 B. DML (2/2)

The reasons for writing programs to issue SQL commands are

1. meeting specific needs of an organisation

2. providing interface for users who do not know SQL

3. handling errors

4. enforce database security

Page 8: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.8/26

9.1 C. DCL

Data Control Language (DCL) commands help DBA to control and

maintain the database, including GRANT, ALTER USER etc.

DBA stands for database Administrator (see Chapter 3)

Page 9: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.9/26

9.2 SQL and RDBMS

The SQL standards are promoted by two standards organisations: the

American National Standards Institute (ANSI) and International

Standards Organisation (ISO). In the textbook, ANSI-92 (launched in

1992) will be used.

Proprietary Extensions mean including extension to the standards, like

ANSI-92.

Page 10: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.10/26

9.2 A. MS Access 97, 2003 and Visual FoxPro

MS Access® 97, MS Access® 2003 and MS Visual FoxPro® 5.0 are

software products of Microsoft.

Page 11: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.11/26

9.2 B. MySQL

MySQL is an open source RDBMS product, meaning that it is possible

for anyone to use and modify the software.

MySQL always works in the client-server mode, even in a standalone

computer.

Fig.9.1 Versions of MySQL

Page 12: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.12/26

9.3 Creating a Database (1/2)

Fig.9.3 Handling of database files by different DBMS

Page 13: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.13/26

9.3 Creating a Database (2/2)

In MS Access, related tables are stored in a single file.

In Visual FoxPro, database container is optional.

The SQL command to create database:

CREATE DATABASE dBName;

MySQL command to show database:

SHOW DATABASES;

MySQL command to open a database:

USE dBName;

Page 14: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.14/26

9.4 Creating a Table (1/3)

The basic syntax for creating a new table is

CREATE TABLE TableName1

(FieldName1 FieldType [(FieldWidth [, Precision])] [NOT NULL]

[PRIMARY KEY] [UNIQUE]

[, FieldName2 ...]);

Page 15: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.15/26

9.4 Creating a Table (2/3)

Fig.9.4 Table to be created

Fig.9.5 Structure of thetable to be created

Page 16: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.16/26

9.4 Creating a Table (3/3)

The SQL statement for creating the table BRASSITEM

Page 17: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.17/26

9.5 Creating a Table with Constraints (1/2)

Some DBMS enforces constraints to insure data integrity. The constraints include

1. the field value is unique (keywords UNIQUE or PRIMARY KEY).

2. the field value is non-empty (keywords NOT NULL or PRIMARY KEY).

3. providing default field value (keyword DEFAULT)

4. generating a number for a numeric field when new record is created (keyword AUTO_INCREMENT)

5. make sure that a foreign key will be updated if the primary key is updated (keyword FOREIGN KEY, CHECK, CASCADE, RESTRICT, ON DELETE and ON UPDATE)

Page 18: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.18/26

9.5 Creating a Table with Constraints (2/2)

MySQL command to show the structure of the table:

SHOW FIELDS FROM TableName;

Page 19: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.19/26

9.6 Filling Table with Data (1/3)

The basic syntax for inserting a new record is:

INSERT INTO TableName [(FieldName1 [, FieldName2 ...])]

VALUES (Expression1 [, Expression2 ...]);

Page 20: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.20/26

9.6 Filling Table with Data (2/3)

If all the fields will be filled with values, you may skip entering the field

names in the Insert statement.

Page 21: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.21/26

9.6 Filling Table with Data (3/3)

The date format in ANSI-92 is YYYY-MM-DD. Dates should be

enclosed in quotation marks.

For example '1991-4-1' represents the 1st day in April of 1991.

This format applies to MySQL, MS Access 97 and MS Access 2003,

but not in Visual FoxPro.

In Visual FoxPro, however, the default date format is MM-DD-YYYY,

and braces are used instead of quotation marks.

For example {4-1-1991}

Page 22: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.22/26

9.7 Other DDL Commands (1/5)

The basic syntax for creating an index is:

CREATE INDEX IndexName

ON TableName (FieldName [ASC|DESC], ...);

Page 23: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.23/26

9.7 Other DDL Commands (2/5)

The syntax for changing the structure of a table is:

ALTER TABLE TableName1

ADD | ALTER (MODIFY|CHANGE) | DROP [COLUMN] FieldName1

FieldType [(FieldWidth [, Precision])];

Page 24: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.24/26

9.7 Other DDL Commands (3/5)

In MS Access and FoxPro, ALTER COLUMN is used to change the

size and data type of a field. But, you cannot change a field name.

In MySQL, MODIFY COLUMN is used to change the size and data type

of a field. CHANGE COLUMN is used to change a field name.

Page 25: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.25/26

9.7 Other DDL Commands (4/5)

The SQL command to delete a table:

DROP TABLE TableName;

Page 26: Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.26/26

9.7 Other DDL Commands (5/5)

The SQL command to delete database:

DROP DATABASE DBName;