Unit I: Introduction To Mysql
Unit I: Introduction To Mysql
Introduction to MySQL
Syllabus
• MySQL uses all the standard ANSI SQL numeric data types
• The following list shows the common numeric data types
and their descriptions −
• INT − A normal-sized integer that can be signed or
unsigned. If signed, the allowable range is from -
2147483648 to 2147483647. If unsigned, the allowable
range is from 0 to 4294967295.
• TINYINT − A very small integer that can be signed or
unsigned. If signed, the allowable range is from -128 to 127.
If unsigned, the allowable range is from 0 to 255
• SMALLINT − A small integer that can be signed or unsigned.
If signed, the allowable range is from -32768 to 32767. If
unsigned, the allowable range is from 0 to 65535.
• MEDIUMINT − A medium-sized integer that can be signed or
unsigned. If signed, the allowable range is from -8388608 to
8388607. If unsigned, the allowable range is from 0 to 16777215.
• Example
• Phone INT Not Null;
UNIQUE
• The UNIQUE constraint in MySQL does not
allow to insert a duplicate value in a column.
• Example
PRIMARY KEY
• A PRIMARY KEY constraint for a table enforces
the table to accept unique data for a specific
column and this constraint creates a unique
index for accessing the table faster.
DEFAULT
• In a MySQL table, each column must contain a
value ( including a NULL). While inserting data
into a table, if no value is supplied to a
column, then the column gets the value set as
DEFAULT.
Working with Databases and Tables
• Viewing Databases
• To list all databases on a MySQL server host,
you use the SHOW DATABASES command as
follows:
• Example
SHOW DATABASES;
Creating Databases
• Syntax
• CREATE DATABASE databasename;
• Example
• CREATE DATABASE testDB;
Using a Database
• You can use the SQL command use to select a
database.
• USE testDB;
Deleting Database
• Drop Databasename;
• Drop testDB;
• Example
Syntax
ALTER TABLE table_name ADD new_column_name
column_definition
[ FIRST | AFTER column_name ];
Example
ALTER TABLE contacts
ADD last_name varchar(40) NOT NULL
AFTER contact_id;
Modify column in table
• Syntax
• ALTER TABLE table_name MODIFY
column_name column_definition [ FIRST |
AFTER column_name ];
• Example
• ALTER TABLE contacts MODIFY last_name
varchar(50) NULL;
Drop column in table
• The syntax to drop a column in a table in
MySQL (using the ALTER TABLE statement) is:
• Syntax
• ALTER TABLE table_name DROP COLUMN
column_name;
• Example
• ALTER TABLE contacts DROP COLUMN
contact_type;