Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database commands : DDL

Similar presentations


Presentation on theme: "Database commands : DDL"— Presentation transcript:

1 Database commands : DDL
ARK2100 Digital recordkeeping and preservation I 2016 Thomas Sødring P48-R407

2 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!!

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

4 First we need access to MySQL
Fisrt you have to create a database account Click on 'Opprett MySQL-konto' Means Create mysql account! Then you can log on If you forget or have problems with your password Click on 'Bytt MySQL-passord' Means Change MySQL password

5 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 or HiOA computers! Do not use a password that you use to log on to gmail / facebook etc Choose something like '_23Frank'

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

7 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.

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

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

10 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

11 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) )

12 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

13 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

14 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 characters MEDIUMTEXT A field of variable length that can consist of a maximum of characters LONGTEXT A field of variable length that can consist of a maximum of characters

15 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 characters MEDIUMBLOB A field of variable length that can consist of a maximum of characters LARGEBLOB A field of variable length that can consist of a maximum of characters

16 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

17 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 or and 32767 MEDIUMINT INT A number between 0 and or and BIGINT A number between 0 and or and

18 Wrap around

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

20 Datatypes (date and time)
Description DATE A date between the year and the year DATETIME A date and time between the year :00:00 and the date and time :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)

21 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

22 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

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

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

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

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

27 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

28 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!

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

30 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;

31 phpMyAdmin

32 Entering SQL commands

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

34 Result of create

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

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

37 Ready to go

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

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

40 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.

41 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) );

42 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

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

44 Result of Create Car

45 Structure of Car

46 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

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

48 Result of Create Airport

49 Structure of Airport

50 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

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

52 Result of Create Building

53 Structure of Building

54 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

55 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 0cinema%20en.sql CREATE DATABASE s123456_cinema;

56 Create Cinema

57 DDL for Create database

58 Result of Create database

59 SQL window for Cinema database

60 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)

61 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;

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

63 Cinema database structure

64 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

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

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

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

68 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;

69 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

70 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;

71 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


Download ppt "Database commands : DDL"

Similar presentations


Ads by Google