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