Introduction To Mysql Server: Mysql Cluster - Provides Fault Tolerance

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Introduction to MySQL Server

MySQL is the most popular open source (i.e. it is free) relational database
management system that is designed to provide an efficient data management.
MySQL is a reliable, efficient and easy to use. MySQL doesn‟t have a built in
„window‟ like interface unless you download and install it separately.

MySQL is very suitable for web-based applications and it runs in many of the
world‟s demanding websites like yahoo, Google, Youtube, etc.

MySQL runs in almost all possible Operating Systems.

MySQL Products

 MySQL community server - free


 MySQL Enterprise - Commercial and It contains enterprise features.
 MySQL Cluster - Provides fault tolerance.
 MySQL embedded database - Embedded in small devices.
 MySQL Workbench - free and it provides a GUI for various database
operations, such as database design and modelling and database
administration.

In this our lab practices we are going to use MySQL community server and
MySQL Workbench
Overview of SQL

The name SQL stands for "Structured Query Language". SQL is a user-friendly
commercial query language. A query language is a high - level Data
Manipulation Language (DML) which is used in stand-alone interactive manner.
SQL is a standard interactive and programming language for querying and
modifying data and managing databases. The core of SQL is formed by a
command language that allows the retrieval (selection), insertion, updating, and
deletion of data, and performing management and administrative functions.

SQL Terminology
 Table - A set of rows. It is analogous to a “file”.
 Row - Analogous to a record of a “file”.
 Column - A column is analogous to a field of a record. Each column in a
given row has a single value.
 Primary Key – One of more columns whose contents are unique
Within a table and thus can be used to identify a row of that table.

Types of SQL Statements

 DDL (Data Definition Language)

Used to build and modify the structure of your


tables and other objects in the database
Examples: CREATE TABLE, ALTER TABLE, DROP
TABLE, CREATE VIEW, etc.

 DML(Data Manipulation Language)


Used to work with the data in tables
Example s: INSERT INTO, UPDATE, DELETE

 DCL (Data Control Language)


Used to control access rights
GRANT, REVOKE
MySQL Data types

MySQL is capable of supporting many data types. Some of the more commonly
used include:

CHAR (n)
CHAR's are used to represent fixed length strings. A CHAR string can range
from 1-255 characters. E.g. StudentName CHAR (30).

VARCHAR (n)
VARCHAR is a more flexible form of the CHAR data type. It also represents
data of type String, yet stores this data in variable length format. Again,
VARCHAR can hold 1-255 characters. E.g. StudentAddress VARCHAR (30).

INT (n) [Unsigned]


The INT data type stores integers ranging from -2,147,483,648 to 2,147,483,647.
An optional "unsigned" can be denoted with the declaration, modifying the
range to be 0 to 4,294,967,295.

E.g. 1 Indian_population INT


Valid integer value: Indian_population = „1,200,000,000‟;

Invalid integer value: Indian_population = „3,000,000,000‟;

E.g. 2 Indian_population INT unsigned;


Valid integer value: Indian_population = „3,000,000,000‟;

Invalid integer value: '-24567';

FLOAT [(n,d)]
A FLOAT represents small decimal numbers, used when a somewhat more
precise representation of a number is required.

E.g. Rainfall FLOAT (4, 2);


This could be used to represent rainfall average in centimetres per year, which
could be a decimal value. More specifically, FLOAT (4, 2) states the fact that
rainfall can hold up to four characters and two decimal places. Thus,

42.35 is valid, accurately represented.


324.45 is invalid, rounded to 324.5.
2.2 is valid, accurately represented.
34.542 is invalid, rounded to 34.54.
Note: Due to the fact that FLOAT is rounded, those wishing to represent money
values would find it wise to use DECIMAL, a data type found within MySQL
that does not round values.

DATE
Stores date related information. The default format is 'YYYY-MM-DD', and
ranges from '0000-00-00' to '9999-12-31'. MySQL provides a powerful set of
date formatting and manipulation commands, too numerous to be covered
within this article. However, one can find these functions covered in detail
within the MySQL documentation.

TEXT / BLOB
The TEXT and BLOB data types are used when a string of 255 - 65535
characters is required to be stored. This is useful when one would need to store
an article such as the one you are reading. However, there is no end space
truncation as with VARCHAR AND CHAR. The only difference between
BLOB and TEXT is that TEXT is compared case insensitively, while BLOB is
compared case sensitively.

SET
A data type of type string that allows one to choose from a designated set of
values, be it one value or several values. One can list up to 64 values in a set.

E.g. transport SET ("Bus",”plane”,”train”) NOT NULL;

ENUM
Is a short for ENUMERATION. It is a data type of type string that has the same
characteristics as that of SET data type, but only one set of allowed values may
be chosen. Usually only takes up one byte of space, thus saving time and space
within a table.

E.g. transport ENUM ("Bus", "plane”,”train") NOT NULL;

You can list up to 65535 values in an ENUM list. If a value is inserted that is
not in the list, a blank value will be inserted.

Records

Together, a group of declared data types form what is known as a record. A


record can be as small as one data variable or as many as deemed needed. One
or more records form the structure of a table.

Summary of MySQL DatatypesNote: The square brackets [] indicate an


optional parameter to be put in parentheses, while parentheses () indicate
required arguments

Type Size Description


CHAR[Length] Length bytes A fixed-length field from 0 to 255
characters long.
VARCHAR(Length) String length + 1 bytes A fixed-length field from 0 to 255
characters long.
TINYTEXT String length + 1 bytes A string with a maximum length of
255 characters
TEXT String length + 2 bytes A string with a maximum length of
65,535 characters.
MEDIUMTEXT String length + 3 bytes A string with a maximum length of
16,777,215 characters.
LONGTEXT String length + 4 bytes A string with a maximum length of
4,294,967,295 characters.
TINYINT[Length] 1 byte Range of -128 to 127 or 0 to 255
unsigned.
SMALLINT[Length] 2 bytes Range of -32,768 to 32,767 or 0 to
65,535 unsigned.
MEDIUMINT[Length] 3 bytes Range of -8,388,608 to 8,388,607 or 0
to 16,777,215 unsigned
INT[Length] 4 bytes Range of -2,147,483,648 to
2,147,483,647 or 0 to 4,294,967,295
unsigned
Type Size Description
BIGINT[Length] 8 bytes Range of -9,223,372,036,854,775,808
to 9,223,372,036,854,775,807 or 0 to
18,446,744,073,709,551,615 unsigned
FLOAT 4 bytes A small number with a floating
decimal point.
DOUBLE[Length, 8 bytes A large number with a floating
Decimals] decimal point.
DECIMAL[Length, Length +1 bytes or A DOUBLE stored as a string,
Decimals] Length + 2 bytes allowing for a fixed decimal point.
DATE 3 bytes In the format YYYY-MM-DD
DATETIME 8 bytes In the format YYYY-MM-DD
HH:MM:SS.
TIMESTAMP 4 bytes In the format
YYYYMMDDHHMMSS; acceptable
range ends in the year 2037.
TIME 3 bytes In the format of HH:MM:SS.
ENUM 1 or 2 bytes Short for enumeration, that is, each
column can have one of several
possible values.
SET 1, 2, 3, 4, or 8 bytes Like ENUM except that each column
can have more than one of several
possible values.

For this laboratory exercise we are going to use the banking application that you
have seen in laboratory exercise one. Before proceeding consider the following
questions:

1. How do we choose the names for the tables in our database?


2. How do we name the columns in a given table?
3. How do we specify the data types and lengths for each the column?
4. How do we enforce integrity constraints in the table? Can you mention
some integrity constraints that you know?
5. Is it possible to change or modify any of the defined elements of the
schema, such as table names, column names, data types and constraints?
6. Is it possible to drop any of the defined elements of the schema when they
are no needed?
Constraints: Note that we have used some constraints like primary key , not
null while creating our first table „BANK‟.A constraint is a property
assigned to a column or the set of columns in a table that prevents certain
types of inconsistent data values from being placed in the column(s).
Constraints are used to enforce the data integrity. This ensures the accuracy
and reliability of the data in the database. The following categories of the
data integrity exist:

1. Entity Integrity: ensures that there are no duplicate rows in a table.


2. Domain Integrity: enforces valid entries for a given column by
restricting the type, the format, or the range of possible values.
3. Referential integrity: ensures that rows cannot be deleted, which are
used by other records (for example, corresponding data values between
tables will be vital).
4. User-Defined Integrity: enforces some specific business rules that do
not fall into entity, domain, or referential integrity categories.

Each of these categories of the data integrity can be enforced by the appropriate
constraints. MySQL supports the following constraints:
PRIMARY KEY , UNIQUE , FOREIGN KEY , CHECK , NOT NULL,
and DEFAULT

PRIMARY KEY: constraint is a unique identifier for a row within a database


table. Every table should have a primary key constraint to uniquely identify
each row and only one primary key constraint can be created for each table. The
primary key constraints are used to enforce entity integrity. For example, A
social security number (SSN) of an employee or customer number of a given
customer might uniquely identify a specific row in the database table.
A primary key may be a single attribute(column),such as the SSN of a customer
or may be composed of a combination of attributes(columns).A primary key
that consists of a combination of attributes is known as a composite primary
key.

A UNIQUE: constraint enforces the uniqueness of the values in a set of


columns, so no duplicate values are entered. The unique key constraints are
used to enforce entity integrity as the primary key constraints.

A FOREIGN KEY: constraint prevents any actions that would destroy link
between tables with the corresponding data values. A foreign key in one table
points to a primary key in another table. Foreign keys prevent actions that
would leave rows with foreign key values when there are no primary keys with
that value. The foreign key constraints are used to enforce referential integrity.

A CHECK: constraint is used to limit the values that can be placed in a column.
The check constraints are used to enforce domain integrity.

A NOT NULL: constraint enforces that the column will not accept null values.
The not null constraints are used to enforce domain integrity, as the check
constraints.

Even though it is not must to follow the following rules it is recommended to


stick to them for good documentation purposes. Documentation is generally a
process that yields a precise definition and description of the database's
components and relationships between those components. A good
documentation facilitates system maintenance and modification.
One of the important ingredients of system documentation is the use of self-
explanatory names for system components. Now let us see some rules that we
should follow:
 Table names are singular nouns and they are capitalized. For example, a
table that contains information about customer of Lion Bank should be
named as 'CUSTOMER'. If you name this table as 'OPQWE' or simply as
'T1-to mean table1' it is not a good idea. This is because only few people
will be able to figure out what this table is likely to contain and after
some time you may forget it.
 Table column names have two parts. The first part (or the prefix)
identifies the table and the second part identifies the actual column name.
Although the names must be descriptive, they must be as short as possible.
Very long attribute names affect output spacing and are likely to be
truncated. So for the customer's first name column in your table it is
better to specify the column name as ‘CUSTOMER_FName’ rather than
‘CUSTOMER_FIRST_NAME’.The same applies for other fields in the
table too. It is easier to figure out from the column name
'CUSTOMER_FNAME' that it is the customer first name stored in to
'CUSTOMER' table.

NOTE: Each column is named for a specific attribute and must contain
contains values for the attribute it represents only. For example,
CUSTOMER_FNAME must contain only customers‟ first name values,
where as a column named CUSTOMER_PHONE must only contain
customers‟ phone number values.

Basic MySQL Commands

1. How to connect to the MySQL Server


 Start the WampServer.
 Click the MySQL Console on the WampServe. Enter password if
password is required.
2. Now you can start entering your queries
 How do you know the version number of the server and current
date?
mysql> SELECT VERSION(), CURRENT_DATE;
Press Enter.
Note: Keywords may be entered in any letter case. The
following queries are equivalent:

mysql> SELECT VERSION(), CURRENT_DATE;


mysql> select version(), current_date;
mysql> SeLeCt vErSiOn(), current_DATE;

 Here is another query. It demonstrates that you can use mysql as a


simple calculator:
mysql> SELECT SIN(PI()/4), (4+1)*5;
 It is also possible to enter multiple statements on a single line. Just
end each one with a semicolon:
mysql> SELECT VERSION(); SELECT NOW();
 Then enter the following query
mysql>select version and press enter
->, press enter
->now ();
 If you decide you do not want to execute a command that you are
in the process of entering, cancel it by typing \c:
mysql> SELECT
-> USER ()
-> \c

3. Creating your own database


 Before creating your own database enter the following commands:
mysql> SHOW DATABASES; This command shows you the
available databases in the MySQL Server.
 To create your own database use the syntax given below:
mysql> CREATE DATABASE databasename ; databasename is
the name of your database. Now execute
mysql> SHOW DATABASES; You will definitely see your
database in the list of available databases.
4. How do you select your database from the available databases and
make it your current database?
 Creating a database does not select it for use. You must do that
explicitly. To make the database created above as your current
database, use this command: mysql>USE databasename;
 At this moment your database is empty.To check this use the
following command:
mysql >SHOW TABLES;

5. Creating tables inside a selected database


 To create a table always use the CREATE TABLE tablename ;
For example , to create a table called „BANK’ you can do it like
this:
mysql> CREATE TABLE BANK(BRANCH_NAME varchar(30)
not null , BRANCH_CITY varchar(30) not null,
BRANCH_ADDRESS varchar(50) not null, primary
key(BRANCH_NAME,BRANCH_CITY));

While creating a table note the following:


1.You must use the 'Create table' Command first.
2. Specify the name of your table.
3. Specify the columns of your table with their corresponding data
types, length and/or constraints.
4. The columns of your table are separated by commas.
5. The is no comma between a column and its corresponding data
type and constraint.
6. And finally notice the opening brace, closing brace and the semi-
colon at the end of the create table command.

You might also like