Data types. Numbers! integer, smallint, bigint! real, double precision! Characters and Strings!

Similar documents
How To Create A Table In Sql (Ahem)

CSC 443 Data Base Management Systems. Basic SQL

Porting from Oracle to PostgreSQL

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

4 Logical Design : RDM Schema Definition with SQL / DDL

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

SQL Data Definition. Database Systems Lecture 5 Natasha Alechina

Section of DBMS Selection & Evaluation Questionnaire

Introduction to Microsoft Jet SQL

IT2304: Database Systems 1 (DBS 1)

!"# $ %& '( ! %& $ ' &)* + ! * $, $ (, ( '! -,) (# *&23. mysql> select * from from clienti;

Using SQL Server Management Studio

IT2305 Database Systems I (Compulsory)

Database Migration from MySQL to RDM Server

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

Lecture 6. SQL, Logical DB Design

SQL Server An Overview

Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

SQL NULL s, Constraints, Triggers

A Brief Introduction to MySQL

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

Data Modeling. Database Systems: The Complete Book Ch ,

2/3/04 Doc 7 SQL Part 1 slide # 1

not at all a manual simply a quick how-to-do guide

Comparison of Open Source RDBMS

Databases What the Specification Says

Review: Participation Constraints

Creating Database Tables in Microsoft SQL Server

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

MS ACCESS DATABASE DATA TYPES

Advance DBMS. Structured Query Language (SQL)

Programming with SQL

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague

Physical Design. Meeting the needs of the users is the gold standard against which we measure our success in creating a database.

4 Simple Database Features

Oracle Database 10g Express

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

The Relational Model. Why Study the Relational Model?

Online shopping cart. Tarik Guelzim Graduate school of computer science at Monmouth University. CS517 Database management systems Project 2

Instant SQL Programming

Information Systems SQL. Nikolaj Popov

The release notes provide details of enhancements and features in Cloudera ODBC Driver for Impala , as well as the version history.

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

Fundamentals of Database Design

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

C++ Wrapper Library for Firebird Embedded SQL

Summary on Chapter 4 Basic SQL

Once the schema has been designed, it can be implemented in the RDBMS.

Working with DB2 UDB objects

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

SQL. Short introduction

Linas Virbalas Continuent, Inc.

Extracting META information from Interbase/Firebird SQL (INFORMATION_SCHEMA)

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

Introduction to SQL ( )

Database Query 1: SQL Basics

DbSchema Tutorial with Introduction in SQL Databases

SQL Server Table Design - Best Practices

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

EECS 647: Introduction to Database Systems


Below is a table called raw_search_log containing details of search queries. user_id INTEGER ID of the user that made the search.

The Relational Data Model: Structure

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

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

Information Systems Modelling Information Systems I: Databases

5.1 Database Schema Schema Generation in SQL

Table and field properties Tables and fields also have properties that you can set to control their characteristics or behavior.

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

Principles of Database Management Systems. Overview. Principles of Data Layout. Topic for today. "Executive Summary": here.

Introduction This document s purpose is to define Microsoft SQL server database design standards.

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Language Reference Guide

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

Guide to the Superbase. ODBC Driver. By Superbase Developers plc

Firebird. Embedded SQL Guide for RM/Cobol

Database Design Overview. Conceptual Design ER Model. Entities and Entity Sets. Entity Set Representation. Keys

Microsoft SQL connection to Sysmac NJ Quick Start Guide

Database 2 Lecture I. Alessandro Artale

sqlite driver manual

Introduction to Database. Systems HANS- PETTER HALVORSEN,

Create a Database Driven Application

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

5. CHANGING STRUCTURE AND DATA

Organization of Records in Blocks

ERserver. DB2 Universal Database for iseries SQL Programming with Host Languages. iseries. Version 5

Databasesystemer, forår 2005 IT Universitetet i København. Forelæsning 3: Business rules, constraints & triggers. 3. marts 2005

AUTHENTICATION... 2 Step 1:Set up your LDAP server... 2 Step 2: Set up your username... 4 WRITEBACK REPORT... 8 Step 1: Table structures...

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

SQL 2: GETTING INFORMATION INTO A DATABASE. MIS2502 Data Analytics

Guide to SQL Programming: SQL:1999 and Oracle Rdb V7.1

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

Conventional Files versus the Database. Files versus Database. Pros and Cons of Conventional Files. Pros and Cons of Databases. Fields (continued)

3. Relational Model and Relational Algebra

INFORMATION BROCHURE Certificate Course in Web Design Using PHP/MySQL

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

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

SQL Server Database Coding Standards and Guidelines

Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology

Transcription:

DDL

Data types Numbers integer, smallint, bigint real, double precision Characters and Strings char(n): up to n characters, blank padded varchar(n): up to n characters, variable length Date & Time date (e.g., 2006-03-01 for the 1st of March) time (e.g., 12:30:00 ) timestamp: date&time together (e.g., 2006-03-01 12:30:00 ) interval: quantity unit (e.g., 2 hours ) Network cidr: network address, with netmask (e.g, 192.168.2.0/24 ) inet: host address, with optional netmask (e.g., 192.168.2.1/24 ) macaddr: MAC address (e.g., 00:0A:19:67:45:12 ) Bytes & bits bytea for raw bytes bit(n), varying bit(n): strings of bits Geometric types, boolean types, etc. (refer to the manual) Possible to create your own types CREATE TYPE 2

Toolbox: managing tables CREATE TABLE person( name varchar(20), age integer, address varchar(30), id serial, PRIMARY KEY(id) ); DROP TABLE person; DROP TABLE person CASCADE; drops also objects depending on this one (careful) ALTER TABLE person ADD CONSTRAINT will see this later on 3

Toolbox: managing tables CREATE TABLE person( name varchar(20), age integer, address varchar(30), id serial, PRIMARY KEY(id), UNIQUE (name) ); keyword UNIQUE candidate keys attributes whose values MUST be unique, although not used to index the table entries (like the PRIMARY KEY) 4

Toolbox: managing tables CREATE TABLE person( name varchar(20) DEFAULT pippo, age integer, address varchar(30), id serial, PRIMARY KEY(id) ); 5

Toolbox: managing table content INSERT INTO person(name,age,address,id) VALUES ( gianni,23, Via Battelli, 5,1); correspondence between the names of the fields and the values UPDATE person P SET name= giovanni WHERE P.name= gianni ; the table is scanned row by row if the row satisfies the WHERE clause the update in SET is applied DELETE FROM person P WHERE P.addr= Via di Pratale,6 ; CREATE TABLE person( name varchar(20), age integer, address varchar(30), id serial, primary key (id) ); DELETE FROM person; removes all tuples from person and all tuples in related tables with cascading behaviour (careful) 6

Toolbox: serial&sequences CREATE TABLE person( name varchar(20), age integer, address varchar(30), id serial, PRIMARY KEY(id) ); Used to automatically assign integer values from a sequence Starting from 1 INSERT INTO person(name,age,address,id) VALUES ( gianni,23, Via Battelli, 5,1); INSERT INTO person(name,age,address) VALUES ( gianni,23, Via Battelli, 5 ); Equivalent (in an empty table) 7

Toolbox: serial&sequences Conflicts INSERT INTO person(name,age,address) VALUES ( gianni,23, Via Battelli, 5 ); INSERT INTO person(name,age,address,id) VALUES ( andrea,40, Via Roma, 30,2); INSERT INTO person(name,age,address) VALUES ( antonio,25, Via di Pratale, 4 ); Not allowed (conflict on id=2) Next value = 1 Next value = 2 Next value = 2 CREATE TABLE person( name varchar(20), age integer, address varchar(30), id serial, primary key (id) ); 8

Toolbox: foreign keys buildings CREATE TABLE buildings( ); addr varchar(30), addr CREATE TABLE person( person FOREIGN KEY (address) REFERENCES buildings(addr) ON UPDATE CASCADE ON DELETE SET NULL, ); Get the same result by altering the table definition after creating it ALTER TABLE person ADD CONSTRAINT person_fk FOREIGN KEY (address) REFERENCES buildings(addr) ON UPDATE CASCADE ON DELETE SET NULL; address 9

Toolbox: constraints CREATE TABLE person( CONSTRAINT check_age CHECK (age>18), ); Check out operators for non-trivial types (look at the manual) Network addresses, geometric, date, CREATE TABLE person( name varchar(20), ageinteger, address varchar(30), id serial, primary key (id) ); 10

Translating relationships Entity A rel Entity B att4 att3 att5 att1 att2 In general (for any kind of relationship) Take primary keys of entities and build the primary key of the relationship Additional attributes for the relationship s attributes CREATE TABLE rel( rel_att3 <type> REFERENCES entitya(att3), rel_att2 <type> REFERENCES entityb(att2), att5 <type>, PRIMARY KEY (rel_att3,rel_att2) ); 11

Translating 1-to-many relationships Entity A rel Entity B att4 att3 att5 att1 att2 In case of 1-to-many relationships A tuple of Entity B ( many side ) can uniquely identify the corresponding tuple of Entity A ( 1 side ) son #1 mother son #2 son #3 12

Translating 1-to-many relationships Entity A rel Entity B att4 att3 att5 att1 att2 In case of 1-to-many relationships A tuple of Entity B ( many side ) can uniquely identify the corresponding tuple of Entity A ( 1 side ) CREATE TABLE entityb( att1 <type>, att2 <type> PRIMARY KEY, pippo <type> REFERENCES entitya(att3), att5 <type> ); pippo does not necessarily belong to the initial set of Entity B attributes it may be added just to translate the relationship att5 is added to EntityB only for the purpose of incorporating the attributes of the relationship 13

Translating 1-to-many relationships mother rel son id mother_id CREATE TABLE mother( id PRIMARY KEY, ); CREATE TABLE son(..., mother_id <type> REFERENCES mother(id),... ); 14

Translating 1-to-many relationships Entity A rel Entity B att4 att3 att5 att1 att2 With total participation (thick arrow) CREATE TABLE entityb( att1 <type>, att2 <type> PRIMARY KEY, pippo <type> NOT NULL REFERENCES entitya(att3), att5 <type> ); 15

Translating 1-to-many relationships mother rel son id mother_id CREATE TABLE mother( id PRIMARY KEY, ); CREATE TABLE son(..., mother_id <type> NOT NULL REFERENCES mother(id),... ); 14

Exercise: ER gw addr mac ip_addr admin net admin host id net topology host 1-to-many relationship Total participation of host How should topology be represented? Do we need a distinct table? 17

Cross foreign keys from page 72, slightly modified Every student has a main course Every course has a representative student CREATE TABLE students( stud_id INTEGER PRIMARY KEY, name VARCHAR(20), main_course INTEGER REFERENCES courses(c_id), ); CREATE TABLE courses( c_id INTEGER PRIMARY KEY, name VARCHAR(20), repr_stud INTEGER REFERENCES students(stud_id), ); Two problems PostrgeSQL does not allow to use unknown table ids How should one insert rows starting from an empty table? 18

Cross foreign keys Table definitions: use ALTER TABLE: CREATE TABLE students( stud_id INTEGER PRIMARY KEY, name VARCHAR(20), main_course INTEGER, ); CREATE TABLE courses( c_id INTEGER PRIMARY KEY, name VARCHAR(20), courses students stud_id repr_stud repr_stud INTEGER REFERENCES students(stud_id), ); 19

Cross foreign keys Table definitions: use ALTER TABLE: CREATE TABLE students( stud_id INTEGER PRIMARY KEY, name VARCHAR(20), main_course INTEGER, ); CREATE TABLE courses( c_id INTEGER PRIMARY KEY, name VARCHAR(20), courses students stud_id main_ course repr_stud INTEGER REFERENCES students(stud_id), ); ALTER TABLE students ADD CONSTRAINT stud_course FOREIGN KEY (main_course) REFERENCES courses(c_id); repr_stud c_id 20

Cross foreign keys By default each SQL statement is a transaction DBMS checks consistency at the end of each transaction INSERT INTO students VALUES (1, gianni,,15, ); is not allowed if course 15 does not already exist INSERT INTO courses VALUES (15, dbms,,1, ); is not allowed if student 1 does not already exist How to initialise the DB? Explicitly define the transaction boundaries: Begin; INSERT INTO students VALUES (1, gianni,,15, ); INSERT INTO courses VALUES (15, dbms,,1, ); commit; transaction 21

Cross foreign keys Explicitly define the transaction boundaries:" Begin; INSERT INTO students VALUES (1, gianni,,15, ); INSERT INTO courses VALUES (15, dbms,,1, ); commit; Force to check at commit time, with regard to the whole transaction (not with regard to the single statements) CREATE TABLE courses( repr_stud INTEGER REFERNCES students(stud_id) DEFERRABLE INITIALLY DEFERRED, ); ALTER TABLE students ADD CONSTRAINT foreign key (main_course) REFERENCES courses(c_id) DEFERRABLE INITIALLY DEFERRED;" 22