Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITX2000 Remote hosts and web servers Prof. Xiaohong (Sharon) Gao Room: T125 Ext: 12252 Week 3 – MySQL – Statements.

Similar presentations


Presentation on theme: "ITX2000 Remote hosts and web servers Prof. Xiaohong (Sharon) Gao Room: T125 Ext: 12252 Week 3 – MySQL – Statements."— Presentation transcript:

1 ITX2000 Remote hosts and web servers Prof. Xiaohong (Sharon) Gao Room: T125 Ext: 12252 Email: x.gao@mdx.ac.ukx.gao@mdx.ac.uk Week 3 – MySQL – Statements

2 1. databases A database is a structure that comes in two flavours: a flat database a relational database. A flat database is just stored on hard drives like a text file. A relational database is much more oriented to the human mind with a table-like structure. MySQL is a relational database.

3 Relational database (1) NameAgeWeight (kg) Height (meter) Telephone Bob65801.8012345 Alice25651.70456778 … …

4 Relational database There are tables that store data. The columns define which kinds of information will be stored in the table. An individual column must be created for each type of data you wish to store (i.e. Age, Weight, Height). On the other hand, a row contains the actual values for these specified columns. Each row will have 1 value for each and every column. NameAgeWeight (kg) Height (meter) Teleph one Bob65801.8012345 Alice25651.70456778

5 Why use a database Storing information that fits into logical categories. Stores data in a systematic way. Information retrieval is made much easier, in particular in our current society with ‘Big Data’. For example, a student’s time tables. Student No Student Name DOBEmailModule 1 M0034556Tom1/1/1990tom@mdx.ac.ukITX2000 ModuleLecturerLabClassTutor ITX2000GaoH105CG76Jerry Staff nameDepartmentOfficeemail GaoCSTG12x.gao@mdx.ac.uk

6 Start MySQL Double click XAMPP-control.exe at folder ‘C:\xampp’. Open up a Command Prompt window ( type ‘cmd’ at search window). At the command prompt, do > cd c:\xampp\mysql\bin > mysql –u root

7 Basic statements of MySQL Create databases Need to login as a root (i.e., super user, no password (for the time being)) c:\> mysql –u root mysql> create database db; mysql> show databases; // to show how many databases mysql> use test; // use test database database Table 1 Table 2 Table 3 Table n

8 Create Tables  The columns specify what the data is going to be.  while the rows contain the actual data. (C = Column, R = Row)  In a real MySQL table only the value would be stored, not the R# and C#!  This table has 3 categories, or "columns", of data: Name, Age, and Weight.  This table has 4 entries, or in other words, four rows.

9 MySQL CREATE TABLE syntax CREATE TABLE tbl_name (C1-name type1, C2-name type2, C3-name type3.) Some DATA types: INT[(length)] INTEGER[(length)] REAL[(length,decimals)] DOUBLE[(length,decimals)] FLOAT[(length,decimals)] DATE TIME YEAR CHAR[(length)] VARCHAR(length)

10 MySQL CREATE TABLE syntax Use a CREATE TABLE statement to specify the layout of your table: mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20), -> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE); mysql> SHOW TABLES; +---------------------+ | Tables in test | +---------------------+ | pet | +---------------------+

11 Describe pet table

12 INSERT INTO (‘value’ or values’ are equally OK) mysql> INSERT INTO pet VALUES -> ('Puffball','Diane','hamster','f','1999-03-30',NULL); mysql> INSERT INTO pet VALUE -> ('Puffball','Diane','hamster','f','1999-03-30',NULL);

13 Select statement (1) mysql> select * from pet;//to view ALL the data in pet table;

14 Select statement (2) mysql> select name from pet;//To view only names in pet table;

15 Select statement (3) mysql> select name, species from pet;//To view both name and species in pet table;

16 Delete statement mysql> delete from pet where name=‘Fluffy’;// deleting first row. mysql> delete from pet where 1>0;// deleting all rows;

17 LOAD data into TABLE pet - Save following data into a file called “pet.txt”; For missing values (such as unknown sexes or death dates for animals that are still living), you can use NULL values. To represent these in your text file, use \N. For example, the record for Whistler the bird would look like this (where the whitespace between values is a single tab character): name owner species sex birth death Whistler Gwen bird \N 1997-12-09\N

18 LOAD data into TABLE pet -Save ‘pet.txt’ at folder ‘c:\xampp\mysql\data\test’; mysql> LOAD DATA INFILE ‘pet.txt’ INTO TABLE pet;

19 Create table mysql> create table person (Name char(50), Age int, Weight int);

20 Ordering table data mysql> insert into person value(‘Alan’,76, 80.9); Mysql> select * from person order by Name; //Ascending order.

21 Ordering table data (1) mysql> SELECT name, weight FROM person ORDER BY birth;

22 Ordering table data mysql> SELECT name, weight FROM person ORDER BY birth DESC;

23 Ordering table data with multiple columns Sorting different columns in different directions. For example, to sort by type of animal in ascending order, then by birth date within animal type in descending order (youngest animals first), use the following query:

24 Summary MySQL


Download ppt "ITX2000 Remote hosts and web servers Prof. Xiaohong (Sharon) Gao Room: T125 Ext: 12252 Week 3 – MySQL – Statements."

Similar presentations


Ads by Google