SQL DDL. Intro SQL CREATE TABLE ALTER TABLE Data types Service-based database in Visual Studio Database in PHPMyAdmin

Similar documents
SQL grouping, views & modifying data

Chapter 6. SQL: SubQueries

Objective. The goal is to review material covered in Chapters 1-5. Do the following questions from the book.

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

SQL Data Definition and Data Manipulation Languages (DDL and DML)

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Lecture 03. Spring 2018 Borough of Manhattan Community College

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE

CS317 File and Database Systems

Chapter 1 SQL and Data

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

Lecture 03. Fall 2017 Borough of Manhattan Community College

SQL Data Definition Language: Create and Change the Database Ray Lockwood

SQL DATA DEFINITION LANGUAGE

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Data Base Concepts. Course Guide 2

SQL DATA DEFINITION LANGUAGE

GlobAl EDITION. Database Concepts SEVENTH EDITION. David M. Kroenke David J. Auer

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture 1: Relational Databases

Introduction to the Structured Query Language [ SQL ] (Significant Concepts)

Introduction to Data Management CSE 344. Lecture 2: Data Models

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

SQL DATA DEFINITION LANGUAGE

Today Learning outcomes LO2

6.9 List the names and addresses of all guests in London, alphabetically ordered by name.

G64DBS Database Systems. Lecture 7 SQL SELECT. The Data Dictionary. Data Dictionaries. Different Sections of SQL (DDL) Different Sections of SQL (DCL)

PART III SOLUTIONS TO REVIEW QUESTIONS AND EXERCISES

INFORMATION TECHNOLOGY NOTES

DATABASES SQL INFOTEK SOLUTIONS TEAM

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Databases CSCI 201 Principles of Software Development

MySQL Introduction. By Prof. B.A.Khivsara

SQL: Data Definition Language

Overview. Data Integrity. Three basic types of data integrity. Integrity implementation and enforcement. Database constraints Transaction Trigger

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Chapter 1 An introduction to relational databases and SQL

Introduction. Example Databases

PASS4TEST. IT Certification Guaranteed, The Easy Way! We offer free update service for one year

1.8 Database and data Data Definition Language (DDL) and Data Manipulation Language (DML)

Chapter 8: Working With Databases & Tables

1 P a g e D a t a b a s e o l d p a p e r b y w a s i m

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

Oracle Create Table Foreign Key On Delete No

Database Systems CSE 303. Lecture 02

Database Systems CSE 303. Lecture 02

CGS 3066: Spring 2017 SQL Reference

Chapter 3. Introduction to relational databases and MySQL. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C3

Relational databases and SQL

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Kaotii.

IT360: Applied Database Systems. SQL: Structured Query Language DDL and DML (w/o SELECT) (Chapter 7 in Kroenke) SQL: Data Definition Language

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

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

Full file at

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

Oral Questions and Answers (DBMS LAB) Questions & Answers- DBMS

Using Relational Databases for Digital Research

Part I: Structured Data

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

COMP283-Lecture 6 Applied Database Management

1. Data Model, Categories, Schemas and Instances. Outline

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

Creating Tables, Defining Constraints. Rose-Hulman Institute of Technology Curt Clifton

CSE 344 JANUARY 5 TH INTRO TO THE RELATIONAL DATABASE

Database Systems. phpmyadmin Tutorial

Lab # 3 Hands-On. DML Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

Chapter 4: SQL Basics

Lecture 2: Introduction to SQL

Lecture 5: SQL s Data Definition Language

Lecture 04: SQL. Wednesday, October 4, 2006

Data Definition and Data Manipulation. Lecture 5: SQL s Data Definition Language CS1106/CS5021/CS6503 Introduction to Relational Databases

Module 3 MySQL Database. Database Management System

CS2300: File Structures and Introduction to Database Systems

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

SQL (and MySQL) Useful things I have learnt, borrowed and stolen

Instructor: Craig Duckett. Lecture 02: Thursday, March 29 th, 2018 SQL Basics and SELECT, FROM, WHERE

Accessing databases in Java using JDBC

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

Physical Design of Relational Databases

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

! An organized collection of data. ! Can easily be accessed, managed, and updated. ! Data are organized as a set of tables.

CMPT 354: Database System I. Lecture 2. Relational Model

SQL Server and SQL Structured Query Language

Who, where, when. Database Management Systems (LIX022B05) Literature. Evaluation. Lab Sessions. About this course. After this course...

Introduction to Databases CSE 414. Lecture 2: Data Models

Q1: Consider the following Company database, describing the data of customer s orders: ORDERS ( OrderNo, Month, Cid, Aid, Pid, OrderedQuantity, Cost )

MTAT Introduction to Databases

Database and MySQL Temasek Polytechnic

Server-side Web Programming

SQL Commands & Mongo DB New Syllabus

Working with Databases and Java

MySQL. Prof.Sushila Aghav

doc. RNDr. Tomáš Skopal, Ph.D. RNDr. Michal Kopecký, Ph.D.


How to use SQL to create a database

IELM 511 Information Systems Design Labs 5 and 6. DB creation and Population

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

Introduction to MySQL. Database Systems

Transcription:

SQL DDL Intro SQL CREATE TABLE ALTER TABLE Data types Service-based database in Visual Studio Database in PHPMyAdmin Steen Jensen, autumn 2017

Languages Languages for relational DBMSs are: SQL QBE Structured Query Language Query by example A graphical frontent to SQL Will convert into corresponding SQL

SQL Two main parts: Data Definition Language (DDL) To define the database structure Data Manipulation Language (DML) For retrieving and updating the content (the data) Two ways of using SQL: Interactive Embedded in a procedural language as Java, C#, PHP etc.

SQL ISO standard A standard database language There are many versions of SQL (aka DBMS: database management systems): SQL Server, DB2, MySQL, Oracle, Informix, Ingres etc. Nearly every database is based on standard SQL, but dialects exist (e.g. in Oracle) SQL Server uses T-SQL Non-procedural language Specify What and not How

Data definition Language(DDL) Statements that are used to define the database structure: CREATE - to create tables in the database ALTER - alters the structure of tables in the database DROP - deletes tables in the database We look at create table Can be done through a GUI interface or through a so-called query window

HotelDB HOTEL:(Hotel_No, Name, Address) ROOM:(Room_No, Hotel_No, Types, Price) BOOKING:( BookingID, Hotel_No, Guest_No, Date_From, Date_To, Room_No) GUEST:(Guest_No, Name, Address)

Create Database CREATE DATABASE HotelDb Creates a database.

Creating tables Creating a table (simplified): CREATE TABLE Hotel (<field definitions> PRIMARY KEY <field list>) Complete syntax for creating a new table is rather complicated See description of syntax at e.g. https://msdn.microsoft.com/en-us/library/ms174979.aspx

Creating tables Each field definition contains The field name The data type for the field Can the field be NULL or not Must the field be unique Does the field have a default value

Create Table Hotel SQL Server: CREATE TABLE Hotel( Hotel_No int IDENTITY(1,1) NOT NULL PRIMARY KEY, Name VARCHAR(30) NOT NULL, Address VARCHAR(50) NOT NULL ); MySQL: CREATE TABLE Hotel( Hotel_No int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(30) NOT NULL, Address VARCHAR(50) NOT NULL );

Creating Table Room CREATE TABLE Room( Room_No int NOT NULL, Hotel_No int NOT NULL, Types CHAR(1) DEFAULT 'S', Price FLOAT, CONSTRAINT checktype CHECK (Types IN ('D','F','S') OR Types IS NULL), CONSTRAINT checkprice CHECK (price BETWEEN 0 AND 9999), ); FOREIGN KEY (Hotel_No) REFERENCES Hotel (Hotel_No) ON UPDATE CASCADE ON DELETE NO ACTION, Primary KEY (Room_No, Hotel_No)

Creating table Guest CREATE TABLE Guest ( Guest_No int NOT NULL PRIMARY KEY, Name VARCHAR(30) NOT NULL, Address VARCHAR(50) NOT NULL );

Creating Table Booking CREATE TABLE Booking( Booking_id int IDENTITY(1,1) NOT NULL PRIMARY KEY, Hotel_No int NOT NULL, Guest_No int NOT NULL, Date_From DATE NOT NULL, Date_To DATE NOT NULL, Room_No int NOT NULL, FOREIGN KEY(Guest_No) REFERENCES Guest(Guest_No), FOREIGN KEY(Room_No, Hotel_No) REFERENCES Room(Room_No, Hotel_No) ); Above syntax is for SQL Server see slide 10 for MySQL syntax

Alter Table ALTER TABLE Booking ADD CONSTRAINT incorrect_dates CHECK ((Date_To > Date_From) AND (Date_From <= '2014-01-01')); NB! The above is only an example don t implement this in your Booking table you won t be able to make any new bookings!!!

Data types for various DBMS http://www.w3schools.com/sql/sql_datatypes.asp

Service-based database in Visual Studio To have a place to experiment with SQL (a sand box ) you must make a so-called service-based database Make a new project (WPF, Windows form, Console) not Windows Store app Right-click on the project and choose Add new item Choose Data + Service-based database The database can now be seen in the Solution explorer window Double-click on the database, and the Server explorer window is shown You can now work with the database ( the right-click method or the Query window method )

Database in PHPMyAdmin Logon to phpmyadmin on your domain in One.com you can find it under the control panel Normally PHPMyadmin will only support one database in the free version The database is selected, and you can start creating tables Tables can be created either clicking the Create table button or by writing a CREATE TABLE command in the SQL window New records can be inserted either through the Insert window or by writing an INSERT INTO command in the SQL window

Exercise SQL DDL queries Create a database to hold all the hotel related SQL tables. Experiment creating a demo table (no connection with hotel case) Create the necessary tables for the hotel case by copy/pasting the content of the file SQLCreateHotel.sql into a query window and executing it Insert data into the created tables by by copy/pasting the content of the file SQLInsertHotel.sql into a query window and executing it