Database commands : DDL

Slides:



Advertisements
Similar presentations
Session 2Introduction to Database Technology Data Types and Table Creation.
Advertisements

Database Chapters.
MySQL. To start go to Login details: login: labuser password:macimd15 – There.
SQL’s Data Definition Language (DDL) n DDL statements define, modify and remove objects from data dictionary tables maintained by the DBMS n Whenever you.
Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands.
MySQL-Database Teppo Räisänen Oulu University of Applied Sciences School of Business and Information Management.
Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
DT211 Stage 2 Databases Lab 1. Get to know SQL Server SQL server has 2 parts: –A client, running on your machine, in the lab. You access the database.
Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
Structured Query Language SQL: An Introduction. SQL (Pronounced S.Q.L) The standard user and application program interface to a relational database is.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
Oracle Data Definition Language (DDL)
Copyright © Curt Hill SQL The Data Definition Language.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
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.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Chapter 4 Introduction to MySQL. MySQL “the world’s most popular open-source database application” “commonly used with PHP”
Introduction to MySQL Lab no. 10 Advance Database Management System.
CSC 2720 Building Web Applications Database and SQL.
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
Advanced Web 2012 Lecture 3 Sean Costain What is a Database? Sean Costain 2012 A database is a structured way of dealing with structured information.
# 1# 1 Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? CS 105.
Sql DDL queries CS 260 Database Systems.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
1 MySQL and SQL. 2 Topics  Introducing Relational Databases  Terminology  Managing Databases MySQL and SQL.
Class 3Intro to Databases Class 4 Simple Example of a Database We’re going to build a simple example of a database, which will allow us to register users.
Relational Databases and MySQL. Relational Databases Relational databases model data by storing rows and columns in tables. The power of the relational.
Digital recordkeeping and preservation I
Digital recordkeeping and preservation I
3 A Guide to MySQL.
Fundamentals of DBMS Notes-1.
Digital recordkeeping and preservation I
CS 3630 Database Design and Implementation
Chapter 5 Introduction to SQL.
CS320 Web and Internet Programming SQL and MySQL
Database to XML extractions
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Digital recordkeeping and preservation I
Data Definition and Data Types
MySQL-Database Jouni Juntunen Oulu University of Applied Sciences
SQL and SQL*Plus Interaction
CS1222 Using Relational Databases and SQL
Designing Tables for a Database System
ORACLE SQL Developer & SQLPLUS Statements
CS4222 Principles of Database System
CS1222 Using Relational Databases and SQL
SQL data definition using Oracle
Creating Tables & Inserting Values Using SQL
PT2520 Unit 5: Physical Design
CS122 Using Relational Databases and SQL
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter 2: Creating And Modifying Database Tables
CS3220 Web and Internet Programming SQL and MySQL
CS1222 Using Relational Databases and SQL
Chapter 4 Introduction to MySQL.
MIS2502: Data Analytics SQL 4– Putting Information Into a Database
MySQL Database System Installation Overview SQL summary
SQL Basics BCHB697.
CS3220 Web and Internet Programming SQL and MySQL
Structured Query Language Path from Unorganized to Organized….
CS122 Using Relational Databases and SQL
Database Instructor: Bei Kang.
SQL (Structured Query Language)
Presentation transcript:

Database commands : DDL ARK2100 Digital recordkeeping and preservation I 2016 Thomas Sødring thomas.sodring@hioa.no P48-R407 67238287

So far We have explored The concept of databases The relational model and related issues ER-modelling Conceptual, logical and physical We should now have a basic understanding of what this means are how this related to eachother Now we will look at how to implement a model in the database Inserting, updating and retrieving data from a database This is a slow process so we have to be patient!!

We will learn about Datatypes What they are, why they are important and how to choose them The DDL commands CREATE, DROP, ALTER, TRUNCATE

First we need access to MySQL Fisrt you have to create a database account https://bibin.hioa.no/sql/ Click on 'Opprett MySQL-konto' Means Create mysql account! Then you can log on https://bibin.hioa.no/phpmyadmin/ If you forget or have problems with your password Click on 'Bytt MySQL-passord' Means Change MySQL password

Create database account Your student number Check the format! Password you want VERY IMPORTANT!! Do not use the password you use to log on fronter / HiOA eMail or HiOA computers! Do not use a password that you use to log on to gmail / facebook etc Choose something like '_23Frank'

https://bibin.hioa.no/phpmyadmin/

DBMS Schema A Schema B r1 r2 r3 r1 r2 r5 r3 r4 r6 r7 A database management system can contain many schemas. People often call a schema a database and use the terms interchangeably.

Data is stored in a relation (table) Cars RegistrationNr ChassisNr Colour Manufacturer Model LH12984 10946534 Red Volkswagen Golf DK23491 9648573 Blue Toyota Yaris BP12349 5523840 Green Skoda Fabia ZT97495 2643923 White Seat Leon

Relation, Attributtes, Tuples RegistrationNr ChassisNr Colour Manufacturer Model Cars LH12984 10946534 Red Volkswagen Golf 4 Tuples DK23491 9648573 Blue Toyota Yaris BP12349 5523840 Green Skoda Fabia ZT97495 2643923 White Seat Leon 5 Attributtes

Data Definition Language (DDL) Data Definition Language (DDL) is a database language that can be used to define a database schema A database schema is a metadata description of one or more tables (relations) in a database r1 r2 r3 r5 r4 r6 r7 Schema A Schema B

Data Definition Language (DDL) DDL is also used to define attribute names (columns) data types relationships to other tables RegistrationNr ChassisNr Colour Manufacturer Model Cars CREATE TABLE Car ( RegistrationNr VARCHAR(20), ChassisNr VARCHAR(20), Manufacturer VARCHAR(30), Model VARCHAR(20), Colour VARCHAR(20), PRIMARY KEY (RegistrationNr) )

Datatypes Databases are very fussy about the knowing what kind of data they are storing Each column needs its datatype set The data type applies to all values in the attribute These can for example be: String, number, date, time All DBMS supports a set of datatypes that can be set on attributes (columns) We look at the data types of MySQL RegistrationNr ChassisNr Colour Manufacturer Model Cars

Why datatypes Efficiency, to reduce storage space and compatibility with programming languages Also helps error handling Efficiency Helps optimise storage of data for querying Items that consist of numbers should be configured as integers (INT) if we know that they will be used for calculations (e.g. average, count, summation) Storage reduction You do not want your database using space that is not required eg country codes consisting of a maximum of 2 letters (NO, SE, DK, FI, D) CHAR (2) Programming languages use datatypes

Datatypes (string) Datatype Type Description CHAR () Fixed A field of fixed length that can consist of between 0 og 255 characters. The length can be specified VARCHAR () Variable A field of variable length that can consist of between 0 og 255 characters. The length can be specified TINYTEXT A field of variable length that can consist of a maximum of 255 characters TEXT A field of variable length that can consist of a maximum of 65535 characters MEDIUMTEXT A field of variable length that can consist of a maximum of 16777215 characters LONGTEXT A field of variable length that can consist of a maximum of 4294967295 characters

Datatypes (BLOB) Binary Large OBject is a field that can be used to store binary data for example a MS Word (.doc) file When the Word file is stored on disk, you cannot read it You need a program to decipher the data and display it for you Datatype Description TINYBLOB A field of variable length that can consist of a maximum of 255 characters BLOB A field of variable length that can consist of a maximum of 65535 characters MEDIUMBLOB A field of variable length that can consist of a maximum of 16777215 characters LARGEBLOB A field of variable length that can consist of a maximum of 4294967295 characters

Binary data Binary data is data encoded such that only a computer can read it A human cannot read it Consists of 1's and 0's

Datatypes (whole numbers) Calculations (e.g. average age) are performed faster when an attribute is specified as a number It is important to know what the max and min value of the data type Maximum Value + 1 = Minimum value Datatype Description TINYINT A number between 0 and 255 or -128 and 127 SMALLINT A number between 0 and 65535 or -32768 and 32767 MEDIUMINT INT A number between 0 and 16777215 or -8388608 and 8388607 BIGINT A number between 0 and 18446744073709551615 or -9223372036854775808 and 9223372036854775807

Wrap around https://en.wikipedia.org/wiki/Integer_overflow#mediaviewer/File:Odometer_rollover.jpg

Datatypes (other numbers) Decimal number e.g. 129.7777 uses the following data types FLOAT DOUBLE DECIMAL If you just require to store a yes or no value, you can use a BOOLEAN

Datatypes (date and time) Description DATE A date between the year 1000-01-01 and the year 9999-12-31 DATETIME A date and time between the year 1000-01-01 00:00:00 and the date and time 9999-12-31 23:59:59 TIME Number of hours:minutes:seconds from -838:59:59 til 838:59:59 YEAR[(2|4)] A year value between 1901 to 2155 TIMESTAMP Number of seconds since a given time (point in history)

So far We have again looked at the concept of a DBMS How data is stored in relations Introduced the concept of datatypes We have to know the datatype for every value in the database String, number, date etc Now we are going to work with a DBMS called MySQL https://www.mysql.com/

phpMyAdmin MySQL and phpMyAdmin phpMyAdmin is a program that can be used to access a MySQL DBMS phpMyAdmin r1 r2 r3 Schema A r5 r4 r6 r7 Schema B

https://bibin.hioa.no/phpmyadmin/ Choose the language you wish to work with

Data Definition Language (DDL) DDL Commands CREATE create DROP delete ALTER change TRUNCATE empty

DDL CREATE CREATE can be used to create the following: Schema (database) Relation (table) User

{DATABASE | SCHEMA} [IF NOT EXISTS] db_navn; CREATE Syntax CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_navn;

A syntax explanation MySQL DDL uses the following syntax { } means that you must select an item from a set of elements | Means either or select | insert Choose either select or insert [ ] Means it is optional

CREATE DATABASE s123456_database; Use your student number - not s123456! The database server at HiOA is set up with a requirement that each database name must always start with your student number If you forget studentnummer_ you will get an error!

; semicolon Always use a semicolon after each database statement This allows us to run multiple commands in one go ;

USE command CREATE DATABASE s123456_firstdatabase; You usually have to specify the name of database you are working with The reason for this is that you will have many databases and the DBMS needs to know which one you want to work with CREATE DATABASE s123456_firstdatabase; USE s123456_firstdatabase;

phpMyAdmin

Entering SQL commands

Create a database 1. Write in the DDL command 2. Press 'Go'

Result of create

Choose database 1. Here you will see all your databases 2. Click on the database you wish to work with

Back to a SQL window 1. Back to an SQL window

Ready to go

[attribute properties] ) CREATE (table) CREATE TABLE [tablename] ( [attribute properties] ) Remember to specify which database you are using

Entity examples Car Airport Building RegistrationNr ChassisNr Colour Manufacturer Model Name Name Location Int Code Address Town Country Postnumber Town

Logical model Airport (Name, IntCode, Town, Country) Car (RegistrationNr, ChassisNr, Colour, Manufacturer, Model) Airport (Name, IntCode, Town, Country) Building (Name, Location, Address, Postnumber, Town) All we are missing is the datatypes. Once we have this we can write DDL.

From a logical to a physical model CREATE TABLE Car ( RegistrationNr VARCHAR(20), ChassisNr VARCHAR(20), Manufacturer VARCHAR(30), Model VARCHAR(20), Colour VARCHAR(20), PRIMARY KEY (RegistrationNr) ); CREATE TABLE Airport ( Name VARCHAR(20), IntCode CHAR(3), Town VARCHAR(20), Country VARCHAR(20), PRIMARY KEY (Name) ); CREATE TABLE Building ( Name VARCHAR(20), Location VARCHAR(30), Address VARCHAR(30), Postnumber VARCHAR(4), Town VARCHAR(100), PRIMARY KEY (Name) );

Create Car CREATE TABLE Car ( RegistrationNr VARCHAR(20), ChassisNr VARCHAR(20), Manufacturer VARCHAR(30), Model VARCHAR(20), Colour VARCHAR(20), PRIMARY KEY (RegistrationNr) ); Remember the semicolon Never a comma at the end

DDL for Car 1. Write in the DDL command 2. Press 'Go'

Result of Create Car

Structure of Car

Create Airport CREATE TABLE Airport ( Name VARCHAR(20), IntCode CHAR(3), Town VARCHAR(20), Country VARCHAR(20), PRIMARY KEY (Name) ); Remember the semicolon Never a comma at the end

DDL for Airport 1. Write in the DDL command 2. Press 'Go'

Result of Create Airport

Structure of Airport

Create Building CREATE TABLE Building ( Name VARCHAR(20), Location VARCHAR(30), Address VARCHAR(30), Postnumber VARCHAR(4), Town VARCHAR(100), PRIMARY KEY (Name) ); Remember the semicolon Never a comma at the end

DDL for Building 1. Write in the DDL command 2. Press 'Go'

Result of Create Building

Structure of Building

Distributor distributorID name address postnr town Contributor Film filmId personId firstname surname dob deathDate title year length manufacturer Participation distributorID filmId personId role customerNr Customer firstname surname address postnr town Booking bookingId time amount filmId customerNr Theater theaterNr capacity filmId

CREATE DATABASE s123456_cinema; Cinema database First we create a new Schema Then we will create the tables The DDL commands can be downloaded from https://edu.hioa.no/ark2100/current/aids/sql%2 0cinema%20en.sql CREATE DATABASE s123456_cinema;

Create Cinema

DDL for Create database

Result of Create database

SQL window for Cinema database

ER model Contributor (personId, firstname, surname, dob, deathDate) Film (filmId, title, year, length, manufacturer, distributorId*) Distributor (distributorId, name, address, postnr, town) Customer (customerNr, firstname, surname, address, postnr, town) Theater (theaterNr, capacity, filmId) Participation (filmId*, personId*, role) Booking (filmId*, customerNr*, bookingDate, time, amount)

CREATE TABLE Contributor ( personId INT NOT NULL AUTO_INCREMENT, firstname VARCHAR(30), surname VARCHAR(30), dob DATE, deathDate DATE, PRIMARY KEY (personId) ) engine = innoDB; CREATE TABLE Theater ( theaterNr INT NOT NULL, capacity INT, filmId INT, PRIMARY KEY (theaterNr), FOREIGN KEY (filmId) REFERENCES Film (filmId) ) engine = innoDB; CREATE TABLE Distributor ( distributorId INT NOT NULL AUTO_INCREMENT, address VARCHAR(100), postnr VARCHAR(4), town VARCHAR(100), PRIMARY KEY (distributorId) ) engine = innoDB; CREATE TABLE Participation ( filmId INT, personId INT, role VARCHAR(100), PRIMARY KEY (filmId, personId, role), FOREIGN KEY (filmId) REFERENCES Film (filmId), FOREIGN KEY (personId) REFERENCES Contributor (personId) ) engine = innoDB; CREATE TABLE Film ( filmId INT NOT NULL AUTO_INCREMENT, tittle VARCHAR(30), year CHAR(4), length INT, manufacturer VARCHAR(30), distributorId INT, PRIMARY KEY (filmId), FOREIGN KEY (distributorId) REFERENCES Distributor (distributorId) ) engine = innoDB; CREATE TABLE Booking ( filmId INT, customerNr INT, bookingDate DATE, time TIME, amount INT, PRIMARY KEY (filmId, customerNr, bookingDate, time), FOREIGN KEY (filmId) REFERENCES Film (filmId), FOREIGN KEY (customerNr) REFERENCES Customer (customerNr) ) engine = innoDB; CREATE TABLE Customer ( customerNr INT NOT NULL AUTO_INCREMENT, firstname VARCHAR(30), surname VARCHAR(30), address VARCHAR(100), postnumber VARCHAR(4), town VARCHAR(100), PRIMARY KEY (customerNr) ) engine = innoDB;

DDL for Create Cinema 1. Write in the DDL command 2. Press 'Go'

Cinema database structure

CREATE USER user [IDENTIFIED BY PASSWORD] 'passord'] CREATE a database user CREATE USER user [IDENTIFIED BY PASSWORD] 'passord'] We do not have the right to run this command

DROP Everything you can CREATE, you can DROP (delete) Database Table User

{DATABASE | SCHEMA} [IF EXISTS] db_navn; DROP Syntax DROP {DATABASE | SCHEMA} [IF EXISTS] db_navn;

DROP DROP {DATABASE | SCHEMA} [IF EXISTS] db_name; DROP TABLE [IF EXISTS] table_name [, table_nameN]; DROP USER user [, userN];

TRUNCATE Sometimes you may want to delete all the contents of a table Easiest way to do this is with the truncate command USE s123456_firstdatabase; TRUNCATE Car;

ALTER After you have created a table, you may need to change it, eg Add a new column to a table Change the primary key

ALTER examples USE s123456_firstdatabase; ALTER TABLE Car ADD MotorCC VARCHAR(255); ALTER TABLE Car DROP MotorCC; USE s123456_firstdatabase; ALTER TABLE Airport ADD Continent VARCHAR(255); ALTER TABLE Airport DROP Continent;

So far We have looked at one DBMS called MySQL and use a program called phpMyAdmin to work with MySQL We looked at datatypes We looked at some DDL commands CREATE database and tables DROP database / DELETE ALTER a table We have not looked at how to insert data or retrieve data