0% found this document useful (0 votes)
3 views

Ch4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Ch4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

University of Somalia (UNISO)

Faculty of Engineering & Computer Science

COURSE: PHP AND MYSQL


Chapter 4: Introduction to
MYSQL
Eng. Ismail Mohamed Jamal
MCSE (Master of Computer Science & Engineering)
Ondokuz Mayis University- Turkiye (OMU)
Email: engismailmj@gmail.com

1
Introduction

• MySQL is the world’s most popular open-source database application


(according to MySQL’s Web site, www.mysql.com) and is commonly
used with PHP.

• The MySQL software comes with the database server (which stores
the actual data), different client applications (for interacting with the
database server), and several utilities.

2
Naming Data
Base element

• Before you start working with databases, you have to identify


your needs. The purpose of the application (or Web site, in
this case) dictates how the database should be designed.

• With that in mind, the examples in this chapter and the next
will use a database that stores some user registration
information.

3
Cont.

When creating databases and tables, you should come up with names
(formally called identifiers) that are clear, meaningful, and easy to type. Also,
identifiers:

◆ Should only contain letters, numbers, and the underscore (no spaces)

◆ Should not be the same as an existing keyword (like an SQL term or a


function name)

◆ Should be treated as case-sensitive

◆ Cannot be longer than 64 characters (approximately)

◆ Must be unique within its realm

4
name a database’s elements:

1. Determine the database’s name


2. Determine the table names.
3. Determine the column names for each table.
Let’s us design users table as an example.

Column Name

User_ID

First_Name

Last_Name

Email

Pass

5
hoosing your column types

Once you have identified all of the tables and columns that the database
will need, you should determine each column’s data

type. When creating a table, MySQL requires that you explicitly state what
sort of information each column will contain. There are three primary
types, which is true for almost every database application:

◆ Text (aka strings)

◆ Numbers

◆ Dates and times

6
Other MySQL Data types

CHAR[Length], VARCHAR[Length], TINYTEXT, TEXT,

MEDIUMINT, MEDIUMINT[Length], BIGINT[Length],

FLOAT[Length, Decimals], DOUBLE[Length, Decimals],

DECIMAL[Length, Decimals], DATE, DTETIME, TIMESTAMP,

TIME.

7
o select the column types:

1. Identify whether a column should be a text, number, or


date/time type.
Column Name Type

User_ID Number

First_Name Text

Last_Name Text

Email Text

Pass Text

RegDate Date/time

8
Cont.

2. Choose the most appropriate subtype for each column.


3. Set the maximum length for text columns
Column Name Type

User_ID MEDIUMINT

First_Name VARCHAR(15)

Last_Name VARCHAR(40)

Email VARCHAR(40)

Pass CHAR(40)

RegDate Date/time

9
Char Vs. Varchar

• Both of these types store strings and can be set with a


maximum length.

• One primary difference between the two is that anything


stored as a CHAR will always be stored as a string the length
of the column (using spaces to pad it; these spaces will be
removed when you retrieve the stored value from the
database). Conversely,

10
Cont.

• strings stored in a VARCHAR column will require only as much

space as the string itself.

• So the word cat in a VARCHAR(10) column requires four bytes

of space (the length of the string plus 1), but in a CHAR(10)

column, that same word requires 10 bytes of space.

11
Tips

• The TIMESTAMP field type is automatically set as the current

date and time when an INSERT or UPDATE occurs, even if no

value

• is specified for that particular field. If a table has multiple

TIMESTAMP columns, only the first one will be updated when

an INSERT or UPDATE is performed.

12
oosing other column properties

• First, every column, regardless of type, can be defined as NOT NULL.

The NULL value, in databases and programming, is equivalent to

saying that the field has no value.

• Ideally, in a properly designed database, every column of every row in

every table should have a value, but that isn’t always the case.

• To force a field to have a value, add the NOT NULL description to its

column type. For example, a require dollar amount can be described

as cost DECIMAL(5,2) NOT NULL

13
Cont.

• When creating a table, you can also specify a default value for any

column, regardless of type. In cases where a majority of the records will

have the same value for a column, presetting

• a default will save you from having to specify a value when inserting

new rows (unless that row’s value for that column is

different from the norm).

• With the gender column, if no value is specified when adding a record,

the default will be used.

14
Cont.

• If a column does not have a default value and one is not

specified for a new record, that field will be given a NULL

value. However, if no value is specified and the column is

defined as NOT NULL, an error will occur.

15
Cont.

• Finally, when designing a database, you’ll need to consider


creating indexes, adding keys, and using the
AUTO_INCREMENT property.

• Chapter 6 discusses these concepts in greater detail, but in


the meantime, check out the sidebar “Indexes, Keys, and
AUTO_INCREMENT” to learn how they affect the users table.

16
finish defining your columns:

1. Identify your primary key.

2. Identify which columns cannot have a NULL value.

3. Make any numeric type UNSIGNED if it won’t ever store


negative numbers.

4. Establish the default value for any column.

5. Confirm the final column definitions

17
exes, Keys, and AUTO_INCREMENT

• Two concepts closely related to database design are indexes and keys. An
index in a database is a way of requesting that the database keep an eye
on the values of a specific column or combination of columns (loosely
stated).

• A key in a database table is integral to the normalization process used for


designing more complicated databases.

• There are two types of keys: primary and foreign.

• Each table should have one primary key, and the primary key in one
table is often linked as a foreign key in another.

18
Any Questions?

19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy