Top Banner
Tonight’s Lecture chapter 3
27

Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

Dec 16, 2015

Download

Documents

London Howett
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

Tonight’s Lecture

chapter 3

Page 2: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

A Guide to MySQL 2

Using MySQL Reference Manual to Get Help

• Can access online

• http://dev.mysql.com/doc/refman/5.0/en/

Page 3: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

A Guide to MySQL 3

Describing a Table

Page 4: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

In Oracle

DESC table_name

example: DESC h_book

Page 5: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

DDL

• Stands for Data Definition Language– create statements– drop statements– alter statements

Page 6: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

A Guide to MySQL 6

Creating a Database

• Must create a database before creating tables

• Use CREATE DATABASE command

• Include database name

Page 7: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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

Page 8: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

You’re changing your default database by selecting your user database from the drop down in PhpMyAdmin

Page 9: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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

Page 10: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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

Page 11: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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

Page 12: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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

Page 13: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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?

Page 14: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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?

Page 15: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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?

Page 16: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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

Page 17: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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

Page 18: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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:

Page 19: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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:

Page 20: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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:

Page 21: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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

Page 22: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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

Page 23: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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

Page 24: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

drop table a_owner

be careful with drop statements

Page 25: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

What is DDL?

Page 26: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

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

Page 27: Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online

A Guide to MySQL 27

The Insert Command