Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.

Slides:



Advertisements
Similar presentations
A Guide to Oracle9i1 Creating and Modifying Database Tables Chapter 2.
Advertisements

Session 2Introduction to Database Technology Data Types and Table Creation.
Multiple Table Queries 2: Outer Joins, Self Joins, Nested Queries, and Views CS 320.
Action Queries CS 320. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects  Data Manipulation Language.
1 Chapter 2: Creating and Modifying Database Tables.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Creating Database Tables © Abdou Illia MIS Spring /21/2015.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
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
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
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)
Copyright © Curt Hill SQL The Data Definition Language.
CSCI 6962: Server-side Design and Programming
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
ASP.NET Programming with C# and SQL Server First Edition
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Introduction to MySQL Lab no. 10 Advance Database Management System.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
CSC 2720 Building Web Applications Database and SQL.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Single-Table Queries 1: Basics CS 320 Online. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Concepts of Database Management Seventh Edition
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
11 3 / 12 CHAPTER Databases MIS105 Lec15 Irfan Ahmed Ilyas.
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
Creating PHPs to Insert, Update, and Delete Data CS 320.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
Introduction to Databases Queries CS 146. Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT.
# 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.
SQL ACTION QUERIES AND TRANSACTION CONTROL CS 260 Database Systems.
1 Chapter 2: Creating and Modifying Database Objects.
Sql DDL queries CS 260 Database Systems.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
Class 3Intro to Databases Class 4 Simple Example of a Database We’re going to build a simple example of a database, which will allow us to register users.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Introduction to MySQL Ullman Chapter 4. Introduction MySQL most popular open-source database application Is commonly used with PHP We will learn basics.
LECTURE FOUR Introduction to SQL DDL with tables DML with tables.
Introduction to Databases Queries CS 146. Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT.
3 A Guide to MySQL.
Fundamentals of DBMS Notes-1.
CS 3630 Database Design and Implementation
Managing Tables, Data Integrity, Constraints by Adrienne Watt
DATABASE MANAGEMENT SYSTEM
CIS16 Application Programming with Visual Basic
Oracle Data Definition Language (DDL)
Chapter 2: Creating And Modifying Database Tables
CS3220 Web and Internet Programming SQL and MySQL
CS3220 Web and Internet Programming SQL and MySQL
SQL (Structured Query Language)
Presentation transcript:

Creating Database Tables CS 320

Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database will be structured, regardless of the DBMS 3. Physical: describes HOW the database will be implemented using a specific DBMS

Required Software MySQL DBMS MySQL Workbench SQL editor (or another command-line SQL query editor)

Review: Web/Database Architecture Network Web browser Web server 2.Request for Web page that requires database data 1.Request for Web page that requires database data 6. Web page HTML file downloaded to client 5. Web page HTML file containing database data downloaded to client 3.Runs a program that makes a data request 4. Data response Database server

MySQL Workbench Architecture Network Client running Workbench 2.SQL command 1.SQL command 4. Response (error or confirmation) 3. Response (error or confirmation) Database server Workbench uses:  Running commands to create tables (only way)  Testing queries to use in PHP pages (later)

MySQL Database Structure  A MySQL database consists of multiple user accounts  Also called database schemas  MySQL databases in general:  A user account can contains different databases  UWEC MySQL database:  In our configuration, you can't create new databases  All of your tables are in the same database

MySQL Database Structure  UWEC MySQL database:  In our configuration, you can't create new databases  All of your tables are in the same database  Issues?  All tables must have a unique name  For your projects, you'll use a group database account

SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects  Data Manipulation Language (DML)  Used to insert, update, delete, and view the data in database objects

DDL Commands  Used to create and modify database objects  CREATE  ALTER  DROP  DDL commands execute as soon as they are issued, and do not need to be explicitly saved

Creating a New Database CREATE DATABASE database_name CREATE DATABASE candy - General syntax - Specific example **You won't do this with the UWEC MySQL database

Specifying Which Database to Use For Creating Tables USE database_name USE candy You'll need to specify to USE your database: USE morrisjp;

Creating a Database Table CREATE TABLE table_name (field1_name datatype size, field2_name datatype size, …) CREATE TABLE candy_product (prod_id BIGINT, prod_desc VARCHAR(30), prod_cost DECIMAL(5,2), prod_price DECIMAL(5,2))

General Notes on SQL Commands Not case-sensitive Can span multiple lines in a text editor To run multiple statements in the editor window, separate each statement with ; To "comment out" (not execute) a statement, enclose it in /* … */

Naming Tables and Fields  Database table and field name rules  1 to 64 characters long  Can contain letters, numbers, or underscores  Can't contain certain characters (/ \,) or characters not permitted in file names  Every table in your database must have a unique name  Every field in a table must have a unique name

Good* Table Naming Practices  Name every table using a combination of 2 words, separated by an underscore  This approach avoids using reserved words  Use the same descriptive first word for all tables in a related database *Required in this class

Good* Field Naming Practices Name every field using a combination of 2 words, with the first word indicating the table name  Avoids creating homonyms What about foreign keys?  Use the name that is in the parent table *Required in this class

Database Field Data Types Specify:  Type of data the field stores  Maximum data size Purpose:  Specify internal encoding  Optimize internal storage use  Provide error checking

Main Data Types Text data types  CHAR  VARCHAR Number data types  Integers: INT and variations  Floating point numbers: DECIMAL, FLOAT Dates & times  DATE, TIME, and variations

 For fixed-width character fields (1-255 characters)  Fields in which you know the exact number of characters  Pads out the remaining characters with blank spaces  Removes the trailing blank spaces when data is retrieved CHAR Data Type fieldname CHAR(size) cust_type CHAR(1)

VARCHAR Data Type For variable-width character fields (1-255 characters)  Number of characters varies for individual data values  Values are not padded with blank spaces fieldname VARCHAR(maxsize) cust_name VARCHAR(30)

MySQL Number Data Types: Integers TINYINT  Signed: -128 to 127  Unsigned: 0 to 255 SMALLINT  Signed: to  Unsigned: 0 to INT  Signed: to  Unsigned: 0 to BIGINT  Signed: to  Unsigned: 0 to

Integer Data Type Usage Choose the "smallest" integer type (SMALLINT, INT, BIGINT) that is big enough to hold the largest possible value Question: Why not just always use BIGINT?

Integer Data Type Examples fieldname integer_data_type prod_id TINYINT cust_id INT purch_id BIGINT

MySQL Numeric Data Types: Floating Point Numbers  For values that have a decimal portion  DOUBLE or FLOAT: very large or very small values that have an indeterminate number of decimal places  DOUBLE: takes more space, but is extremely accurate (15 decimal places)  FLOAT: takes less space, use when extreme accuracy isn't required  DECIMAL: for values that have a predetermined number of decimal places

Floating Point Examples fieldname FLOAT fieldname DOUBLE pounds FLOAT fieldname DECIMAL(precision, scale) prod_cost DECIMAL(5,2) Why not just omit the precision and scale? It defaults to the Maximum allowed by your hardware…

Think critically: Why not just declare all numeric values using the DOUBLE data type? That will take care of all cases.  This doesn't make use of error checking  This doesn't optimize use of data storage capacity

Numeric  Surrogate keys (primary key ID values)  All numeric data you might use in a calculation (prices, quantities, etc.) Character: Use an appropriate character data type for all fields that are not numbers or dates  Names, addresses, etc.  Postal codes, telephone numbers, social security numbers Character and Numeric Data Type Usage

MySQL Date and Time Data Types DATE  Dates ranging from to  Display in the format YYYY-MM-DD TIME  Times ranging from is -838:59:59 to 838:59:59 Can also be used to represent elapsed time intervals  Display in the format HH:MI:SS

MySQL Date and Time Data Types (continued) DATETIME  Used to store both a date and time component ranging from :00:00 to :59:59  Displays in the format YYYY-MM-DD HH:MI:SS YEAR  Can store either a 2-digit or 4-digit year  4-digit range: 1901 to 2155  2-digit range: 70 to 69 (1970 – 2069)

Review: Creating a Database Table CREATE TABLE table_name (field1_name datatype size, field2_name datatype size, …) CREATE TABLE candy_purchase( purch_id INT, prod_id INT, cust_id INT, purch_date DATE, purch_delivery_date DATE, purch_pounds FLOAT, purch_status VARCHAR(10))

Scripts Text files that contain a series of SQL commands  Each command separated by ; Purpose:  Write a series of commands and then execute them all at once Create all tables in a database Insert a set of test records into the tables Issue with a script that creates all of the tables in a database:  You can't create two tables with the same name in the same database  In a script file, you always need to (try to) DROP all tables first before you create them

Dropping a Database Table Use IF EXISTS in a script to avoid error messages DROP TABLE table_name DROP TABLE IF EXISTS table_name

Example DROP TABLE Commands in a Script /*commands to drop all tables */ DROP TABLE IF EXISTS candy_purchase; DROP TABLE IF EXISTS candy_customer; DROP TABLE IF EXISTS candy_cust_type; DROP TABLE IF EXISTS candy_product; /*commands to create tables */ CREATE TABLE candy_cust_type (…

Creating Tables in the MySQL Visual Environment Generates the SQL command to create the table Doesn't save the command or generate a script

Test Yourself: What data type declaration would you use to create a database field that stores part descriptions (DESCRIPTION) in the Premiere Products PART table? a. VARCHAR(30) b. CHAR(30) c. VARCHAR(10) d. CHAR(10) e. None of the above

Test Yourself: What data type would you use to create a database field that stores part quantities on hand (ON_HAND) in the Premiere Products PARTS table? a. INT b. SMALLINT c. BIGINT d. DECIMAL e. Either a or b ON_HAND

Test Yourself: What data type declaration would you use to create a database field that stores part prices (PRICE) in the Premiere Products PARTS table? a. VARCHAR(10) b. DECIMAL(4,2) c. DECIMAL(5,2) d. DOUBLE e. None of the above PRICE $24.95 $ $ $ $ $ $ $ $ $

Test Yourself: What data type would you use to store ZIP code (ZIP) values in the Premiere Products REP table? a. CHAR(5) b. TINYINT c. DECIMAL(5,0) d. VARCHAR(10) e. None of the above ZIP