CSI 2132 Lab 3. Outline 09/02/2012. More on SQL. Destroying and Altering Relations. Exercise: DROP TABLE ALTER TABLE SELECT



Similar documents
A basic create statement for a simple student table would look like the following.

Physical Database Design Process. Physical Database Design Process. Major Inputs to Physical Database. Components of Physical Database Design

A Brief Introduction to MySQL

Chapter 5. SQL: Queries, Constraints, Triggers

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Database Administration with MySQL

SQL. Short introduction

5. CHANGING STRUCTURE AND DATA

Oracle Database 10g Express

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Unit 3. Retrieving Data from Multiple Tables

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

Intro to Databases. ACM Webmonkeys 2011

Mul$media im Netz (Online Mul$media) Wintersemester 2014/15. Übung 03 (Nebenfach)

EECS 647: Introduction to Database Systems

Oracle 10g PL/SQL Training

Lab 2: PostgreSQL Tutorial II: Command Line

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

Learning MySQL! Angola Africa SELECT name, gdp/population FROM world WHERE area > !

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

Programming with SQL

David Dye. Extract, Transform, Load

Managing Objects with Data Dictionary Views. Copyright 2006, Oracle. All rights reserved.

DBMS Questions. 3.) For which two constraints are indexes created when the constraint is added?

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

Geodatabase Programming with SQL

5.1 Database Schema Schema Generation in SQL

Access Tutorial 2 Building a Database and Defining Table Relationships

B.1 Database Design and Definition

DBMS / Business Intelligence, SQL Server

Migrate Topaz databases from One Server to Another

Oracle SQL. Course Summary. Duration. Objectives

Information Technology NVEQ Level 2 Class X IT207-NQ2012-Database Development (Basic) Student s Handbook

Unit Storage Structures 1. Storage Structures. Unit 4.3

Using the SQL Procedure

Database Query 1: SQL Basics

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

Knocker main application User manual

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

DIPLOMA IN WEBDEVELOPMENT

Schema Evolution in SQL-99 and Commercial (Object-)Relational DBMS

Lecture 6. SQL, Logical DB Design

DATABASE MANAGEMENT SYSTEMS

Teach Yourself InterBase

Outline. SAS-seminar Proc SQL, the pass-through facility. What is SQL? What is a database? What is Proc SQL? What is SQL and what is a database

Specialized Programme on Web Application Development using Open Source Tools

Would-be system and database administrators. PREREQUISITES: At least 6 months experience with a Windows operating system.

The Relational Model. Why Study the Relational Model?

Topics. Database Essential Concepts. What s s a Good Database System? Using Database Software. Using Database Software. Types of Database Programs

Oracle Database 10g: Introduction to SQL

Information Systems SQL. Nikolaj Popov

3.GETTING STARTED WITH ORACLE8i

SQL: Queries, Programming, Triggers

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

Microsoft Office 2010

Advance DBMS. Structured Query Language (SQL)

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Financial Data Access with SQL, Excel & VBA

Administering a Microsoft SQL Server 2000 Database

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

P_Id LastName FirstName Address City 1 Kumari Mounitha VPura Bangalore 2 Kumar Pranav Yelhanka Bangalore 3 Gubbi Sharan Hebbal Tumkur

Chancery SMS Database Split

Maintaining Stored Procedures in Database Application

Unit 10: Microsoft Access Queries

Instant SQL Programming

Oracle Database: SQL and PL/SQL Fundamentals NEW

Netezza Basics Class Outline

There are five fields or columns, with names and types as shown above.

ATTACHMENT 6 SQL Server 2012 Programming Standards

Workflow Templates Library

Using the Query Analyzer

SQL Server Training Course Content

DB2 - DATABASE SECURITY

IENG2004 Industrial Database and Systems Design. Microsoft Access I. What is Microsoft Access? Architecture of Microsoft Access

DATABASE SECURITY, INTEGRITY AND RECOVERY

How To Create A Table In Sql (Ahem)

In this session, we use the table ZZTELE with approx. 115,000 records for the examples. The primary key is defined on the columns NAME,VORNAME,STR

A table is a collection of related data entries and it consists of columns and rows.

Database Security. The Need for Database Security

Chapter 2: Security in DB2

ORACLE 10g Lab Guide

Administering a Microsoft SQL Server 2000 Database

ICAB4136B Use structured query language to create database structures and manipulate data

W I S E. SQL Server 2008/2008 R2 Advanced DBA Performance & WISE LTD.

Oracle Database: SQL and PL/SQL Fundamentals

SQL Server Replication Guide

SQL: Queries, Programming, Triggers

Introduction to Triggers using SQL

Extracting META information from Interbase/Firebird SQL (INFORMATION_SCHEMA)

C HAPTER 4 INTRODUCTION. Relational Databases FILE VS. DATABASES FILE VS. DATABASES

Introducción a las bases de datos SQL Libro de referencia

SQL Server. 1. What is RDBMS?

Question 1. Relational Data Model [17 marks] Question 2. SQL and Relational Algebra [31 marks]

Introduction to SQL ( )

1.264 Lecture 15. SQL transactions, security, indexes

Oracle 12c Recovering a lost /corrupted table from RMAN Backup after user error or application issue

2. Oracle SQL*PLUS Winter Some SQL Commands. To connect to a CS server, do:

Transcription:

CSI 2132 Lab 3 More on SQL 1 Outline Destroying and Altering Relations DROP TABLE ALTER TABLE SELECT Exercise: Inserting more data into previous tables Single-table queries Multiple-table queries 2 1

Destroying and Altering Relations The Command DROP TABLE destroys the table and deletes all records on that relation. Usage: DROP TABLE TableName The command ALTER TABLE allows us to make several modifications to a table we have created before. We can add/drop columns and constraints, rename table name, columns and do much more (Check the PostgreSQL manual) 3 Altering Table (Cont d) Adding a column to an already created table. ALTER TABLE TableName ADD COLUMN ColumnName ColumnType; We can also add a column with an additional integrity constraint. ALTER TABLE TableName ADD COLUMN ColumnName ColumnType CHECK ( Constraint ); 4 2

Your tasks Open the query tool. By using ALTER TABLE as described in previous slide, do the following: 1. Add Country column to Artist table (say, with the type VARCHAR(20)) 2. Add a Rating column to the Customer table, with the following check constraint: the rating value has to be BETWEEN 1 AND 10. 5 More on SELECT statements The simple SELECT clause that we have seen in the previous lab can be extended by adding more clauses. GROUP BY: Groups all resulting rows of our query in terms of one or more attributes with this clause. HAVING: Group qualification is specified here. Groups which satisfy this qualification will be displayed. ORDER BY: We can sort the data based on one or more attributes with this clause. 6 3

More on SELECT statements SELECT [ DISTINCT ] select-list FROM from-list WHERE record-qualification GROUP BY grouping-list HAVING group-qualification 7 Your tasks You will insert more data into the Art database we just created last week. You will delete rows from a table Then, you ll code queries involving single and multiple tables. 8 4

Insertions Insert the following into the Artist table ('Leonardo','Florence','Renaissance','04-15-1452','Italy') ('Michelangelo','Arezzo','Renaissance','03-06-1475','Italy') ('Josefa','Seville','Baroque','09-09-1630','Spain') ('Hans Hofmann','Weisenburg','Modern','02-17- 1966','Germany') ('John,'San Francisco','Modern','02-17-1920','USA') 9 Insertions Insert the following into Artwork table ('Waves', 2000, null, 4000.00, 'John') ('Three Musicians', 1921,'Modern',11000.00,'Picasso') Insert the following into Customer table (1,'Emre','Preston',20000.00,5) (2,'Saeid',null,40000.00,6) Insert the following into LikeArtist table (1,'Picasso') (2,'Picasso') and (2, Leonardo') 10 5

We can delete certain rows satisfying a condition from a table with the DELETE command. Condition has the same format as that in the WHERE clause of a SELECT query. If you omit the WHERE clause, all records will be permanently deleted. Syntax DELETE FROM TableName WHERE Condition 11 Suppose the artist Smith moved to another gallery, and we have to remove him from our database. Write a DELETE query to remove him from the DB. Note that Artwork table has a foreign key to the Artist table. Two ways of doing this: Manual: We remove all records in all tables related to the Smith record in Artist. Automated: We remove Smith from Artist and all related information is removed by the DBMS. To try them both, we need to backup and restore the database. 12 6

Backup: A snapshot of the database (including data and structure) at any point in time. Generates a data file *.backup that you save on disk. Restore: Uses a previously generated backup file to bring the database to a certain state in time. Before restore, we need to: Either remove all tables (DROP TABLE) Or remove the table data (DELETE FROM ) 13 The manual way: (perform a backup first) If no backup before deleting Smith, then every erased record cannot be recovered later on. They have to be manually generated again. Delete all art works related to Smith. Then delete Smith from the artist list. 14 7

The automatic way Remove all tables with DROP TABLE statement. Perform restore using the backup file. The Smith author should be there again. Select Properties on the artwork table Remove the existing foreign key constraint Create a new foreign key constraint but now selecting the Cascade option for UPDATE and DELETE operations. Delete Smith from the author list. All Smith s artworks are automatically deleted. 15 Write SQL queries for the following a) List the names and birthplaces of all Artists. b) List the title and price of all Artworks that were painted after 1600. c) List the title and type of all Artworks that was either painted in 2000 or was painted by Picasso. d) List the names and birthplaces of all Artists who were born between 1880 and 1930. (HINT: EXTRACT(YEAR FROM Dateofbirth) gives you the year from a DATE attribute) e) List the names and country of birth of all Artists whose painting style are Modern, Baroque or Renaissance. (HINT: Use the IN keyword). f) List all details of the Artworks in the database, ordered by Title. 16 8

Write SQL queries for the following Note that these two queries involve more than one table. 1. List the names and customer ids of all customers who like Picasso. 2. List the names of all customers who like Artists from the Renaissance style and having an amount larger than 30000. 17 End of the lab If the time was not enough, please complete today s lab before next lab, since we might use the data that we have created in previous labs. 18 9