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

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
Advertisements

A Guide to Oracle9i1 Creating and Modifying Database Tables Chapter 2.
Creating Tables Unit 9 - Databases.
CIT 613: Relational Database Development using SQL Revision of Tables and Data Types.
Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? Page 97 in Course.
Data Definition Language (DDL)
MS-Access XP Lesson 1. Introduction to MS-Access Database Management System Software (DBMS) Store data in databases Database is a collection of table.
Database Chapters.
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
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.
Maintenance Modifying the data –Add records –Delete records –Update records Modifying the design –Add fields into tables –Remove fields from a table –Change.
This course has taken from This unique introductory SQL tutorial not only provides easy-to-understand SQL instructions, but it allows.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
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.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
Concepts of Database Management Sixth Edition
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.
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
Oracle Data Definition Language (DDL)
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands 
ASP.NET Programming with C# and SQL Server First Edition
Concepts of Database Management Seventh Edition
SQL data definition using Oracle1 SQL Data Definition using Oracle.
CSC 2720 Building Web Applications Database and SQL.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Concepts of Database Management Seventh Edition
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
Intro to SQL| MIS 2502  Spacing not relevant › BUT… no spaces in an attribute name or table name  Oracle commands keywords, table names, and attribute.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
# 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.
Visual Programing SQL Overview Section 1.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
Fox MIS Spring 2011 Database Week 6 ERD and SQL Exercise.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Sql DDL queries CS 260 Database Systems.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
Introduction to MySQL Ullman Chapter 4. Introduction MySQL most popular open-source database application Is commonly used with PHP We will learn basics.
Basic SQL*Plus edit and execute commands SQL*Plus buffer and built-in editor holds the last SQL statement Statements are created in free-flow style and.
Creating and Managing Tables. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically represents subsets.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
3 A Guide to MySQL.
Fundamentals of DBMS Notes-1.
SQL and SQL*Plus Interaction
Insert, Update and the rest…
SQL Creating and Managing Tables
ORACLE SQL Developer & SQLPLUS Statements
SQL Creating and Managing Tables
SQL Creating and Managing Tables
CS4222 Principles of Database System
Oracle Data Definition Language (DDL)
CS122 Using Relational Databases and SQL
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter 2: Creating And Modifying Database Tables
CS1222 Using Relational Databases and SQL
Data Definition Language
Data Definition Language
MySQL Database System Installation Overview SQL summary
CS122 Using Relational Databases and SQL
Database Instructor: Bei Kang.
SQL (Structured Query Language)
Presentation transcript:

Session 2Introduction to Database Technology Data Types and Table Creation

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

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

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)

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.

Session 2Introduction to Database Technology MySQL Numeric Data Types INT, INTEGER Stores integers (whole numbers with no decimal part). Range: to SMALLINT Stores integers but uses less storage space than INT Range: to 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 ) Use this data type for currency fields Use Numeric data types for fields whose values will be used in calculations

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

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.

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)

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.

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

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

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.

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

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)

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

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.

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

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

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

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

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)

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

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

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.