Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Module 7 Basic SQL-1

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Module 7 Basic SQL-1

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

‫الجامعة السعودية االلكترونية‬

‫الجامعة السعودية االلكترونية‬

‫‪26/12/2021‬‬
College of Computing and
Informatics

Introduction to Database
Introduction to Database

Basic SQL-1
Contents
1. SQL Data Definition and Data Types
2. Specifying Constraints in SQL
Weekly Learning
Outcomes
1. Explain data definition and constraints in SQL.
Required Reading
1. Chapter 6: SQL Data Definition and Data Types
2. Chapter 6: Specifying Constraints in SQL
(Fundamentals of Database Systems, Global Edition,
7th Edition (2017) by Ramez Elmasri & Shamkant
Navathe)

Recommended Reading
SQL-I: https://courses.cs.vt.edu/cs4604/Spring21/pdfs/3-sql1.pdf
Learn to create database objects and manipulate data:
https://github.com/VTCourses/CS4604_Labs/tree/master/1.ddl_dml

This Presentation is mainly dependent on the textbook: Fundamentals of Database Systems, Global Edition, 7th Edition (2017) by Ramez Elmasri & Shamkant Navathe
• SQL Data Definition and Data Types
Basic SQL

• Overview of SQL

• Data Definition Language (DDL) for Creating a Relational Database Schema


– CREATE statements

– Specifying Constraints

• Basic Retrieval Queries in SQL


– SELECT … FROM … WHERE … statements

• Basic Database Modification in SQL


– INSERT, DELETE, UPDATE statements
Overview of SQL
• SQL is a standard, comprehensive language, based on
the relational model
• SQL includes capabilities for many functions:
– DDL statements for creating schemas and specifying data
types and constraints
– Statements for specifying database retrievals
– Statements for modifying the database
– Statements for specifying views, triggers, and assertions
– Many other capabilities
• We introduce the first three capabilities in this chapter
SQL Features in This Chapter
• Basic SQL DDL
– Includes the CREATE statements
– Has a comprehensive set of SQL data types
– Can specify key, referential integrity, and other constraints
• Basic Retrieval Queries in SQL
– SELECT … FROM … WHERE … statements
• Basic Database Modification in SQL
– INSERT, DELETE, UPDATE statements
SQL: Origins and History
• Evolution of the SQL Standard
– First standard approved in 1989 (called SQL-89 or SQL1)
– Comprehensive revision in 1992 (SQL-92 or SQL2)
– Third big revision in 1999 (SQL-99, a trimmed-down version of a
more ambitious revision known as SQL3)
– Other revisions known as SQL:2003, SQL:2006, SQL:2008
• Origins of SQL
– Originally called SEQUEL (Structured English Query Language),
then SQL (Structured Query Language)
– Developed at IBM Research for experimental relational DBMS
called System-R in the 1970s
SQL Data Definition
• CREATE statement can be used to:
– Create a named database schema
– Create individual database tables and specify data
types for the table attributes, as well as key, referential
integrity, and NOT NULL constraints
– Create named constraints
• Other commands can modify the tables and
constraints
– DROP and ALTER statements
CREATE TABLE (1)
• In its simplest form, specifies a new base relation by giving it a
name, and specifying each of its attributes and their data
types (INTEGER, FLOAT, DECIMAL(i, j), CHAR(n), VARCHAR(n),
DATE, and other data types)
• A constraint NOT NULL may be specified on an attribute
CREATE TABLE DEPARTMENT (
DNAME VARCHAR(15) NOT NULL,
DNUMBER INT NOT NULL,
MGRSSN CHAR(9) NOT NULL,
MGRSTARTDATE DATE );
CREATE TABLE (2)
• CREATE TABLE can also specify the primary key, UNIQUE keys, and
referential integrity constraints (foreign keys)
• Via the PRIMARY KEY, UNIQUE, and FOREIGN KEY phrases

CREATE TABLE DEPARTMENT (


DNAME ARCHAR(15) NOT NULL,
DNUMBER INT NOT NULL,
MGRSSN CHAR(9) NOT NULL,
MGRSTARTDATE DATE,
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY(MGRSSN) REFERENCES EMPLOYEE);
Example: The COMPANY Database

• Figure 3.7 (next slide) shows the COMPANY database


schema diagram
• Referential integrity constraints shown as directed edges
from foreign key to referenced relation
• Primary key attributes of each table underlined
Example: The COMPANY DDL

• Figure 4.1 (following two slides) shows example DDL for


creating the tables of the COMPANY database
• Circular reference problem:
– In Figure 3.7, some foreign keys cause circular references:
• EMPLOYEE.Dno → DEPARTMENT.Dnumber
• DEPARTMENT.Mgr_ssn → EMPLOYEE.Ssn
– One of the tables must be created first without the FOREIGN KEY
in the CREATE TABLE statement
• The missing FOREIGN KEY can be added later using ALTER TABLE
Continued next page…
The CREATE SCHEMA Statement

• A DBMS can manage multiple databases


• DBA (or authorized users) can use CREATE SCHEMA to have
distinct databases; for example:
CREATE SCHEMA COMPANY AUTHORIZATION 'Smith';
• Each database has a schema name (e.g. COMPANY)
• User 'Smith' will be “owner” of schema, can create tables,
other constructs in that schema
• Table names can be prefixed by schema name if multiple
schemas exist (e.g. COMPANY.EMPLOYEE)
• Prefix not needed if there is a “default schema” for the user
Basic SQL Data Types (1)
• Basic numeric data types:
– Integers: INTEGER (or INT), SMALLINT
– Real (floating point): FLOAT (or REAL), DOUBLE PRECISION
– Formatted: DECIMAL(i, j) (or DEC (i, j) or NUMERIC(i, j))
specify i total decimal digits, j after decimal point
• i called precision, j called scale
• Basic character string data types:
– Fixed-length: CHAR(n) or CHARACTER(n)
– Variable-length: VARCHAR(n) or CHAR VARYING(n) or
CHARACTER VARYING(n)
SQL Data Types (2)
• Basic boolean data types:
– BIT(n), BIT VARYING (n)
• Large object data types:
– Binary large objects: BLOB(n)
– Can be used to store attributes that represent images, audio,
video, or other large binary objects
– Character large objects: CLOB(n)
– Can be used attributes that store articles, news stories, text files,
and other large character objects
SQL Data Types (3)
• DATE data type:
– Standard DATE formatted as yyyy-mm-dd
– For example, DATE '2010-01-22'
– Older formats still used in some systems, such as 'JAN-22-2010'
– Values are ordered, with later dates larger than earlier ones
• TIME data type:
– Standard TIME formatted as hh:mm:ss
– E.g., TIME '15:20:22' represents 3.20pm and 22 seconds
– TIME(i) includes and additional i-1 decimal digits for fractions of
a second
– E.g., TIME(5) value could be '15:20:22.1234'
SQL Data Types (4)
• TIMESTAMP data type:
– A DATE combined with a TIME(i), where the default i=7
– For example, TIMESTAMP '2010-01-22 15:20:22.123456'
– A different i>7 can be specified if needed
• INTERVAL represents a relative YEAR/MONTH or
DAY/TIME (e.g. 2 years and 5 months, or 5 days 20
hours 3 minutes 22 seconds)
• Other SQL data types exist; we presented the most
common ones
• Specifying Constraints in SQL
Specifying Referential Integrity Options
and Naming of Constraints

• We can specify RESTRICT (the default), CASCADE, SET NULL or


SET DEFAULT on referential integrity constraints (foreign keys)
• Separate options for ON DELETE, ON UPDATE
• Figure 4.2 (next slide) gives some examples
• A constraint can be given a constraint name; this allows to DROP
the constraint later
• Figure 4.2 illustrates naming of constraints
Specifying DOMAIN Constraints Using
the CHECK Clause

• Another type of constraint can be specified using CHECK


• E.g., CHECK (Dnumber > 0 AND Dnumber < 21) can be specified
after the Dnumber attribute
• Alternatively, a special domain can be created with a domain
name using CREATE DOMAIN
• E.g. CREATE DOMAIN D_NUM AS INTEGER CHECK (Dnumber > 0
AND Dnumber < 21);
• D_NUM can now be used as the data type for the Dnumber
attribute of the DEPARTMENT table, as well as for Dnum of
PROJECT, Dno of EMPLOYEE, and so on
• CHECK can also be used to specify more general constraints
Main Reference
1. Chapter 6: SQL Data Definition and Data Types
2. Chapter 6: Specifying Constraints in SQL
(Fundamentals of Database Systems, Global Edition,
7th Edition (2017) by Ramez Elmasri & Shamkant
Navathe)

Additional References
https://courses.cs.vt.edu/cs4604/Spring21/pdfs/3-sql1.pdf

https://github.com/VTCourses/CS4604_Labs/tree/master/1.ddl_dml

This Presentation is mainly dependent on the textbook: Fundamentals of Database Systems, Global Edition, 7th Edition (2017) by Ramez Elmasri & Shamkant Navathe
Thank
You

You might also like