Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Main PDF 6 - MySQL Database

This document provides an overview of MySQL databases, including their structure, importance in web applications, and basic commands for managing databases and tables. It covers key concepts such as primary and foreign keys, normalization, and data manipulation language (DML) syntax. Additionally, it includes examples of creating, modifying, and deleting databases and tables, as well as querying data using SQL commands.

Uploaded by

Charles Uy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Main PDF 6 - MySQL Database

This document provides an overview of MySQL databases, including their structure, importance in web applications, and basic commands for managing databases and tables. It covers key concepts such as primary and foreign keys, normalization, and data manipulation language (DML) syntax. Additionally, it includes examples of creating, modifying, and deleting databases and tables, as well as querying data using SQL commands.

Uploaded by

Charles Uy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Applications Development and

Emerging Technologies
MODULE 6

MySQL Database
Objectives
• To provide a good background of Relational Database using
MySQL.
• To know the importance of Database in Web Application using
MySQL.
• To Identify the importance of Database Structure in constructing
tables.
• To be familiar with the syntax in managing users and database.
• To define a good structure of tables in a given database for data
storage.
• To be familiar in the common syntax of creating database and
tables and the correct data type to be used for each field.
Introduction to Databases
• Database – is an ordered collection of information
from which a computer program can quickly access.
• Information stored in computer databases is actually
stored in tables similar to spreadsheet.
• A record in a database is a single complete set of
related information.
• Fields are the individual categories of information
stored in a record.
Example: Employee Directory DB
Fields

last_name first_name address city state zip


Blair Dennis 204 Spruce Lane Brookfield MA 01506
Hernandez Louis 68 Boston Post Road Spencer MA 01562
Miller Erica 271 Baker Hill Road Brookfield MA 01515
Morinaga Scott 17 Ashley Road Brookfield MA 01515
Picard Raymond 1113 Oakham Road Barre MA 01531

Records (Row)

Also called as flat-file database that stores information in a single


table.
Stores information across multiple related tables.
Composed of Primary Table and Related Table (child table)
Primary key is a field that contains a unique identifier for
each record in a primary table.
Foreign key is a field in a related table that refers to the
primary key in a primary table.
Normalization is the process of breaking tables into multiple
related tables to reduce
redundant information.
 One important aspect of database management is its
querying capability.
 A query is a structured set of instructions and criteria for
retrieving, adding, modifying, and deleting database
information.
 Data Manipulation Language (DML) is use for creating
queries.
 Structured Query Language (SQL) has a standard data
manipulation language among many database
management system.
•Either as outgrowths of earlier academic research
(PostgreSQL)

• Developments of lightweight storage tools for websites


(MySQL)

•Open-sourced commercial products (InterBase)


• Most popular open source relational database
• An Australian academician named David Hughes
(Hughes Technologies) wrote a very lightweight
database engine called mSQL (short for mini SQL) -- it
didn't implement a number of features required for full
ANSI SQL certification, but it was very small and very
fast.
• mSQL was distributed as shareware
• incapable of doing a number of essential things -- like
joins
improvement was done by a Swedish programmer Monty
Widenius
MySQL rapidly grew until, although it's still a fast, light
database, it's also a pretty powerful one.
MySQL is written in C++, compiles using GNU gcc, and is
pretty portable -- it has been ported to OS/2, Windows 95 and
NT, as well as a variety of flavours of UNIX
According to the article of Ian Gilfillan in 2004, the
following are the claimed features of MySQL:
• High availability
• High scalability
• High performance
Connecting to MySQL : Console Base
• Console Base
mysql >

• Go to the Command Window


Start\Run
Type command or cmd

• Go to the MySQL path


C:\xampp\mysql\bin\mysql –u root –p
C:\xampp\mysql\bin\mysql –u root
• **Enter password if there is any.
Type commands in MySQL prompt which looks like this:
mysql >
MySQL Log In
MySQL Prompts
Prompt Meaning
mysql> Ready for a new command

-> Waiting for the next line of multiple line command.


‘> Waiting for next line, waiting for completion of a string that
began with single quote (‘ ‘ ‘).
“> Waiting for next line, waiting for completion of a string that
began in double quote (‘ “ ‘).
`> Waiting for next line, waiting for completion of an identifier that
began with a backtick (‘ ` ‘).
/*> Waiting for next line, waiting for completion of a comment that
began with /*.
Example
Basic Commands
• To clear command:
mysql > \c

 Get status from the server:


mysql > status

mysql > \s
Basic Commands
Basic Commands
• Need help?:
mysql > \h

mysql > ?

mysql > \?

mysql > help

 To display the SQL version:


mysql > select version();
Example
Example
Basic Commands
• Display current user:
mysql > select user();

 Display current date:


mysql > select current_date();

 Display today’s date and time:


mysql > select now()
Example
Basic Commands
• To quit
mysql > \q

mysql > quit

mysql > exit


MySQL
Data manipulation Language (DML)
Create, Select, Insert, Update, Delete
Database Creation
Syntax:
CREATE DATABASE <databasename>;
mysql> CREATE DATABASE DBStudent;

Showing and Using the Database:


mysql> SHOW databases;
Syntax: USE <database name>;
mysql> USE DBStudent;

Note: It should display Query OK, 1 row affected as a result.


Example
Table Creation
Syntax:
CREATE TABLE <table name> (field name field type(size)
condition...);

mysql> CREATE TABLE tblStudent(id int(5) primary key


not null auto_increment, name char(25));

Display the structure of the table:


Syntax: DESCRIBE <table name>;
mysql > DESCRIBE tblStudent;
Displaying the list of table:
mysql> SHOW tables;
Sample Screen
Adding of Primary Key
mysql> create table primaryTable (id int not
null auto_increment, name varchar(30),
primary key (id));

mysql> create table keyTable (id int not null


auto_increment, name varchar(30), key (id));

mysql> create table uniqueTable (id int not


null auto_increment, name varchar(30),
unique key (id));
Field Data Types
Type Range Storage
BOOL -128 to 127; 0 is considered false 1 byte
INT or INTEGER -2147483648 to 2147483647 4 bytes

FLOAT A small number with a floating decimal point. 4 bytes


DOUBLE A large number with a floating decimal point. 4 bytes
DATE YYYY-MM-DD Varies
TIME HH:MM:SS Varies
CHAR(m) Fixed length string between 0 to 255 characters Number of bytes
specified by m
VARCHAR(m) Variable length string between 1 to 65,535 Varies according
characters to the number of
bytes specified by
m.
The ‘show’ Command
• This time you can show additional information of a
table
mysql > SHOW COLUMNS FROM tblStudent;

mysql > SHOW TABLE STATUS;

mysql > SHOW TABLE STATUS \G;

mysql > SHOW TABLE STATUS LIKE ‘tblStudent’ \G;

mysql > SHOW CREATE TABLE tblStudent \G;


Example
Example
Modifying the Table Structure
• Adding new column
Syntax: ALTER TABLE <table name> ADD <column
name> <data type(size)>;
mysql > ALTER TABLE tblStudent ADD gender
char(1);
Modifying Table Structure
• Changing Field Type Sizes
Syntax: ALTER TABLE <table name> MODIFY <column
name> <data type(size)>;
mysql > ALTER TABLE tblStudent MODIFY gender
char(7);
Modifying Table Structure
• Changing Field Names
Syntax: ALTER TABLE <table name> CHANGE <column
name> <new column name> <data type(size)>;

mysql > ALTER TABLE tblStudent CHANGE gender


sex char(7);
Modifying Table Structure
• Dropping column
Syntax: ALTER TABLE <table name> DROP <column
name>;
mysql > ALTER TABLE tblStudent DROP sex;
Inserting Values to your Table
Syntax: INSERT INTO <table name> VALUES
(value1, value2, value3, ....);
mysql > INSERT INTO tblStudent VALUES (10001,
‘Juan Dela Cruz’);

To view use ‘SELECT’ command:


Syntax: SELECT * FROM <table name>;
mysql> SELECT * FROM tblStudent;
Example
Inserting Values in a Specific Column
Syntax: INSERT INTO <table name> (column name)
VALUES (value);
mysql> INSERT INTO tblStudent (name) VALUES
(‘Ma. Conchita Borromeo’);
Other Structure
mysql> INSERT INTO tblStudent (id,name) VALUES
(10003,’Mar Roxas’);

mysql> INSERT INTO tblStudent (name,id) VALUES


(’Jojo Binay’,10004);
Updating the Records
Syntax: UPDATE <table name> SET <column name> =
<value> WHERE <column name> = <value>;
mysql> UPDATE tblStudent SET name=‘Ma.
Conchita Dimagiba’ WHERE id=‘10002’;
Updating Multiple Columns
Syntax: UPDATE <table name> SET <column name> =
<value>, <column name> = <value> WHERE <column
name> = <value>;
mysql> UPDATE tblStudent SET name=‘Juan Dela
Peña’, gender=‘M’ WHERE id=‘10001’;
Deleting of Columns
Syntax: DELETE FROM <table name> WHERE <column
name> = <value>;
mysql> DELETE FROM tblStudent WHERE
id=‘10002’;
Deleting all data in a table
Syntax: TRUNCATE TABLE <table name>
or
mysql> TRUNCATE TABLE tblStudent;

mysql> DELETE FROM tblStudent;


To Delete a Table
Syntax: DROP TABLE <table name>;
mysql> DROP TABLE tblStudent;

* You remove a table from the database when it is not required. You
use the DROP TABLE statement to remove a table. When you remove
a table, all the data from the table is also deleted.
To Delete Database
Syntax: DROP DATABASE <database name>;
mysql> DROP DATABASE dbStudent;

You might also like