Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Databases 1 F27DB Introduction to Database Systems Dr. Helen Hastie, Room: LT2 Material available on Vision Vision (modified.

Similar presentations


Presentation on theme: "Introduction to Databases 1 F27DB Introduction to Database Systems Dr. Helen Hastie, Room: LT2 Material available on Vision Vision (modified."— Presentation transcript:

1 Introduction to Databases 1 F27DB Introduction to Database Systems Dr. Helen Hastie, h.hastie@hw.ac.uk Room: LT2 Material available on Vision Vision (modified from Monica Farrow’s slides) Twitter: @IntroDBHW and #IntroDBHW

2 Introduction to Databases 2 Overview Before computers Introduction to databases What do they look like? Why are they like that? What database management systems could we use? How to use MySQL in the Linux lab A web application with a database Designing web pages using XHTML Programming PHP scripts to query the database and produce web page reports Looking ahead to 2 nd year module and beyond

3 Introduction to Databases 3 Before Computerised Databases Information could be stored in box of record cards for a class of students Each card contains - name address (term and home) date of birth matric no.

4 Introduction to Databases 4 What is a database ? A database is an organized collection of data, systematically organized for easy access and analysis The box is a table in the database The cards are records Records are made up of fields Having designed and created a database, we can then amend, query, sort and report the data

5 Introduction to Databases 5 Edge-notched card – Invented in 1896 The card held details on literature on the study of metals The card was preprinted with a fact by each hole, maybe a hole for each metal such as copper, silver etc. You type the information about the name of the book in the empty space, and then clip out a notch on the edge for every true fact.

6 Introduction to Databases 6 Another edge-notched card This card is just printed with numbers so you have to decide for yourself which numbers mean which facts. Cut the notches out for facts that are true, with the clipper tool.

7 Introduction to Databases 7 Edge-notched cards demo Blank preprinted cards Add data (a picture) Round hole – NO Make a notch – YES A simple true/false state is the basis of all data stored on computer SWIM PET STRIPES LEGS

8 Introduction to Databases 8 Animals information system SWIMPETSTRIPESLEGS             

9 Introduction to Databases 9 Edge-notched card – retrieving information Information is retrieved by putting one or more long wire pins through certain holes. As the pins were lifted, the cards that were notched in the hole positions where the pins were inserted would be left behind as rest of the deck was lifted by the pins. Find all the pets... SWIM PET STRIPES LEGS

10 Introduction to Databases 10 Edge-notched card – logical operations The cards lifted out match a logical NOT operation Find all the creatures that do not swim SWIM PET STRIPES LEGS

11 Introduction to Databases 11 Edge-notched card – logical operations Using two or more pins produced a logical AND function. Find all the creatures who can swim and also has legs SWIM PET STRIPES LEGS

12 Introduction to Databases 12 Edge-notched card – logical operations Combining the cards from two different selections produced a logical OR. Find the creatures with stripes or legs (or both) Quite complex manipulations, including sorting were possible using these techniques. SWIM PET STRIPES LEGS

13 Introduction to Databases 13 A complete system today Server Program with Functionality Code Response e.g. html for display Management Programs (application in e.g. java or maybe also a web app) Data Management (updates) Customer email Manager Reports Queries (e.g. SQL) Data/ Success? Request with parameters maybe from forms Search Order Queries (e.g. SQL) Data/ Success? Browse Data Repository e.g. Database

14 Introduction to Databases 14 How you will start You send commands to the database from a terminal window in the Linux lab, EM250 It is all text-based, no GUI We can define tables, view the data and perform updates. Queries (e.g. SQL) Data/ Success? MySQL Database

15 Introduction to Databases 15 A complete system for this module Response e.g. html for display Data Management (updates) Manager Reports Queries (e.g. SQL) Data/ Success? Request with parameters maybe from forms Search Add Queries (e.g. SQL) Data/ Success? View PHP scripts MySQL Database Apache server program Server-side PHP scripts My Website

16 Introduction to Databases 16 Database Management Systems A DBMS provides security, integrity, concurrency control and recovery control There is a comparison on wikipedia of almost 50 different DBMSs. In the CS department we have MySQL – industrial strength Open source, available from the Linux lab Oracle – industrial strength Oldish version, available from the Linux lab Microsoft Access, suitable for small applications Installed on each Windows PC

17 Introduction to Databases 17 DBMS Compatibility Relational DBMSs provide the same sort of features and use the same language, SQL, to define the tables and update and retrieve the data However, there are differences! This module will teach you the basic principles If you start using a different DBMS, you will have to find out exactly what features that particular one provides

18 Introduction to Databases 18 MySQL Initially released in 1995 Popular for web applications, particularly in conjunction with the PHP scripting language Continually being improved MySQL is available from terminal windows in the department Linux lab Type in commands and view the results

19 Introduction to Databases 19 MySQL Limitations There are some limitations. E.g. It is ‘forgiving’ of incorrect data. So if are trying to insert a date into a column and you provide an incorrect date, it helpfully puts in a date of 0 and carries on. It only tells you if you have switched warnings on. Many DBMSs allow you to constrain the values of the data. E.g. only M or F for gender. However, although MySQL accepts these commands as valid commands, they are not acted upon. You will have to do your own checks. A relational database is usually made up of >1 tables, with relationships between these tables. So you might wish to constrain the data entered in one table to be compatible with data in another table. Again, MySQL accepts these commands as valid syntatically, but only enforces these commands in one type of table (InnoDB). We will use these tables.

20 Introduction to Databases 20 MySQL at home This module assumes that you will do all the work in the lab However, if you wish to work on your own computer, you could try installing something like easyPHP EasyPHP is a WAMP software bundle that installs web server services on to the Windows computer and allows quick and easy development of PHP and MySQL on a localhost (also known as 127.0.0.1). The package includes an Apache server, a MySQL database, and the PHP extension. Unfortunately, departmental staff do not have the time to help with this.

21 Introduction to Databases 21 An example - spies Your friendly international spy recruitment agency is contemplating installing a database for the storage of data about its spies. They would like you to advise them on the design and implementation of its database. The agency requires to store information about spies. Each spy should have a unique code name, a first name, last name, date of birth, sex, distinguishing mark, and payment due.

22 Introduction to Databases 22 The spy table Each spy should have a unique code name, a first name, last name, date of birth, sex, distinguishing mark, and payment due. code name first name last name date of birthsexmark amount due 007JamesBond12 December 1972 Mmole on chin 5050 budFannyCharles ton 31 July 1983Fscar on cheek 25.67 freddieJohnSmith05 September 1954 Mone finger missing 312.5

23 Introduction to Databases 23 Spy table definition First you need to define the table What will the table be called? What will the columns be called? E.g. firstName. Don’t use spaces What type of data will be stored there? E.g.: VARCHAR(5) A variable number of characters, up to a maximum CHAR(1) A fixed number of characters DECIMAL (9,2) A decimal number, up to a maximum number of characters with a fixed number of decimal places INTEGER(5) An integer number, up to a maximum number of characters DATE Etc – the above types are the most useful ones

24 Introduction to Databases 24 SQL We use MySQL – Structured Query Language For definition, such as table definition For inserting, deleting, updating and retrieving data Today we’ll just look at a few commands CREATE TABLE INSERT VALUES SELECT

25 Introduction to Databases 25 CREATE TABLE MySpy CREATE TABLE MySpy ( codeName VARCHAR(10) PRIMARY KEY, firstName VARCHAR(20), lastName VARCHAR(20), dateOfBirth DATE, gender CHAR(1), mark VARCHAR(20), amountDue DECIMAL (9,2), )ENGINE=INNODB; The last line specifies the table type. Note the Primary Key

26 Introduction to Databases 26 Primary key In a relational database table, you should usually specify a primary key This is a field which uniquely identifies the record National insurance number identifies a person Country name identifies a country Country name and town name together identifies a town Christchurch in New Zealand and in England

27 Introduction to Databases 27 Adding in some data Use the INSERT command to add some rows to the table Surround text and dates with single quotes Don’t put quotes around numbers The format of the date is important mysql>INSERT INTO MySpy VALUES ('007','James', 'Bond', '1972-12-01','M', 'Mole on chin', 5050);

28 Introduction to Databases 28 Viewing the data (1) To see the complete contents of the table mysql> SELECT * FROM MySpy;

29 Introduction to Databases 29 Viewing the data (1) To see the contents ordered e.g. mysql> SELECT * FROM MySpy ORDER BY dateOfBirth;

30 13/09/2015 Introduction to Databases 30 Viewing the data (1) To see just some of the columns mysql> SELECT codeName, firstName, lastName FROM MySpy; To see just some of the rows mysql> SELECT * FROM MySpy WHERE gender = ‘M’;

31 Introduction to Databases 31 Viewing the data (1) Combine these as appropriate mysql> SELECT firstName, dateOfBirth FROM MySpy WHERE gender = ‘M’ ORDER BY dateOfBirth;

32 Introduction to Databases 32 Viewing the data (2) Select some of the rows on multiple conditions mysql> SELECT * FROM MySpy WHERE firstName = ‘James’ AND lastName = ‘Bond’; mysql> SELECT * FROM MySpy WHERE firstName = ‘James’ OR lastName = ‘Bond’;

33 Introduction to Databases 33 Viewing the data (2) Perform aggregate functions mysql> SELECT count(*) FROM MySpy WHERE gender = ‘M’; mysql>SELECT sum(amountDue) FROM MySpy; mysql> SELECT gender, sum(amountDue) FROM MySpy GROUP BY gender;

34 Introduction to Databases 34 Using MySQL The first lab (after this lecture in EM250) is an introduction to MySQL Work through the handout so that you become familiar with the system You enter commands by typing them at the command prompt. You can use the up arrow to retrieve previous commands. It is useful to store long commands in a text file, and run them from there You don’t have to retype the whole thing if you made a mistake. Although there are MySQL commands to edit a command, it’s easier just to use a standard text editor Your command is safely stored in case you want to run it again or alter it.

35 Introduction to Databases 35 Helping each other In the lab there will be one or two lecturers and three lab helpers. We want you to learn to use MySQL, not get stuck with the linux system in the lab. See Partner List for first few labs CS/IS students - Help them with linux if necessary Elective students – ask for help if necessary

36 Introduction to Databases 36 BEFORE TODAY’s LAB First year CS & IS students do nothing Others please go to the Computer Science Helpdesk in EM1.33 If you aren’t registered to use the CS linux lab, fill in form and register. Then ask for a MySQL account. If you have already registered to use any CS dept machines for another reason, just ask for a MySQL account


Download ppt "Introduction to Databases 1 F27DB Introduction to Database Systems Dr. Helen Hastie, Room: LT2 Material available on Vision Vision (modified."

Similar presentations


Ads by Google