Defining a Database Schema

Slides:



Advertisements
Similar presentations
CIT 613: Relational Database Development using SQL Revision of Tables and Data Types.
Advertisements

1 More SQL Database Modification Defining a Database Schema Views.
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014.
1 More SQL Database Modification Defining a Database Schema Views Source: slides by Jeffrey Ullman.
Representing Data Elements Gayatri Gopalakrishnan.
Fall 2001Arthur Keller – CS 1808–1 Schedule Today Oct. 18 (TH) Schemas, Views. u Read Sections u Project Part 3 extended to Oct. 23 (T). Oct.
1 More SQL Defining a Database Schema Views. 2 Defining a Database Schema uA database schema comprises declarations for the relations (“tables”) of the.
Winter 2002Arthur Keller – CS 1808–1 Schedule Today: Jan. 29 (T) u Modifications, Schemas, Views. u Read Sections Assignment 3 due. Jan. 31 (TH)
Basic SQL types String –Char(n): fixed length. Padded –Varchar(n): variable length Number –Integer: 32 bit –Decimal(5,2): –Real, Double: 32 bit,
Winter 2002Judy Cushing8–1 Schedule Jan. 30 (Wed) u Modifications, Schemas, Views. Read Sections This Week (Feb 4ff) u Constraints Read Sections.
Database Modifications A modification command does not return a result as a query does, but it changes the database in some way. There are three kinds.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #3.
Winter 2002Arthur Keller – CS 1807–1 Schedule Today: Jan. 24 (TH) u Subqueries, Grouping and Aggregation. u Read Sections Project Part 2 due.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
DB Modifications Modification = insert + delete + update. Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch Via Yoonsuck Choe.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
CSCE 520- Relational Data Model Lecture 2. Relational Data Model The following slides are reused by the permission of the author, J. Ullman, from the.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Winter 2006Keller Ullman Cushing8–1 Turning in Assignments Please turn in hard copy (use only in the direst of circumstances). I am not your secretary.
Databases 1 Fourth lecture. Rest of SQL Defining a Database Schema Views Foreign Keys Local and Global Constraints Triggers 2.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
1 Database Systems Defining Database Schema Views.
CSCE 520- Relational Data Model Lecture 2. Oracle login Login from the linux lab or ssh to one of the linux servers using your cse username and password.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
Himanshu GuptaCSE 532-SQL-1 SQL. Himanshu GuptaCSE 532-SQL-2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying.
Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
CPSC-310 Database Systems
Slides are reused by the approval of Jeffrey Ullman’s
Managing Tables, Data Integrity, Constraints by Adrienne Watt
COP4710 Database Systems Relational Model.
Outerjoins, Grouping/Aggregation Insert/Delete/Update
CS 480: Database Systems Lecture 13 February 13,2013.
Foreign Keys Local and Global Constraints Triggers
CPSC-310 Database Systems
CPSC-608 Database Systems
CPSC-608 Database Systems
Database Models Relational Model
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
Instructor: Mohamed Eltabakh
SQL OVERVIEW DEFINING A SCHEMA
IT 244 Database Management System
2018, Fall Pusan National University Ki-Joune Li
CMSC-461 Database Management Systems
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter 2: Creating And Modifying Database Tables
Chapter 4 Introduction to MySQL.
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
Instructor: Mohamed Eltabakh
CS4433 Database Systems Relational Model.
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
SQL – Constraints & Triggers
CPSC-608 Database Systems
Session - 6 Sequence - 1 SQL: The Structured Query Language:
CPSC-608 Database Systems
CMSC-461 Database Management Systems
Introduction to Oracle
Database Instructor: Bei Kang.
SQL (Structured Query Language)
Presentation transcript:

Defining a Database Schema CREATE TABLE name (list of elements). Principal elements are attributes and their types, but key declarations and constraints also appear. Similar CREATE X commands for other schema elements X: views, indexes, assertions, triggers. “DROP X name” deletes the created element of kind X with that name. Example CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL ); DROP TABLE Sells;

Types INT or INTEGER. REAL or FLOAT. CHAR(n) = fixed length character string, padded with “pad characters.” VARCHAR(n) = variable-length strings up to n characters. Oracle uses VARCHAR2(n) as well. PostgreSQL uses VARCHAR and does not support VARCHAR2.

DATE. SQL form is DATE 'yyyy-mm-dd' NUMERIC(precision, decimal) is a number with precision digits with the decimal point decimal digits from the right. NUMERIC(10,2) can store ±99,999,999.99 DATE. SQL form is DATE 'yyyy-mm-dd' PostgreSQL follows the standard. Oracle uses a different format. TIME. Form is TIME 'hh:mm:ss[.ss…]' in SQL. DATETIME or TIMESTAMP. Form is TIMESTAMP 'yyyy-mm-dd hh:mm:ss[.ss…]' in SQL. INTERVAL. Form is INTERVAL 'n period' in PostgreSQL. Period is month, days, year, etc.

PostgreSQL Dates PostgreSQL supports extensive date calculations. Conversions to_date(text), to_char(date/time/etc.), interval(text) Date ± Integer = Date; Date  Date = Integer (always = number of days); Date + Date is invalid! Timestamp ± Interval = Timestamp; Timestamp  Timestamp = Interval; Interval ± Interval = Interval; Date + Date is invalid. Interval: '1 month' could be 28, 29, 30, or 31 days; '31 days' is always just that. SQL uses DATEADD and DATEDIFF; PostgreSQL uses the simpler + and . Also CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP.

Declaring Keys Use PRIMARY KEY or UNIQUE. But only one primary key, many UNIQUEs allowed. SQL permits implementations to create an index (data structure to speed access given a key value) in response to PRIMARY KEY only. But PostgreSQL and Oracle create indexes for both. SQL does not allow nulls in primary key, but allows them in “unique” columns (which may have two or more nulls, but not repeated non-null values).

Declaring Keys Two places to declare: After an attribute’s type, if the attribute is a key by itself. As a separate element. Essential if key is >1 attribute.

Example CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL, PRIMARY KEY(bar,beer) );

Example is different than: CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL, UNIQUE(bar,beer) ); is different than: bar CHAR(20) UNIQUE, beer VARCHAR(20) UNIQUE, price REAL

Other Properties You Can Give to Attributes NOT NULL = every tuple must have a real value for this attribute. DEFAULT value = a value to use whenever no other value of this attribute is known. Example CREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT '123 Sesame St', phone CHAR(16) );

results in the following tuple: INSERT INTO Drinkers(name) VALUES('Sally') results in the following tuple: name addr phone Sally 123 Sesame St. NULL Primary key is by default not NULL. This insert is legal. OK to list a subset of the attributes and values for only this subset. But if we had declared phone CHAR(16) NOT NULL then the insertion could not be made.

Interesting Defaults DEFAULT CURRENT_TIMESTAMP SEQUENCE CREATE SEQUENCE customer_seq; CREATE TABLE Customer ( customerID INTEGER DEFAULT nextval('customer_seq'), name VARCHAR(30) );

Changing Columns Example Add an attribute of relation R with ALTER TABLE R ADD <column declaration>; Example ALTER TABLE Bars ADD phone CHAR(16) DEFAULT 'unlisted'; Columns may also be dropped. ALTER TABLE Bars DROP license;

Views An expression that describes a table without creating it. View definition form is: CREATE VIEW <name> AS <query>;

Example Querying Views Example The view CanDrink is the set of drinker-beer pairs such that the drinker frequents at least one bar that serves the beer. CREATE VIEW CanDrink AS SELECT drinker, beer FROM Frequents, Sells WHERE Frequents.bar = Sells.bar; Querying Views Treat the view as if it were a materialized relation. Example SELECT beer FROM CanDrink WHERE drinker = ‘Sally’;

Semantics of View Use Example

Compose

Optimize Query Push selections down tree. Eliminate unnecessary projections.