100% found this document useful (1 vote)
29 views39 pages

Unit I: Introduction To Mysql

The document provides an introduction to MySQL by discussing data types, attributes, working with databases and tables, and altering table structure. It defines what a database and data are, describes various MySQL data types including numeric, date/time, and string types. It also discusses attributes such as AUTO_INCREMENT, NOT NULL, UNIQUE, and DEFAULT. The document demonstrates how to work with databases and tables, including viewing, creating, deleting, and using databases as well as creating, viewing, copying, deleting and altering tables.

Uploaded by

Prathyusha Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
29 views39 pages

Unit I: Introduction To Mysql

The document provides an introduction to MySQL by discussing data types, attributes, working with databases and tables, and altering table structure. It defines what a database and data are, describes various MySQL data types including numeric, date/time, and string types. It also discusses attributes such as AUTO_INCREMENT, NOT NULL, UNIQUE, and DEFAULT. The document demonstrates how to work with databases and tables, including viewing, creating, deleting, and using databases as well as creating, viewing, copying, deleting and altering tables.

Uploaded by

Prathyusha Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

UNIT I

Introduction to MySQL
Syllabus

 Data types and attributes


 Working with databases
 Working with tables
 Altering table structure.
• What is Data?
• In simple words data can be facts related to
any object in consideration.
• For example your name, age, height, weight,
etc are some data related to you.
• What is a Database?
• Database is a systematic collection of data.
Databases support storage and manipulation
of data. Databases make data management
easy.
DATA TYPE
• A database table contains multiple columns
with specific data types such as numeric or
string.
• Each data type in MySQL can be determined
by the following characteristics:
• The kind of values it represents.
• The space that takes up and whether the
values is a fixed-length or variable length.
• MySQL uses many different data types broken
into three categories −
• Numeric
• Date and Time
• String Types.
Numeric Data Types

• 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.

• BIGINT − A large integer that can be signed or unsigned. If signed,


the allowable range is from -9223372036854775808 to
9223372036854775807. If unsigned, the allowable range is from 0
to 18446744073709551615. You can specify a width of up to 20
digits.
• FLOAT(M,D) − A floating-point number that cannot be unsigned.
You can define the display length (M) and the number of decimals
(D). This is not required and will default to 10,2, where 2 is the
number of decimals and 10 is the total number of digits (including
decimals). Decimal precision can go to 24 places for a FLOAT.
• DOUBLE(M,D) − A double precision floating-point
number that cannot be unsigned. You can define the
display length (M) and the number of decimals (D).
This is not required and will default to 16,4, where 4 is
the number of decimals. Decimal precision can go to
53 places for a DOUBLE. REAL is a synonym for
DOUBLE.
• DECIMAL(M,D) − An unpacked floating-point number
that cannot be unsigned. In the unpacked decimals,
each decimal corresponds to one byte. Defining the
display length (M) and the number of decimals (D) is
required. NUMERIC is a synonym for DECIMAL.
Date and Time Types

• The MySQL date and time datatypes are as follows −

• DATE − A date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-31. For


example, December 30th, 1973 would be stored as 1973-12-30.
• DATETIME − A date and time combination in YYYY-MM-DD HH:MM:SS format,
between 1000-01-01 00:00:00 and 9999-12-31 23:59:59. For example, 3:30 in the
afternoon on December 30th, 1973 would be stored as 1973-12-30 15:30:00.
• TIMESTAMP − A timestamp between midnight, January 1st, 1970 and sometime in
2037. This looks like the previous DATETIME format, only without the hyphens
between numbers; 3:30 in the afternoon on December 30th, 1973 would be stored
as 19731230153000 ( YYYYMMDDHHMMSS ).
• TIME − Stores the time in a HH:MM:SS format.
• YEAR(M) − Stores a year in a 2-digit or a 4-digit format. If the length is specified as
2 (for example YEAR(2)), YEAR can be between 1970 to 2069 (70 to 69). If the
length is specified as 4, then YEAR can be 1901 to 2155. The default length is 4.
String Types
• CHAR(M) − A fixed-length string between 1 and 255
characters in length (for example CHAR(5)), right-padded
with spaces to the specified length when stored. Defining a
length is not required, but the default is 1.
• VARCHAR(M) − A variable-length string between 1 and 255
characters in length. For example, VARCHAR(25). You must
define a length when creating a VARCHAR field.
• BLOB or TEXT − A field with a maximum length of 65535
characters. BLOBs are "Binary Large Objects" and are used
to store large amounts of binary data, such as images or
other types of files. Fields defined as TEXT also hold large
amounts of data. The difference between the two is that
the sorts and comparisons on the stored data are case
sensitive on BLOBs and are not case sensitive in TEXT
fields. You do not specify a length with BLOB or TEXT.
• TINYBLOB or TINYTEXT − A BLOB or TEXT column with a
maximum length of 255 characters. You do not specify a
length with TINYBLOB or TINYTEXT.
• MEDIUMBLOB or MEDIUMTEXT − A BLOB or TEXT column
with a maximum length of 16777215 characters. You do not
specify a length with MEDIUMBLOB or MEDIUMTEXT.
• LONGBLOB or LONGTEXT − A BLOB or TEXT column with a
maximum length of 4294967295 characters. You do not
specify a length with LONGBLOB or LONGTEXT.
• ENUM − An enumeration, which is a fancy term for list.
When defining an ENUM, you are creating a list of items
from which the value must be selected (or it can be NULL).
For example, if you wanted your field to contain "A" or "B"
or "C", you would define your ENUM as ENUM ('A', 'B', 'C')
and only those values (or NULL) could ever populate that
field.
Attributes
• A database consists of tables. Each table has
columns and rows. Each row (called a tuple) is
a data set that applies to a single item. Each
column (attribute) contains describing
characteristics of the rows.
Data Type Attributes
• Following are some most common MySQL
Data type constraints:
• AUTO_INCREMENT
• NOT NULL
• NULL
• UNIQUE
• PRIMARY KEY
• DEFAULT
AUTO_INCREMENT
• Auto increment attribute when specified on
a column with a numeric data types,
generates numbers sequentially whenever a
new row is added into the database.
• Example:
NOT NULL

• In MySQL NOT NULL constraint allows to


specify that a column can not contain any
NULL value.

• 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;

• It drops all tables in the database and deletes


the database.
Working with Tables
• Create a table
• Delete a table
• Alter a table
• View a table
Table Creation
• The table creation command requires the
following details −
• Name of the table
• Name of the fields
• Definitions for each field
Syntax
• CREATE TABLE table_name (column_name
column_type);
• Example
CREATE TABLE customer(
cus_id INT NOT NULL AUTO_INCREMENT,
cus_firstname VARCHAR(100) NOT NULL,
cus_surname VARCHAR(100) NOT NULL,
PRIMARY KEY ( cus_id )
);
Copying a table
• Create Table customer2 select * from
customer;
Viewing all tables available in the
database
• SHOW TABLES;
Viewing a Table Structure
• Describe customer;
Deleting a table
• Syntax
• DROP TABLE table_name;

• Example

• DROP TABLE customers;


Altering a Table
• The MySQL ALTER TABLE statement is used to
add a column
• Modify a column
• drop a column
• rename a column
• rename a table
Add column in table

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;

You might also like