Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Tonight’s Lecture chapter 3. A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online"— Presentation transcript:

1 Tonight’s Lecture chapter 3

2 A Guide to MySQL2 Using MySQL Reference Manual to Get Help Can access online http://dev.mysql.com/doc/refman/5.0/en/

3 A Guide to MySQL3 Describing a Table

4 In Oracle DESC table_name example: DESC h_book

5 DDL Stands for Data Definition Language – create statements – drop statements – alter statements

6 A Guide to MySQL6 Creating a Database Must create a database before creating tables Use CREATE DATABASE command Include database name

7 A Guide to MySQL7 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

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

9 A Guide to MySQL9 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

10 A Guide to MySQL10 Table and Column Name Restrictions Names cannot exceed 18 characters Must start with a letter Can contain letters, numbers, and underscores (_) Cannot contain spaces

11 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

12 A Guide to MySQL12 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

13 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) ) FieldTypeNullKeyDefaultExtra MARINA_NUMchar(4)NOPRI NAMEchar(20)YES ADDRESSchar(15)YES CITYchar(15)YES STATEchar(2)YES ZIPchar(5)YES What will this SQL statement do?

14 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) ) FieldTypeNullKeyDefaultExtra MARINA_NUMchar(4)NOPRI NAMEchar(20)YES ADDRESSchar(15)YES CITYchar(15)YES STATEchar(2)YES ZIPchar(5)YES What will this SQL statement do?

15 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) ) FieldTypeNullKeyDefaultExtra MARINA_NUMchar(4)NO NAMEchar(20)YES ADDRESSchar(15)YES CITYchar(15)YES STATEchar(2)YES ZIPchar(5)YES What will this SQL statement do?

16 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

17 FieldTypeNullKeyDefaultExtra OWNER_NUMchar(4)NOPRI LAST_NAMEchar(50)YES FIRST_NAMEchar(20)YES ADDRESSchar(15)YES CITYchar(15)YES STATEchar(2)YES ZIPchar(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) )

18 FieldTypeNullKeyDefaultExtra employee_idvarchar(10)YES namevarchar(100)YES manager_idvarchar(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:

19 FieldTypeNullKeyDefaultExtra ORDER_NUMchar(5)NOPRI PART_NUMchar(4)NOPRI NUM_ORDEREDdecimal(3,0)YES QUOTED_PRICEdecimal(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:

20 FieldTypeNullKeyDefaultExtra BOOK_CODEchar(4)NOPRI BRANCH_NUMdecimal(2,0)NOPRI0 ON_HANDdecimal(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:

21 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: FieldTypeNullKeyDefaultExtra BOOK_CODEchar(4)NOPRI TITLEchar(40)YES PUBLISHER_CODEchar(3)YES TYPEchar(3)YES PRICEdecimal(4,2)YES PAPERBACKchar(1)YES

22 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: FieldTypeNullKeyDefaultExtra REP_NUMchar(2)NOPRI LAST_NAMEchar(15)YES FIRST_NAMEchar(15)YES STREETchar(15)YES CITYchar(15)YES STATEchar(2)YES ZIPchar(5)YES COMMISSIONdecimal(7,2)YES RATEdecimal(3,2)YES

23 A Guide to MySQL23 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

24 drop table a_owner be careful with drop statements

25 What is DDL?

26 A Guide to MySQL26 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

27 A Guide to MySQL27 The Insert Command


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

Similar presentations


Ads by Google