Winter 2002Arthur Keller – CS 1808–1 Schedule Today: Jan. 29 (T) u Modifications, Schemas, Views. u Read Sections 6.5-6.7. Assignment 3 due. Jan. 31 (TH)

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.
Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
1 Introduction to SQL Multirelation Queries Subqueries Slides are reused by the approval of Jeffrey Ullman’s.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
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 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
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.
Fall 2001Arthur Keller – CS 1809–1 Schedule Today Oct. 23 (T) Constraints. u Read Sections Assignment 4 due. Project Part 3 due Oct. 24 (W). Oct.
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.
1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.
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.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Triggers, PL/SQL, embedded SQL, JDBC.
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.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
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.
Chapter 4 Introduction to MySQL. MySQL “the world’s most popular open-source database application” “commonly used with PHP”
CSC 2720 Building Web Applications Database and SQL.
Databases 1 Fourth lecture. Rest of SQL Defining a Database Schema Views Foreign Keys Local and Global Constraints Triggers 2.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
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.
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
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.
Sql DDL queries CS 260 Database Systems.
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.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
1 Introduction to Database Systems, CS420 SQL Views and Indexes.
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
CPSC-310 Database Systems
COP4710 Database Systems Relational Model.
Outerjoins, Grouping/Aggregation Insert/Delete/Update
CS 480: Database Systems Lecture 13 February 13,2013.
CPSC-310 Database Systems
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
CPSC-608 Database Systems
CPSC-608 Database Systems
Database Models Relational Model
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
SQL OVERVIEW DEFINING A SCHEMA
IT 244 Database Management System
Defining a Database Schema
Session - 6 Sequence - 1 SQL: The Structured Query Language:
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
SQL – Constraints & Triggers
CPSC-608 Database Systems
CPSC-608 Database Systems
CMSC-461 Database Management Systems
SQL (Structured Query Language)
Presentation transcript:

Winter 2002Arthur Keller – CS 1808–1 Schedule Today: Jan. 29 (T) u Modifications, Schemas, Views. u Read Sections Assignment 3 due. Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers, PL/SQL. u Read Sections 7.4, 8.2. Assignment 4 due. Feb. 7 (TH) u PL/SQL, Embedded SQL, CLI, JDBC. u Read Sections 8.1, Reminder: Midterm is Feb. 14 (TH)

Winter 2002Arthur Keller – CS 1808–2 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;

Winter 2002Arthur Keller – CS 1808–3 Types 1.INT or INTEGER. 2.REAL or FLOAT. 3.CHAR( n ) = fixed length character string, padded with “pad characters.” 4.VARCHAR (n ) = variable-length strings up to n characters.  Oracle uses VARCHAR2 (n ) as well. PostgreSQL uses VARCHAR and does not support VARCHAR2.

Winter 2002Arthur Keller – CS 1808–4 5. 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, DATE. SQL form is DATE 'yyyy-mm-dd' PostgreSQL follows the standard. Oracle uses a different format. 7. TIME. Form is TIME 'hh:mm:ss[.ss…] ' in SQL. 8. DATETIME or TIMESTAMP. Form is TIMESTAMP 'yyyy-mm-dd hh:mm:ss[.ss…] ' in SQL. 9. INTERVAL. Form is INTERVAL ' n period' in PostgreSQL. Period is month, days, year, etc.

Winter 2002Arthur Keller – CS 1808–5 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.

Winter 2002Arthur Keller – CS 1808–6 Declaring Keys Use PRIMARY KEY or UNIQUE. But only one primary key, many UNIQUE s allowed. SQL permits implementations to create an index (data structure to speed access given a key value) in response to PRIMARY KEY only. u 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).

Winter 2002Arthur Keller – CS 1808–7 Declaring Keys Two places to declare: 1.After an attribute’s type, if the attribute is a key by itself. 2.As a separate element. u Essential if key is >1 attribute.

Winter 2002Arthur Keller – CS 1808–8 Example CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL, PRIMARY KEY(bar,beer) );

Winter 2002Arthur Keller – CS 1808–9 Example CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL, UNIQUE(bar,beer) ); is different than: CREATE TABLE Sells ( bar CHAR(20) UNIQUE, beer VARCHAR(20) UNIQUE, price REAL );

Winter 2002Arthur Keller – CS 1808–10 Other Properties You Can Give to Attributes 1. NOT NULL = every tuple must have a real value for this attribute. 2. 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) );

Winter 2002Arthur Keller – CS 1808–11 INSERT INTO Drinkers(name) VALUES('Sally') results in the following tuple: nameaddrphone Sally123 Sesame St. NULL Primary key is by default not NULL. This insert is legal. u 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.

Winter 2002Arthur Keller – CS 1808–12 Interesting Defaults DEFAULT CURRENT_TIMESTAMP SEQUENCE CREATE SEQUENCE customer_seq; CREATE TABLE Customer ( customerID INTEGER DEFAULT nextval('customer_seq'), name VARCHAR(30) );

Winter 2002Arthur Keller – CS 1808–13 Changing Columns Add an attribute of relation R with ALTER TABLE R ADD ; Example ALTER TABLE Bars ADD phone CHAR(16) DEFAULT 'unlisted'; Columns may also be dropped. ALTER TABLE Bars DROP license;

Winter 2002Arthur Keller – CS 1808–14 Views An expression that describes a table without creating it. View definition form is: CREATE VIEW AS ;

Winter 2002Arthur Keller – CS 1808–15 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’;

Winter 2002Arthur Keller – CS 1808–16 Semantics of View Use Example

Winter 2002Arthur Keller – CS 1808–17 Compose

Winter 2002Arthur Keller – CS 1808–18 Optimize Query 1.Push selections down tree. 2.Eliminate unnecessary projections.