MySQL Creating a Database Lecture 3

Similar documents
Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

Minimal Spanning Trees

Insertions, Deletions, and Updates

Scheduling and Digraphs

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

The Pairwise-Comparison Method

The Critical-Path Algorithm

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Fundamental Data Types

Draft. Students Table. FName LName StudentID College Year. Justin Ennen Science Senior. Dan Bass Management Junior

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Relational Databases

Relational Databases Lecture 2

Data Types in MySQL CSCU9Q5. MySQL. Data Types. Consequences of Data Types. Common Data Types. Storage size Character String Date and Time.

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Solving Recursive Sequences by Iteration

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Recognition of Tokens

Basic SQL. Basic SQL. Basic SQL

Scope and Parameter Passing

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

The Traveling Salesman Problem Brute Force Method

SQL: Data Definition Language

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

Rotations and Translations

Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

PHP Arrays. Lecture 18. Robb T. Koether. Hampden-Sydney College. Mon, Mar 4, 2013

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Basis Data Terapan. Yoannita

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Programming Languages

PHP Queries and HTML Forms Lecture 23

The Decreasing-Time Algorithm

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

Views. Lecture 15. Robb T. Koether. Fri, Feb 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, / 28

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Density Curves Sections

LR Parsing - Conflicts

Sampling Distribution Examples Sections 15.4, 15.5

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

SQL Data Definition and Data Manipulation Languages (DDL and DML)

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Friends and Unary Operators

Displaying Distributions - Quantitative Variables

CS634 Architecture of Database Systems Spring Elizabeth (Betty) O Neil University of Massachusetts at Boston

The Class Construct Part 1

Simple Quesries in SQL & Table Creation and Data Manipulation

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Nondeterministic Programming in C++

Recursive Descent Parsers

The Traveling Salesman Problem Nearest-Neighbor Algorithm

The Plurality-with-Elimination Method

PHP Arrays. Lecture 20. Robb T. Koether. Hampden-Sydney College. Wed, Feb 28, 2018

Introduction to Databases

Programming Languages

Binary Tree Applications

Scope and Parameter Passing

CS2300: File Structures and Introduction to Database Systems

Chapter 8 How to work with data types

Recursive Linked Lists

Introduction to Compiler Design

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

MySQL: an application

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

The Coefficient of Determination

Basic PHP Lecture 17

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

Read this before starting!

QQ Group

Inheritance: The Fundamental Functions

Introduction to Databases

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #4: SQL---Part 2

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Dynamic Allocation of Memory

Applying Textures. Lecture 27. Robb T. Koether. Hampden-Sydney College. Fri, Nov 3, 2017

Implementing Linked Lists

The Relational Model of Data (ii)

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Street-Routing Problems

Building the Abstract Syntax Trees

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

2.9 Table Creation. CREATE TABLE TableName ( AttrName AttrType, AttrName AttrType,... )

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Lab 4: Tables and Constraints

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

Module 3 MySQL Database. Database Management System

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

SQL Introduction. CS 377: Database Systems

The Normal Distribution

Transcription:

MySQL Creating a Database Lecture 3 Robb T Koether Hampden-Sydney College Mon, Jan 23, 2012 Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 1 / 31

1 Multiple Tables 2 MySQL 3 Creating a Database 4 MySQL Data Types 5 Assignment Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 2 / 31

Outline 1 Multiple Tables 2 MySQL 3 Creating a Database 4 MySQL Data Types 5 Assignment Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 3 / 31

Multiple Tables Suppose that we wish to add to the database the courses and section numbers currently being taught, the names of students who are enrolled in classes, and the professors who are teaching the classes We need additional information Class section number Meeting time Meeting place Maximum enrollment Professor s name Professor s office number Students first names Students last names Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 4 / 31

Relational Databases How should we incorporate this additional information into the database? Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 5 / 31

Relational Databases To add all of those attributes to the Courses relation would violate the limited redundancy principle All of the related data for each class would be repeated for each student in that course Furthermore, all the related data for each student, such as ID number and e-mail address, it would be repeated for each course in which the student is enrolled Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 6 / 31

Relational Databases Clearly, the better solution is to create a new tables of classes, students, and professors Classes table Dept Number Sec Time Room Instr COMS 480 1 12:30 MWF B 020 Koether MATH 262 1 10:30 MWF B 020 Valente Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 7 / 31

Relational Databases Clearly, the better solution is to create a new tables of classes, students, and professors Students table First Last StuID Email Dorm Room Jimmy Jones 12345 jim@hscedu Ven 123 Johnny Smith 56789 john@hscedu CarpX 321 Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 8 / 31

Relational Databases Clearly, the better solution is to create a new tables of classes, students, and professors Instructors table First Last Bldg Room Koether Robb Bagby 114 Valente Tom Bagby 123 Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 9 / 31

Relational Databases Then create an Enroll table that contains ordered pairs of courses and students Enrolled table Dept Number Sec StuID COMS 480 1 56789 COMS 480 1 65432 MATH 262 1 23456 Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 10 / 31

Outline 1 Multiple Tables 2 MySQL 3 Creating a Database 4 MySQL Data Types 5 Assignment Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 11 / 31

MySQL SQL = Structured Query Language SQL was proposed in 1970 and developed in the early 1970s MySQL is an open-source version of SQL Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 12 / 31

MySQL Commands MySQL commands may be issued through a command line or from within a program (eg, C, C++, Java, PHP) When issued through the command line, all MySQL commands end with a semicolon When using MySQL through a programming language, a complication is that the internal structure of MySQL relations does not match any of the built-in datatypes Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 13 / 31

Outline 1 Multiple Tables 2 MySQL 3 Creating a Database 4 MySQL Data Types 5 Assignment Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 14 / 31

Creating a Database Creating a Database CREATE DATABASE db_name; The CREATE DATABASE command will create a database This creates a database with the name db_name, but it does not create any content Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 15 / 31

Creating a Database The Current Database USE db_name; To work with a database, we use the USE command to make it the current database This makes db_name the current database Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 16 / 31

Creating Tables Creating a Table CREATE TABLE table_name (attribute_list); The CREATE TABLE command will create a table (ie, a relation) within a database This creates the table table_name with the attributes specified in attribute_list Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 17 / 31

Creating Attributes To specify an attribute, we need to provide the name and the data type These may be followed by a number of modifiers that we will introduce shortly For example, the courses table will have four attributes: dept - the department name number - the course number title - the course title credit - the number of credit hours Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 18 / 31

Creating Attributes Creating an Attribute dept CHAR(4) The dept attribute would be described as CHAR(4) This states that the value of dept is a character string of length 4 Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 19 / 31

Creating Tables Creating a Table CREATE TABLE courses ( dept CHAR(4), number INT, title CHAR(64), credit INT ); The other attributes may be defined similarly Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 20 / 31

Creating Tables SHOW TABLES SHOW TABLES; +---------------------+ Tables_in_academics +---------------------+ courses +---------------------+ We can now use the SHOW TABLES command to see what tables exist Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 21 / 31

Creating Tables DESCRIBE DESCRIBE courses; +----------------------------------------------------+ Field Type Null Key Default Extra +----------------------------------------------------+ dept char(4) YES NULL number int(11) YES NULL title char(64) YES NULL credit int(11) YES NULL +----------------------------------------------------+ We can use the DESCRIBE command to see a detailed description of a table Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 22 / 31

Outline 1 Multiple Tables 2 MySQL 3 Creating a Database 4 MySQL Data Types 5 Assignment Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 23 / 31

MySQL Data Types Categories of data types Integer Fixed point Floating point Boolean String Time/Date Generic Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 24 / 31

Integer Types Standard integer types SMALLINT - 2-byte integers INT - 4-byte integers Extended integer types TINYINT - 1-byte integers MEDIUMINT - 3-byte integers BIGINT - 8-byte integers Each integer type may be either signed (default) or unsigned Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 25 / 31

Fixed-Point Types Standard fixed-point types DECIMAL - Whole numbers up to 10 digits DECIMAL(n) - Whole numbers up to n digits DECIMAL(n, d) - Real numbers with n digits, d of which are after the decimal point Fixed-point values are stored exactly Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 26 / 31

Floating-Point Types Standard floating-point types FLOAT - 4-byte floating-point numbers DOUBLE - 8-byte floating-point numbers Floating-point values are stored approximately Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 27 / 31

Boolean Types Standard boolean type BOOL is equivalent to TINYINT(1), ie, a one-digit integer The values 0 and 1 are interpreted as false and true, respectively The symbols FALSE and TRUE are equivalent to 0 and 1, respectively Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 28 / 31

String Types Standard string types CHAR(n) - Exactly n characters (0 < n 255) VARCHAR(n) - Up to n characters (0 < n 255) BINARY(n) - Exactly n bytes VARBINARY(n) - Up to n bytes TEXT - Arbitrary amount of character data BLOB - Arbitrary amount of byte data ENUM - Any one string from a specified list SET - Any number of strings from a specified list Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 29 / 31

Outline 1 Multiple Tables 2 MySQL 3 Creating a Database 4 MySQL Data Types 5 Assignment Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 30 / 31

Assignment Read Chapter 3 The Relational Data Model and Relational Database Constraints Read Section 41 SQL Data Definition and Data Types Review Questions 1-10, page 80 Exercises 14, 19, 20, page 80 Visit the websites http://devmysqlcom/doc/refman/56/en/create-databasehtml http://devmysqlcom/doc/refman/56/en/usehtml http://devmysqlcom/doc/refman/56/en/create-tablehtml http://devmysqlcom/doc/refman/56/en/data-typeshtml Robb T Koether (Hampden-Sydney College) MySQL Creating a DatabaseLecture 3 Mon, Jan 23, 2012 31 / 31