Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Session 2Introduction to Database Technology Data Types and Table Creation."— Presentation transcript:

1 Session 2Introduction to Database Technology Data Types and Table Creation

2 Session 2Introduction to Database Technology Session learning outcomes At the end of this session you will be able to: Work with your database using the MySQL Query Browser Describe the relationship between fields, records and tables Select appropriate data types and sizes for the fields in a table Design a table Use SQL commands to create a table in a MySQL database Use SQL Commands to enter and edit data in a table

3 Session 2Introduction to Database Technology MySQL Query Browser Provides a graphical user interface (GUI) for use with MySQL databases Will be used in the rest of this module Hands-on 3 enables you to familiarise yourself with this interface

4 Session 2Introduction to Database Technology Table Creation Steps 1.List the attributes (fields) 2.Choose an appropriate data type for each attribute 3.Select an appropriate primary key for the table 4.Create the table in the database 5.Modify the table structure (if required) 6.Enter data into the table 7.Edit the data (if required)

5 Session 2Introduction to Database Technology MySQL Character Data Types CHAR(n) Stores character string up to n (max 255) characters long. If the string is less than n characters long, blank spaces are stored for the remaining characters. VARCHAR(n) Stores character string up to n (max 255) characters long. Only stores the string with no blank characters. Uses less storage space. Slower processing for queries and updates TEXT Use to store textual data up to a maximum of 65,535 characters Use Character data types for fields that will contain character strings – letters, digits and special characters. Also use for numbers that will not be used in calculations – a customer number, for example.

6 Session 2Introduction to Database Technology MySQL Numeric Data Types INT, INTEGER Stores integers (whole numbers with no decimal part). Range: -2147483648 to 214748367 SMALLINT Stores integers but uses less storage space than INT Range: -32768 to 32767 Use when you are sure the values will be within this range DECIMAL(p,q) Stores decimal number p digits long with q decimal places. E.g. DECIMAL(5,2) has 3 places to the left and two to the right of the decimal point (such as 100.00) Use this data type for currency fields Use Numeric data types for fields whose values will be used in calculations

7 Session 2Introduction to Database Technology Storing dates in MySQL DATEStores dates. Dates have format YYYY-MM-DD Enter dates using single quote marks. E.g. 2008-08-20 is August 20, 2008 This has introduced the basic data types that will be used in the Yum Juices database that you create during this module You can find more information about data types in the MySQL Reference Manual

8 Session 2Introduction to Database Technology NULL Values If a field does not contain data its value is referred to as NULL To ensure a field cannot contain a NULL value you need to define it as NOT NULL when you create the table NULL values are appropriate when a field may not contain data for some records – for example, in the Customer table there are two fields for the address but some customers only have one address line.

9 Session 2Introduction to Database Technology Primary Keys A unique identifier for each record in a table All tables in a relational database should have a primary key Often a number –E.g. customer ID, order number Can be one or more fields –Primary keys made up of two or more fields are known as composite primary keys (covered later in this module)

10 Session 2Introduction to Database Technology Now do… Hands-on Exercise 4.1 –Define data types for the fields in the Yum Customer table. –Decide which fields can contain NULL values. –Select an appropriate primary key for the table. –Record your decisions on the sheet provided.

11 Session 2Introduction to Database Technology CREATE TABLE Syntax CREATE TABLE tablename ( col_name datatype [NOT NULL] [PRIMARY KEY], …, col_name datatype ) ;

12 Session 2Introduction to Database Technology Table and field name rules Can include alphanumeric characters and underscores _ but no other characters. Should not consist only of digits.- e.g. 1223 Should not start with a digit – e.g. 1Pet Cannot exceed 18 characters. Are case sensitive - 'Pet', 'PET' and 'pet' would be regarded as three different names, for example. Must not contain spaces. Must not be a MySQL reserved word – a word that has special meaning in MySQL such as SELECT, for example.

13 Session 2Introduction to Database Technology Table and field name conventions (used in this module) Table names start with an initial uppercase letter and are singular – Customer rather than Customers, for example. Field names are in lower case, prefixed by the name of the table (also lowercase) with an underscore – customer_name, for example. Will be descriptive of their contents. For example, Customer rather than Table1.

14 Session 2Introduction to Database Technology CREATE TABLE Example CREATE TABLE Pet (pet_id CHAR(2) PRIMARY KEY NOT NULL, pet_name CHAR(10), pet_type CHAR(10), pet_cost DECIMAL(5,2), pet_dateofbirth DATE );

15 Session 2Introduction to Database Technology Modifying the structure of a table in MySQL ALTER TABLE tablename MODIFY col_name new_datatype - Change the data type of a specified field ADD col_name datatype - Add a new field to the table CHANGE old_col_name new_col_name datatype - Change the name of a field DROP COLUMN col_name - Remove a field (together with any data it contains)

16 Session 2Introduction to Database Technology ALTER TABLE Examples ALTER TABLE Pet MODIFY pet_name VARCHAR(25) - Changes length of pet_name field ADD pet_food VARCHAR(30) - Adds pet_food field to the Pet table CHANGE pet_dateofbirth pet_birthday DATE - Changes name of pet_dateofbirth field to pet_birthday DROP COLUMN pet_food - Removes pet_food field from Pet table

17 Session 2Introduction to Database Technology MySQL Scripts Saving your SQL statements as a script allows you to re- run them. A script can contain one or more SQL statements Each statement must end with a semi-colon ; A script is simply a text file. –You can create a script using a text editor (such as Notepad). In this module, you will use the Query Browser to write and run scripts. Scripts saved using the Query Browser will be given a.sql extension –scripts saved with a.txt extension can also be opened and run from the Query Browser.

18 Session 2Introduction to Database Technology Now do… The rest of Hands-on 4 –Create the Yum customer table using the attributes and data types you listed earlier –Save your table creation statement as a script

19 Session 2Introduction to Database Technology Entering data INSERT INTO tablename VALUES (Val_1, Val_2, …, Val_n ) ; Inserts data into all fields from left to right You must specify a value for each field Example: INSERT INTO Pet VALUES (1,Mog,cat,20.00,2006-05-10);

20 Session 2Introduction to Database Technology Entering data with NULLs You must explicitly enter NULL if a field does not contain a value For example, to enter Mogs details without a date of birth: INSERT INTO Pet VALUES (1,Mog,cat,20.00,NULL);

21 Session 2Introduction to Database Technology Entering data into specific fields List the fields that data is to be entered into after the table name You must include all fields that are defined as NOT NULL Data will be entered in the order in which the fields are specified Example – enter the type, name and ID for a pet INSERT INTO Pet (pet_type, pet_name, pet_id) VALUES (cat,Mog,1);

22 Session 2Introduction to Database Technology Updating Data You may make mistakes when entering the data and need to correct them Data stored in the database may need to be changed (a customer moves to a new address, for example)

23 Session 2Introduction to Database Technology UPDATE Syntax UPDATE tablename SET fieldname = new_value WHERE condition to select record Example: UPDATE Pet SET pet_name = 'Moggy' WHERE pet_id = 1';

24 Session 2Introduction to Database Technology Now do… Hands-on 5 –Enter data into the Yum Customer table that you created in Hands-on 3 –Make sure you have downloaded the Yum sample data before starting this exercise

25 Session 2Introduction to Database Technology SQL Command Summary This session has introduced the following SQL commands: –CREATE TABLE –ALTER TABLE –INSERT INTO –UPDATE Use the Reference Manual to review these commands and ensure you are familiar with their syntax and usage.


Download ppt "Session 2Introduction to Database Technology Data Types and Table Creation."

Similar presentations


Ads by Google