Data Manipulation Language (DML) Data Definition ..., Summaries of Database Management Systems (DBMS)

Data types. − Integrity constraints. • Data Manipulation Language (DML). − Subset of SQL used to manipulate data in databases.

Typology: Summaries

2022/2023

Uploaded on 03/01/2023

aasif
aasif 🇺🇸

4.9

(7)

218 documents

1 / 60

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Definition Lang. (DDL)
Data Manipulation Lang. (DML)
Views & Indexes
EECS3421 - Introduction to Database Management Systems
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c

Partial preview of the text

Download Data Manipulation Language (DML) Data Definition ... and more Summaries Database Management Systems (DBMS) in PDF only on Docsity!

Data Definition Lang. (DDL)

Data Manipulation Lang. (DML)

Views & Indexes

EECS3421 - Introduction to Database Management Systems

SQL Main Components

  • Queries − Subset of SQL for read-only access to database − SELECT statements
  • Data Definition Language (DDL) − Subset of SQL used to describe database schemas − CREATE, ALTER, DROP statements − Data types − Integrity constraints
  • Data Manipulation Language (DML) − Subset of SQL used to manipulate data in databases − INSERT, DELETE, UPDATE statements

Creating (Declaring) a Schema

  • A schema is essentially a namespace − it contains named objects (tables, data types, functions, etc.)
  • The schema name must be distinct from any existing schema name in the current database
  • Syntax: CREATE SCHEMA schemaname [ AUTHORIZATION username ] [ schema_element [ ... ] ] Examples: CREATE SCHEMA myschema; CREATE SCHEMA myschema AUTHORIZATION manos;

Creating (Declaring) a Relation/Table

  • To create a relation:

CREATE TABLE (

  • To delete a relation:

DROP TABLE ;

  • To alter a relation (add/remove column): ALTER TABLE ADD ALTER TABLE DROP

Examples

  • To create a relation: CREATE TABLE employees ( id INTEGER, first_name CHAR(50), last_name VARCHAR(100));
  • To delete a relation: DROP TABLE employees;
  • To alter a relation (add/remove column): ALTER TABLE employees ADD age INTEGER; ALTER TABLE employess DROP last_name;

SQL Values

• Integers and reals are represented as you would

expect

• Strings are too, except they require single quotes.

− Two single quotes = real quote, e.g., ’Joe’’s Bar’

• Any value can be NULL

− Unless attribute has NOT NULL constraint E.g.: price REAL NOT NULL

INTEGRITY CONSTRAINTS

10

Running Example

Beers(name, manf)

Bars(name, addr, license)

Drinkers(name, addr, phone)

Likes(drinker, beer)

Sells(bar, beer, price)

Frequents(drinker, bar)

Underline = key (tuples cannot have the same value in all

key attributes)

− Excellent example of a constraint

Declaring Keys

  • An attribute or list of attributes may be declared

PRIMARY KEY or UNIQUE

− Either says that no two tuples of the relation may agree in all the attribute(s) on the list − There are a few distinctions to be mentioned later − Place PRIMARY KEY or UNIQUE after the type in the declaration of the attribute. CREATE TABLE Beers ( name CHAR(20) PRIMARY KEY , manf CHAR(20) );

Declaring Multi-attribute Keys

  • A key declaration can appear as element in the list of elements of a CREATE TABLE statement
  • This form is essential if the key consists of more than one attribute CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL, PRIMARY KEY (bar, beer));

The bar and beer together are the key for Sells

Foreign Keys

  • Values appearing in attributes of one relation must appear

together in certain attributes of another relation

Example:

We might expect that a value in Sells.beer also appears

as value in Beers.name

Beers (name, manf)

Sells (bar, beer, price)

Expressing Foreign Keys

  • Use keyword REFERENCES , either: − After an attribute (for one-attribute keys) REFERENCES () − As an element of the schema:

FOREIGN KEY ()

REFERENCES ()

  • Referenced attributes must be declared PRIMARY KEY

or UNIQUE

Example: As Schema Element

CREATE TABLE Beers (

name CHAR(20) PRIMARY KEY ,

manf CHAR(20) );

CREATE TABLE Sells (

bar CHAR(20),

beer CHAR(20),

price REAL,

FOREIGN KEY (beer) REFERENCES Beers(name));

Enforcing Foreign-Key Constraints

  • If there is a foreign-key constraint from relation R to relation S , two violations are possible: − An insert or update to R introduces values not found in S − A deletion or update to S causes some tuples of R to “dangle” Example: suppose R = Sells , S = Beers
  • An insert or update to Sells that introduces a non-existent beer must be rejected
  • A deletion or update to Beers that removes a beer value found in some tuples of Sells can be handled in three ways (next slide).