Chapter01 Lesson04
Chapter01 Lesson04
Chapter01 Lesson04
In this lesson, you will learn how to manage databases in MySQL. You
will learn how to create new databases, remove existing databases, and
display all databases in the MySQL database server.
Creating Database
Before doing anything else with the data, you need to create a
database. A database is a container of data. It stores contacts, vendors,
customers or any kind of data that you can think of. In MySQL, a
database is a collection of objects that are used to store and
manipulate data such as tables, database views, triggers,
stored procedures, etc.
How to Create DB
• To create a database in MySQL, you use the CREATE DATABASE
statement as follows:
SHOW DATABASES;
Selecting a database to work with
Before working with a particular database, you must tell MySQL which
database you want to work with by using the USE statement.
USE database_name;
Removing Databases
Removing database means you delete the database physically. All the
data and associated objects inside the database are permanently
deleted and this cannot be undone. Therefore, it is very important to
execute this query with extra cautions.
To delete a database, you use the DROP DATABASE statement as
follows:
DATE YYYY-MM-DD.
DATETIME YYYY-MM-DD HH:MM:SS.
TIMESTAMP YYYY-MM-DD HH:MM:SS.
TIME HH:MM:SS.
YEAR A year value in CCYY or YY format
Date and Time Data Types (Conti…)
• -Two-digit Year Support
MySQL pre-5.6.6
00-69=2000-2060
70-99=1970-1999
MySQL 5.6.6 and later
Deprecated
• -Standard SQL Date and Time format
2015-07-14 18:24:42 DATETIME YYYY-MM-DD HH:MM:SS.
• -Time Zone
SHOW VARIABLES LIKE ‘%time_zone%’;
SELECT Now();
SET time_zone=’US/Eastern’;
BOOLEAN DATA TYPE
• 0=False
• 1=True
CREATE TABLE
Or
ALTER TABLE t1
ADD PRIMARY KEY(id);
Foreign Key Constraint
• A foreign key is a field in a table
that matches another field of
another table. A foreign key
places constraints on data in the
related tables, which enables
MySQL to maintain referential
integrity.
Defining MySQL FOREIGN KEY
Constraints
• using CREATE TABLE statement
• using ALTER TABLE statement
• Syntax:
• Syntax: