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

What Is MySQL

Relational database management systems (RDBMS) store data in tables with relationships established using primary and foreign keys. RDBMS like MySQL are based on SQL and the relational model. MySQL is a popular open-source RDBMS that allows users to define, access, and manipulate relational databases. MySQL supports various data types including strings, numerics, and dates for storing different types of data.

Uploaded by

Doaa Rezk
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

What Is MySQL

Relational database management systems (RDBMS) store data in tables with relationships established using primary and foreign keys. RDBMS like MySQL are based on SQL and the relational model. MySQL is a popular open-source RDBMS that allows users to define, access, and manipulate relational databases. MySQL supports various data types including strings, numerics, and dates for storing different types of data.

Uploaded by

Doaa Rezk
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Relational Database Management Systems

Nowadays, we use relational database management systems (RDBMS) to store


and manage huge volume of data. This is called relational database because all
the data is stored into different tables and relations are established using primary
keys or other keys known as foreign keys.

RDBMS is the basis for SQL and for all modern database systems like MS SQL
Server, IBM DB2, Oracle, MySQL, and Microsoft Access.

A Relational database management system (RDBMS) is a database


management system (DBMS) that is based on the relational model.

A Relational DataBase Management System (RDBMS) is a software that −

 Enables you to implement a database with tables, columns and indexes.


 Guarantees the Referential Integrity between rows of various tables.
 Updates the indexes automatically.
 Interprets an SQL query and combines information from various tables.

RDBMS Terminology

Before we proceed to explain the MySQL database system, let us revise a


few definitions related to the database.

 Database − A database is a collection of tables, with related data.

 Table − A table is a matrix with data. A table in a database looks


like a simple spreadsheet.

 Column − One column (data element) contains data of one and the same
kind, for example the column postcode.

 Row − A row (= tuple, entry or record) is a group of related data, for example
the data of one subscription.

 Primary Key − A primary key is unique. A key value cannot occur twice in
one table. With a key, you can only find one row. AutoNumber guarantees
uniqueness but does not carry any useful information

 Foreign Key − A foreign key is the linking pin between two tables. No
uniqueness constraint for foreign keys

 Compound Key − A compound key (composite key) is a key that consists of


multiple columns, because one column is not sufficiently unique.

 Index − An index in a database resembles an index at the back of a book.

 Referential Integrity − Referential Integrity makes sure that a foreign key


value always points to an existing row.

Relation between primary and foreign keys


 Same format
 Same values

Enforcing referential integrity


 Cascade Update Related Fields – the values of foreign keys change following
changes of the values of the primary key
 Cascade Delete Related Records – deleting a record from the primary field in
a relationship causes a deletion of all related records in the second table

What is MySQL?
MySQL is the most popular Open Source Relational SQL Database Management System
that is developed and supported by Oracle Corporation. MySQL is one of the best
RDBMS being used for developing various web-based software applications.
Why MySQL?
1. Allows users to access data in relational database management systems.

2. Allows users to describe the data.

3. Allows users to define the data in database and manipulate that data.

4. Allows to embed within other languages using SQL modules, libraries &
pre-compilers.

5. Allows users to create and drop databases and tables.

MySQL Database
MySQL is a fast, easy-to-use RDBMS being used for many small and big businesses.
MySQL is developed, marketed and supported by MySQL AB, which is a Swedish
company. MySQL is becoming so popular because of many good reasons:-
 MySQL is released under an open-source license. So you have nothing to
pay to use it.

 MySQL is a very powerful program in its own right. It handles a large


subset of the functionality of the most expensive and powerful database packages.
 MySQL uses a standard form of the well-known SQL data language.

 MySQL works on many operating systems and with many languages


including PHP, PERL, C, C++, JAVA, etc.
 MySQL works very quickly and works well even with large data sets.

 MySQL is very friendly to PHP, the most appreciated language for web
development.

 MySQL supports large databases, up to 50 million rows or more in a


table. The default file size limit for a table is 4GB, but you can increase this (if
your operating system can handle it) to a theoretical limit of 8 million terabytes
(TB).
 MySQL is customizable. The open-source GPL license allows
programmers to modify the MySQL software to fit their own specific
environments.
MySQL data types

In MySQL there are three main data types: string, numeric, and date and time.

1- String data types:

In MySQL, a string can hold anything from plain text to binary data such as images or
files. Strings can be compared and searched based on pattern matching
by using the LIKE operator, regular expression, and full-text search.

Data type Description

A FIXED length string (can contain letters, numbers, and special


CHAR(size) characters). The size parameter specifies the column length in
characters - can be from 0 to
255. Default is 1
A VARIABLE length string (can contain letters, numbers, and
VARCHAR(size) special characters). The size parameter specifies the maximum
column length in characters - can be from 0 to 65535
Equal to CHAR(), but stores binary byte strings. The size
BINARY(size)
parameter specifies the column length in bytes. Default is 1
Equal to VARCHAR(), but stores binary byte strings. The size
VARBINARY(size)
parameter specifies the maximum column length in bytes.
TINYBLOB For BLOBs (Binary Large OBjects). Max length: 255 bytes
TINYTEXT Holds a string with a maximum length of 255 characters
TEXT(size) Holds a string with a maximum length of 65,535 bytes

BLOB(size) For BLOBs (Binary Large OBjects). Holds up to 65,535 bytes of


data
MEDIUMTEXT Holds a string with a maximum length of 16,777,215 characters
MEDIUMBLOB For BLOBs (Binary Large OBjects). Holds up to 16,777,215
bytes of data
LONGTEXT Holds a string with a maximum length of 4,294,967,295
characters
LONGBLOB For BLOBs (Binary Large OBjects). Holds up to 4,294,967,295
bytes of data
A string object that can have only one value, chosen from a list of
possible values. You can list up to 65535 values in an ENUM
ENUM(val1, val2, val3, ...)
list. If a value is inserted that is not in the list, a blank value will
be inserted. The values are sorted in
the order you enter them

A string object that can have 0 or more values, chosen from a list
SET(val1, val2, val3, ...)
of possible values. You can list up to 64 values in a SET list
2- Numeric data types:

In MySQL, you can find all SQL standard numeric types including exact number data
type and approximate numeric data types including integer, fixed-point and floating-
point.

All the numeric data types may have an extra option: UNSIGNED or ZEROFILL. If you
add the UNSIGNED option, MySQL disallows negative values for the column. If you add
the ZEROFILL option, MySQL automatically also adds the UNSIGNED attribute to the
column.

Data type Description


A bit-value type. The number of bits per value is specified in size. The
BIT(size)
size parameter can hold a value from 1 to 64. The default value for
size is 1.
A very small integer. Signed range is from -128 to 127. Unsigned
TINYINT(size)
range is from 0 to
255. The size parameter specifies the maximum display width (which
is 255)
BOOL Zero is considered as false, nonzero values are considered as true.
BOOLEAN Equal to BOOL
A small integer. Signed range is from -32768 to 32767. Unsigned
SMALLINT(size)
range is from 0 to 65535. The size parameter specifies the maximum
display width (which is 255)
A medium integer. Signed range is from -8388608 to 8388607.
MEDIUMINT(size) Unsigned range is from 0 to 16777215. The size parameter specifies
the maximum display width (which is 255)
A medium integer. Signed range is from -2147483648 to 2147483647.
INT(size) Unsigned range is from 0 to 4294967295. The size parameter specifies
the maximum display width (which is 255)
INTEGER(size) Equal to INT(size)
A large integer. Signed range is from -9223372036854775808 to
BIGINT(size) 9223372036854775807. Unsigned range is from 0 to
18446744073709551615. The size parameter specifies the maximum
display width (which is 255)
A floating point number. The total number of digits is specified in
FLOAT(size, d) size. The number of digits after the decimal point is specified in the d
parameter. This syntax is deprecated in MySQL 8.0.17, and it will be
removed in future MySQL versions
A floating point number. MySQL uses the p value to determine
FLOAT(p) whether to use FLOAT or DOUBLE for the resulting data type. If p is
from 0 to 24, the data type becomes FLOAT(). If p is from 25 to 53,
the data type becomes DOUBLE()
A normal-size floating point number. The total number of digits is
DOUBLE(size, d)
specified in size. The number of digits after the decimal point is
specified in the d parameter
An exact fixed-point number. The total number of digits is specified
in size. The number of digits after the decimal point is specified in the
DECIMAL(size, d)
d parameter. The maximum number for size is 65. The maximum
number for d is 30. The default value for size is
10. The default value for d is 0.
DEC(size, d) Equal to DECIMAL(size,d)
MySQL does not have the built-in BOOLEAN or BOOL data type. To represent boolean
values, MySQL uses the smallest integer type which is TINYINT(1).
In other words, BOOLEAN and BOOL are synonyms for TINYINT(1).

3- Date and Time data types

MySQL provides types for date and time as well as the combination of date and time. In
addition, MySQL supports the timestamp data type for tracking the changes in a row of a
table. If you just want to store years without dates and months, you can use the YEAR
data type.

Data type Description


A date. Format: YYYY-MM-DD. The supported range is from '1000-
DATE
01-01' to '9999-12-31'
A date and time combination. Format: YYYY-MM-DD hh:mm:ss. The
supported range is from '1000-01-01 00:00:00' to '9999-12-31
DATETIME(fsp)
23:59:59'. Adding DEFAULT and ON UPDATE in the column
definition to get automatic initialization and updating to the current date
and time
A timestamp. TIMESTAMP values are stored as the number of seconds
since the Unix epoch ('1970-01-01 00:00:00' UTC). Format: YYYY-
MM-DD hh:mm:ss. The supported range is from '1970-01-01 00:00:01'
TIMESTAMP(fsp)
UTC to '2038-01-09 03:14:07' UTC. Automatic initialization and
updating to the current date and time can be specified using DEFAULT
CURRENT_TIMESTAMP and ON UPDATE
CURRENT_TIMESTAMP in the column definition
A time. Format: hh:mm:ss. The supported range is from '-838:59:59' to
TIME(fsp)
'838:59:59'
A year in four-digit format. Values allowed in four-digit format: 1901
YEAR to 2155, and 0000.
MySQL 8.0 does not support year in two-digit format.

MySQL Commands:

1- DDL -Data Definition Language:


Command Description
CREATE  Defines the database structure schema
 Creates a new table, a view of a table, or
other object in database
ALTER Modifies an existing database object, such as a table.
DROP Deletes an entire table, a view of a table or other object in the
database (removes tables and databases).
2- DML -Data Manipulation Language:
Command Description
INSERT Inserts data into the row of a table (Creates a record)
UPDATE Updates data in a database (Modifies records)
DELETE Removes one or more rows from a table (Deletes
records ).

3- DCL -Data Control Language:


Command Description
GRANT Gives a privilege to user
REVOKE Takes back privileges granted from user

4- DQL -Data Query Language:


Command Description
SELECT selects the attribute based on the condition
described by the WHERE clause (Retrieves
certain records from one
or more tables )
Creating, Table and Database in
MySQL
Note: Database names are case sensitive

Info about databases and tables


1- Listing the databases on the MySQL server host

mysql> show databases;

2- Access/change database (to select a database)


mysql> Use [database_name]

3- Showing the current selected database


mysql> select database()

4- Showing tables in the current database

mysql> show tables;

5- Showing the structure of a table


mysql> describe [table_name];

Creating Database in MySQL

Create Database in two ways

1) By executing a simple SQL query


2) By using forward engineering in MySQL Workbench

How to Create Database


Syntax:
CREATE DATABASE DatabaseName;

For Example:
CREATE DATABASE movies;
IF NOT EXISTS

A single MySQL server could have multiple databases. If you are not the only one accessing
the same MySQL server or if you have to deal with multiple databases there is a probability of
attempting to create a new database with name of an existing database . IF NOT EXISTS let
you to instruct MySQL server to check the existence of a database with a similar name prior to
creating database.

When IF NOT EXISTS is used database is created only if given name does not conflict with an
existing database's name. Without the use of IF NOT EXISTS MySQL throws an error.

CREATE DATABASE IF NOT EXISTS movies;

Creating a table in MySQL

CREATE TABLE command is used to create tables in a database.

Syntax:
CREATE TABLE [IF NOT EXISTS]`TableName` (`fieldname`
dataType [optio nal parameters]) ENGINE = storage Engine;

HERE

 "CREATE TABLE" is the one responsible for the creation of the table in the
database.
 "[IF NOT EXISTS]" is optional and only create the table if no matching
table name is found.
 "`fieldName`" is the name of the field and "data Type" defines the nature of the
data to be stored in the field.
 "[optional parameters]" additional information about a field
such as
" AUTO_INCREMENT" , NOT NULL etc.
For Example:
CREATE TABLE IF NOT EXISTS `MyFlixDB`.`Members` (
`membership_number` INT AUTOINCREMENT ,
`full_names` VARCHAR(150) NOT NULL ,
`gender` VARCHAR(6) ,
`date_of_birth` DATE ,
`physical_address` VARCHAR(255) ,
`postal_address` VARCHAR(255) ,
`contact_number` VARCHAR(75) ,
`email` VARCHAR(255) ,
PRIMARY KEY (`membership_number`) );

You might also like