SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections 6.5-6.7. (except 6.6.5 and 6.6.6) Next u Constraints. u Read.

Slides:



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

Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
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)
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,
Winter 2002Judy Cushing8–1 Schedule Jan. 30 (Wed) u Modifications, Schemas, Views. Read Sections This Week (Feb 4ff) u Constraints Read Sections.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all account records at the Perryridge branch.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 7.
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.
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.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
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.
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
Holliday - COEN 1781 Introduction to SQL. Holliday - COEN 1782 DB Tables and SQL The data is stored in the database in relations or tables Data types.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
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.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
1 The Relational Model Instructor: Mohamed Eltabakh
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
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.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all tuples from the loan relation. delete.
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.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
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.
CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.
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.
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.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
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
COP4710 Database Systems Relational Model.
Outerjoins, Grouping/Aggregation Insert/Delete/Update
CS 480: Database Systems Lecture 13 February 13,2013.
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
CPSC-608 Database Systems
Database Models Relational Model
2018, Fall Pusan National University Ki-Joune Li
Instructor: Mohamed Eltabakh
SQL OVERVIEW DEFINING A SCHEMA
Defining a Database Schema
Introduction to SQL Holliday - COEN 178.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
CS4433 Database Systems Relational Model.
SQL – Constraints & Triggers
CPSC-608 Database Systems
CMSC-461 Database Management Systems
SQL (Structured Query Language)
Presentation transcript:

SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read Sections ,

SCUHolliday - coen 1788–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;

SCUHolliday - coen 1788–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.

SCUHolliday - coen 1788–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.

SCUHolliday - coen 1788–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.

SCUHolliday - coen 1788–6 Declaring Keys Use PRIMARY KEY or UNIQUE. But only one primary key, many UNIQUE s allowed. SQL instructs implementations to create an index (data structure to speed access given a key value) in response to PRIMARY KEY only. 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).

SCUHolliday - coen 1788–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.

SCUHolliday - coen 1788–8 Example CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL, PRIMARY KEY(bar,beer) ); or CREATE TABLE Sells ( bar CHAR(20) PRIMARY KEY, beer VARCHAR(20), price REAL, );

SCUHolliday - coen 1788–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 );

SCUHolliday - coen 1788–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) );

SCUHolliday - coen 1788–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.

SCUHolliday - coen 1788–12 Changing Columns Add an attribute to 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;

SCUHolliday - coen 1788–13 Views An expression that describes a table without creating that table in the database. Provides a mechanism for hiding data from certain users. View definition form is: CREATE VIEW AS ;

SCUHolliday - coen 1788–14 View to Hide Info Create a view of customer names and their branch names without balances or acc# Depositor = (c-name,acc#) Account = (b-name,acc#,balance) create view cust as (select c-name, b-name from Depositor D, Account A where D.acc#=A.acc#);

SCUHolliday - coen 1788–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;

SCUHolliday - coen 1788–16 Querying Views Treat the view as if it were a materialized relation. Example SELECT beer FROM CanDrink WHERE drinker = ‘Sally’;

SCUHolliday - coen 1788–17 View Semantics The view definition becomes a part of the database schema and it is re-calculated every time it is used. u So your view automatically reflects changes (updates, etc) in the base relations. The view instance (the actual data in the view) is not stored in the database as the view but as part of the base tables. u Although special materialized views are.

SCUHolliday - coen 1788–18 Updates on Views Updates can be done on views if the system can translate it to an update of the base tables. You can update two or more tables in this way, however, updates on complex views that are difficult to translate to base tables (or would cause unacceptable nulls) are not allowed.

SCUHolliday - coen 1788–19 Updates on Views Create a view of all loan data in the Loan table leaving out the amount attribute. create view Branch-loan as select branch-name, loan# from Loan Now add a new row to Branch-loan insert into Branch-loan values ("Oakland", 11445) What will happen? There will be an insert of the row ("Oakland", 11445, null) into the Loan table.