tonight’s lecture chapter 3. a guide to mysql2 using mysql reference manual to get help can access...
TRANSCRIPT
Tonight’s Lecture
chapter 3
A Guide to MySQL 2
Using MySQL Reference Manual to Get Help
• Can access online
• http://dev.mysql.com/doc/refman/5.0/en/
A Guide to MySQL 3
Describing a Table
In Oracle
DESC table_name
example: DESC h_book
DDL
• Stands for Data Definition Language– create statements– drop statements– alter statements
A Guide to MySQL 6
Creating a Database
• Must create a database before creating tables
• Use CREATE DATABASE command
• Include database name
A Guide to MySQL 7
Changing the Default Database
• Default database: database to which all subsequent commands pertain
• USE command, followed by database name:– Changes the default database
– Execute at the start of every session
You’re changing your default database by selecting your user database from the drop down in PhpMyAdmin
A Guide to MySQL 9
Creating a Table• Describe the layout of each table in the database
• Use CREATE TABLE command
• TABLE is followed by the table name
• Follow this with the names and data types of the columns in the table
• Data types define type and size of data
A Guide to MySQL 10
Table and Column Name Restrictions
• Names cannot exceed 18 characters
• Must start with a letter
• Can contain letters, numbers, and underscores (_)
• Cannot contain spaces
Avoid using reserved words as names of your database objects
Reserved words can differ from database platform to database platform
MySQL reserved word examples - select- numeric- database- group
A Guide to MySQL 12
Implementation of Nulls
• Use NOT NULL clause in CREATE TABLE command to exclude the use of nulls in a column
• Default is to allow null values
• If a column is defined as NOT NULL, system will reject any attempt to store a null value there
CREATE TABLE a_marina( MARINA_NUM char(4) NOT NULL, NAME char(20) NULL, ADDRESS char(15) NULL, CITY char(15) NULL, STATE char(2) NULL, ZIP char(5) NULL, PRIMARY KEY (MARINA_NUM))
Field Type Null Key Default ExtraMARINA_NUM char(4) NO PRI NAME char(20) YES ADDRESS char(15) YES CITY char(15) YES STATE char(2) YES ZIP char(5) YES
What will this SQL statement do?
CREATE TABLE a_marina( MARINA_NUM char(4) NOT NULL, NAME char(20), ADDRESS char(15), CITY char(15), STATE char(2), ZIP char(5), PRIMARY KEY (MARINA_NUM))
Field Type Null Key Default ExtraMARINA_NUM char(4) NO PRI NAME char(20) YES ADDRESS char(15) YES CITY char(15) YES STATE char(2) YES ZIP char(5) YES
What will this SQL statement do?
CREATE TABLE a_marina( MARINA_NUM char(4) NOT NULL, NAME char(20), ADDRESS char(15), CITY char(15), STATE char(2), ZIP char(5))
Field Type Null Key Default ExtraMARINA_NUM char(4) NO NAME char(20) YES ADDRESS char(15) YES CITY char(15) YES STATE char(2) YES ZIP char(5) YES
What will this SQL statement do?
CREATE TABLE a_marina( MARINA_NUM char(4) NOT NULL, NAME char(20), ADDRESS char(15), CITY char(15), STATE char(2), ZIP)
What will this SQL statement do?
ERROR
We did not specify a datatype for the column ZIP
Field Type Null Key Default ExtraOWNER_NUM char(4) NO PRI LAST_NAME char(50) YES FIRST_NAME char(20) YES ADDRESS char(15) YES CITY char(15) YES STATE char(2) YES ZIP char(5) YES
Write the SQL that will create an A_OWNER table with the following structure:
CREATE TABLE a_owner( OWNER_NUM char(4) NOT NULL, LAST_NAME char(50) NULL, FIRST_NAME char(20) NULL, ADDRESS char(15) NULL, CITY char(15) NULL, STATE char(2) NULL, ZIP char(5) NULL, PRIMARY KEY (OWNER_NUM))
Field Type Null Key Default Extraemployee_id varchar(10) YES name varchar(100) YES manager_id varchar(10) YES
CREATE TABLE employees( employee_id varchar(10) NULL, name varchar(100) NULL, manager_id varchar(10) NULL)
Write the SQL that will create an EMPLOYEES table with the following structure:
Field Type Null Key Default ExtraORDER_NUM char(5) NO PRI PART_NUM char(4) NO PRI NUM_ORDERED decimal(3,0) YES QUOTED_PRICE decimal(6,2) YES
CREATE TABLE p_order_line( ORDER_NUM char(5) NOT NULL, PART_NUM char(4) NOT NULL, NUM_ORDERED decimal(3,0) NULL, QUOTED_PRICE decimal(6,2) NULL, PRIMARY KEY (ORDER_NUM,PART_NUM))
Write the SQL that will create an P_ORDER_LINE table with the following structure:
Field Type Null Key Default ExtraBOOK_CODE char(4) NO PRI BRANCH_NUM decimal(2,0) NO PRI 0 ON_HAND decimal(2,0) YES
CREATE TABLE h_inventory( BOOK_CODE char(4) NOT NULL, BRANCH_NUM decimal(2,0) NOT NULL default 0, ON_HAND decimal(2,0) NULL, PRIMARY KEY (BOOK_CODE,BRANCH_NUM))
Write the SQL that will create an H_INVENTORY table with the following structure:
CREATE TABLE h_book( BOOK_CODE char(4) NOT NULL, TITLE char(40) NULL, PUBLISHER_CODE char(3) NULL, TYPE char(3) NULL, PRICE decimal(4,2) NULL, PAPERBACK char(1) NULL, PRIMARY KEY (BOOK_CODE))
Write the SQL that will create an H_BOOK table with the following structure:
Field Type Null Key Default ExtraBOOK_CODE char(4) NO PRI TITLE char(40) YES PUBLISHER_CODE char(3) YES TYPE char(3) YES PRICE decimal(4,2) YES PAPERBACK char(1) YES
CREATE TABLE p_rep( REP_NUM char(2) NOT NULL, LAST_NAME char(15) NULL, FIRST_NAME char(15) NULL, STREET char(15) NULL, CITY char(15) NULL, STATE char(2) NULL, ZIP char(5) NULL, COMMISSION decimal(7,2) NULL, RATE decimal(3,2) NULL, PRIMARY KEY (REP_NUM))
Write the SQL that will create an P_REP table with the following structure:
Field Type Null Key Default ExtraREP_NUM char(2) NO PRI LAST_NAME char(15) YES FIRST_NAME char(15) YES STREET char(15) YES CITY char(15) YES STATE char(2) YES ZIP char(5) YES COMMISSION decimal(7,2) YES RATE decimal(3,2) YES
A Guide to MySQL 23
Dropping a Table
• Can correct errors by dropping (deleting) a table and starting over
• Useful when table is created before errors are discovered
• Command is followed by the table to be dropped and a semicolon
• Any data in table also deleted
drop table a_owner
be careful with drop statements
What is DDL?
A Guide to MySQL 26
Adding Rows to a Table
• INSERT command:
– INSERT INTO followed by table name
– VALUES command followed by specific values in parentheses
– Values for character columns in single quotation marks
A Guide to MySQL 27
The Insert Command