Docsity

SQL-99: Schema Definition, Constraints, and Queries and Views - Prof. Zhijiang Dong, Study notes of Database Management Systems (DBMS)

Partial preview of the text

Download SQL-99: Schema Definition, Constraints, and Queries and Views - Prof. Zhijiang Dong and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

CSCI 4560

Data Types

DDL

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

DML

INSERT

DELETE

UPDATE

DQL

One Table Query Combining Queries Multi-table Query INNER JOIN OUTER JOIN

SQL Function

Catalog &

Schema

Database Management Systems

Topic: SQL-99: Schema Definition, Constraints, and

Queries and Views

Zhijiang Dong

Dept. of Computer Science

Middle Tennessee State University

Based on the slides coming with the textbook

CSCI 4560

Data Types

DDL

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

DML

INSERT

DELETE

UPDATE

DQL

One Table Query Combining Queries Multi-table Query INNER JOIN OUTER JOIN

SQL Function

Catalog &

Schema

Topic: SQL-99: Schema Definition, Constraints,

and Queries and Views

1 Data Types

2 Data Definition Language

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

3 Data Manipulation Language

INSERT

DELETE

UPDATE

4 Data Query Language

One Table Query

Combining Queries

Multi-table Query

INNER JOIN

OUTER JOIN

5 SQL Function

6 Catalog & Schema

CSCI 4560

Data Types

DDL

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

DML

INSERT

DELETE

UPDATE

DQL

One Table Query Combining Queries Multi-table Query INNER JOIN OUTER JOIN

SQL Function

Catalog &

Schema

Topic: SQL-99: Schema Definition, Constraints,

and Queries and Views

1 Data Types

2 Data Definition Language

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

3 Data Manipulation Language

INSERT

DELETE

UPDATE

4 Data Query Language

One Table Query

Combining Queries

Multi-table Query

INNER JOIN

OUTER JOIN

5 SQL Function

6 Catalog & Schema

CSCI 4560

Data Types

DDL

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

DML

INSERT

DELETE

UPDATE

DQL

One Table Query Combining Queries Multi-table Query INNER JOIN OUTER JOIN

SQL Function

Catalog &

Schema

SQL Data Types

Predefined Data Types

Built-in data types

Constructed Types

Specified using one of SQL’s data type constructors, such as ARRAY,

MULTISET, REF, and ROW

User-defined Types

A type defined by users using SQL statements

CSCI 4560

Data Types

DDL

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

DML

INSERT

DELETE

UPDATE

DQL

One Table Query Combining Queries Multi-table Query INNER JOIN OUTER JOIN

SQL Function

Catalog &

Schema

Predefined Data Types: Binary String Types

A binary string is a sequence of bytes in the same way that a

character string is.

A binary string is used to hold nontraditional data such as

images, audio, and video

Literal bit strings are placed between single quotes but

preceded by a B to distinguish them from character strings.

B’101100’

CSCI 4560

Data Types

DDL

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

DML

INSERT

DELETE

UPDATE

DQL

One Table Query Combining Queries Multi-table Query INNER JOIN OUTER JOIN

SQL Function

Catalog &

Schema

Predefined Data Types: Binary String Types

BIT(n)

used for fixed-length binary strings

n specifies the length

if n is omitted, the length is 1

BIT VARYING(n) or VARBIT(n)

used for varying-length binary strings

n specifies the length

n cannot be omitted

BINARY LARGE OBJECT or BLOB

to store large binary values

typically, BLOB can store up to 2 or 4 gigabytes of data

CSCI 4560

Data Types

DDL

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

DML

INSERT

DELETE

UPDATE

DQL

One Table Query Combining Queries Multi-table Query INNER JOIN OUTER JOIN

SQL Function

Catalog &

Schema

Predefined Data Types: Exact Numbers

Exact numbers can either be whole integers or have decimal

points.

Exact numbers can be positive and negative.

Exact numbers have precision and scale.

Precision determines the maximum total number of decimal digits that

can be stored (both to the left and to the right of the decimal point)

Scale specifies the maximum number of decimals allowed.

Literals for exact numbers

CSCI 4560

Data Types

DDL

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

DML

INSERT

DELETE

UPDATE

DQL

One Table Query Combining Queries Multi-table Query INNER JOIN OUTER JOIN

SQL Function

Catalog &

Schema

Predefined Data Types: Exact Numbers

INTEGER or INT

represents countable numbers

precision is implementation-specific

SMALLINT

virtually same as INT, but with a smaller precision

NUMERIC(precision, scale) or DECIMAL(precision, scale)

The default for precision is implementation-defined.

The default for scale is 0.

An error is reported if inserting a value with more figures before the

decimal point than column allows.

Values with more decimal points than specified will simply be

rounded.

CSCI 4560

Data Types

DDL

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

DML

INSERT

DELETE

UPDATE

DQL

One Table Query Combining Queries Multi-table Query INNER JOIN OUTER JOIN

SQL Function

Catalog &

Schema

Predefined Data Types: Approximate Numbers

FLOAT(precision)

store floating-point numbers with precision optionally specified by user

REAL

similar to FLOAT, but its precision is fixed

DOUBLE PRECISION

virtually the same as REAL, but with a greater precision

CSCI 4560

Data Types

DDL

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

DML

INSERT

DELETE

UPDATE

DQL

One Table Query Combining Queries Multi-table Query INNER JOIN OUTER JOIN

SQL Function

Catalog &

Schema

Predefined Data Types: Date and Time Data

Types

DATE

Made up of year-month-day in the format yyyy-mm-dd

Literal values are represented by single-quoted strings preceded by

the keyword DATE. Example: DATE’2002-09-27’

TIME

Made up of hour:minute:second in the format hh:mm:ss

Literal values are represented by single-quoted strings preceded by

the keyword TIME. Example: TIME’09:12:47’

TIME(i)

Made up of hour:minute:second plus i additional digits specifying

fractions of a second

format is hh:mm:ss:ii...i

CSCI 4560

Data Types

DDL

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

DML

INSERT

DELETE

UPDATE

DQL

One Table Query Combining Queries Multi-table Query INNER JOIN OUTER JOIN

SQL Function

Catalog &

Schema

Predefined Data Types: Boolean

In SQL, because of the presence of NULL values, a

three-valued logic is used, i.e. TRUE, FALSE, or

UNKNOWN.

But ORACLE, DB2 and MS SQL don’t have a BOOLEAN

data type. Therefore, we cannot create a column of

BOOLEAN type.

However, BOOLEAN data type can be easily simulated, for

example by using a user-defined data type of type

VARCHAR that only allows FALSE and TRUE for its values

CSCI 4560

Data Types

DDL

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

DML

INSERT

DELETE

UPDATE

DQL

One Table Query Combining Queries Multi-table Query INNER JOIN OUTER JOIN

SQL Function

Catalog &

Schema

Predefined Data Types: User-Defined Types

CREATE TYPE

This statement is used to create a new data type.

CREATE DOMAIN

This statement is used to create a new domain.

A domain is essentially a data type with optional constraints

(restrictions on the allowed set of values)

Syntax:

CREATE DOMAIN name [ AS ] data_type

[ DEFAULT expression ]

[ domain constraints ... ]

Examples:

CREATE DOMAIN SSN_TYPE AS CHAR(9);

CREATE DOMAIN D_NUM AS INT

CHECK (D_NUM > 0 AND D_NUM < 21);

CSCI 4560

Data Types

DDL

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

DML

INSERT

DELETE

UPDATE

DQL

One Table Query Combining Queries Multi-table Query INNER JOIN OUTER JOIN

SQL Function

Catalog &

Schema

Data Definition Language

CREATE statement

To make a new database, table, view, index, or users

DROP statement

To destroy an existing database, table, index, view, or users

ALTER statement

To modify an existing database object such as table, or views.

CSCI 4560

Data Types

DDL

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

DML

INSERT

DELETE

UPDATE

DQL

One Table Query Combining Queries Multi-table Query INNER JOIN OUTER JOIN

SQL Function

Catalog &

Schema

Topic: SQL-99: Schema Definition, Constraints,

and Queries and Views

1 Data Types

2 Data Definition Language

CREATE TABLE

CREATE VIEW

DROP TABLE

ALTER TABLE

3 Data Manipulation Language

INSERT

DELETE

UPDATE

4 Data Query Language

One Table Query

Combining Queries

Multi-table Query

INNER JOIN

OUTER JOIN

5 SQL Function

6 Catalog & Schema