+ All Categories
Home > Documents > A Guide to MySQL 3. 2 Introduction Structured Query Language (SQL): Popular and widely used...

A Guide to MySQL 3. 2 Introduction Structured Query Language (SQL): Popular and widely used...

Date post: 04-Jan-2016
Category:
Upload: julius-simpson
View: 216 times
Download: 1 times
Share this document with a friend
34
A Guide to MySQL 3
Transcript
Page 1: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL

3

Page 2: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL2

Introduction

Structured Query Language (SQL):

Popular and widely used language for retrieving and manipulating database data

Developed in mid-1970s under the name SEQUEL

Renamed SQL in 1980

Used by most DBMSs

Page 3: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL3

Starting MySQL

Windows XP Click Start button Point to All Programs Point to MySQL on menu Point to MySQL Server 4.1 Click MySQL Command Line Client

Must enter password in Command Line Client window

Page 4: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL4

Obtaining Help in MySQL

Type \h at MySQL> prompt

Type “help” followed by name of command

help contents

help union

Page 5: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL5

Page 6: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL6

Page 7: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL7

Creating a Database

Must create a database before creating tables Use CREATE DATABASE command Include database name

Page 8: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL8

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 9: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

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

Page 10: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

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

Page 11: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL11

Creating the REP Table

Page 12: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL12

Entering Commands in MySQL

Commands are free-format; no rules stating specific words in specific positions

Press ENTER to move to the next line in a command

Indicate the end of a command by typing a semicolon

Commands are not case sensitive

Page 13: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL13

Running SQL Commands

Page 14: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL14

Editing SQL Commands

Statement history: stores most recently used command

Editing commands: Use arrow keys to move up, down, left, and right Use Ctrl+A to move to beginning of line Use Ctrl+E to move to end of line Use Backspace and Delete keys

Page 15: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL15

Errors in SQL Commands

Page 16: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL16

Editing MySQL Commands

Press Up arrow key to go to top line

Press Enter key to move to next line if line is correct

Use Right and Left arrow keys to move to location of error

Press ENTER key when line is correct

If Enter is not pressed on a line, line not part of the revised command

Page 17: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL17

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 18: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL18

Data Types For each table column, type of data must be defined

Common data types:

CHAR(n)

VARCHAR(n)

DATE

DECIMAL(p,q)

INT

SMALLINT

Page 19: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL19

Nulls

A special value to represent situation when actual value is not known for a column

Can specify whether to allow nulls in the individual columns

Should not allow nulls for primary key columns

Page 20: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL20

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 21: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL21

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 22: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL22

The Insert Command

•To add new rows modify previous INSERT command•Use same editing techniques as those used to correct errors

Page 23: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL23

Adding Additional Rows

Page 24: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL24

The INSERT Command with Nulls

Use a special format of INSERT command to enter a null value in a table

Identify the names of the columns that accept non-null values, then list only the non-null values after the VALUES command

Enter only non-null values Precisely indicate values you are entering by listing the

columns

Page 25: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL25

The INSERT Command with Nulls (continued)

Page 26: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL26

Viewing Table Data

Use SELECT command to display all the rows and columns in a table

SELECT * FROM followed by the name of the table

Ends with a semicolon

Page 27: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL27

Viewing Table Data (continued)

Page 28: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL28

Correcting Errors In the Database

UPDATE command is used to update a value in a table

DELETE command allows you to delete a record

INSERT command allows you to add a record

Page 29: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL29

Correcting Errors in the Database (continued)

Page 30: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL30

Correcting Errors in the Database (continued)

Page 31: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL31

Saving SQL Commands

Allows you to use commands again without retyping

Different methods for each SQL implementation you are using

Oracle SQL*Plus and SQL*Plus Worksheet use a script file

Access saves queries as objects

MySQL uses an editor to save text files

Page 32: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL32

Saving SQL Commands

Script file: File containing SQL commands Use a text editor or word processor to create Save with a .txt file name extension Run in MySQL:

SOURCE file name \. file name

Include full path if file is in folder other than default

Page 33: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL33

Creating the Remaining Database Tables

Execute appropriate CREATE TABLE and INSERT commands

Save these commands to a secondary storage device

Page 34: A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.

A Guide to MySQL34

Describing a Table


Recommended