cmsc127 3 Mysql PDF

You might also like

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

Topics

Data Types in SQL 1 Why Use MySQL?

2 Database Commands
CMSC127

3 Column and Data Types

(CMSC127) Data Types in SQL 1 / 24 (CMSC127) Data Types in SQL 2 / 24

Why use MySQL Case Sensitivity

Cost SQL keywords are not case sensitive.


MySQL is open-source, and is usually free to use (and even modify)
This is standard across database systems.
the software without paying for it.
In MySQL, whether database and table names are case sensitive
Performance
depends on your operating system.
MySQL is fast (make that very fast).
The reason for this is that generally each database will have an
Trusted
underlying directory in your operating system, and each table will
MySQL is used by some of the most important and prestigious
have an underlying file.
organizations and sites, all of whom entrust it with their critical data.
These directory names and filenames follow different rules depending
Simplicity
on the operating system.
MySQL is easy to install and get up and running.

(CMSC127) Data Types in SQL 3 / 24 (CMSC127) Data Types in SQL 4 / 24


Creating a Database Creating a Database

You can check to see that this statement worked by executing the
After design, the first step in creating a database is, logically enough, command
to tell MySQL that we want to create a new database show databases;
We do this with the CREATE DATABASE SQL statement, as follows: You should now see the employee database listed among the
create database employee; databases in the system.
We now have an empty database, waiting for some tables to be
created.

(CMSC127) Data Types in SQL 5 / 24 (CMSC127) Data Types in SQL 6 / 24

Selecting a Database Creating Tables

To create the tables in the employee database, we use the CREATE


Before we can create any tables or do anything else with the employee TABLE SQL statement. The usual form of this statement is
database, we need to tell MySQL that we want to work with our new create table tablename ( table definition ) [type=table_type];
database.
example:
We do this with the use statement, as follows:
create table department
use employee; (
The employee database is now selected, and all actions we take from departmentID int not null auto_increment primary key,
now on will be applied to this database by default. name varchar(20)
) type=InnoDB;

(CMSC127) Data Types in SQL 7 / 24 (CMSC127) Data Types in SQL 8 / 24


Showing Tables Describing Tables

You can check whether the tables in your database have been set up You can get more information about the structure of each table by
correctly using the command using the describe command, for example,
show tables; describe department;

(CMSC127) Data Types in SQL 9 / 24 (CMSC127) Data Types in SQL 10 / 24

Column and Data Types Numerical Types

Numerical types are used for storing numbers.


There are three basic column types in MySQL: In our example, we used the types int (integer) and float
I numerical types (floating-point number).
I string or text types
I date and time types.
These represent the two subtypes of numerical types:
I the exact numerical types
I the approximate numerical types

(CMSC127) Data Types in SQL 11 / 24 (CMSC127) Data Types in SQL 12 / 24


Numerical Types Integers

This type can be abbreviated as INT.


This is a standard integer, stored in 4 bytes, giving a range of 232
Numerical types may be constrained by a display width M and, for possible values.
floating-point types, a number of decimal places, D.
There are also several variations on INT:
These numbers go after the declaration; for example: A TINYINT is 1 byte (28 possible values).
salary decimal(10, 2) The keywords BIT and BOOL are synonyms for TINYINT.
A SMALLINT is 2 bytes (216 possible values).
A MEDIUMINT is 3 bytes (224 possible values).
A BIGINT is 8 bytes (264 possible values).

(CMSC127) Data Types in SQL 13 / 24 (CMSC127) Data Types in SQL 14 / 24

Float Double

This is a double-precision floating-point number.


This is a single-precision floating-point number.
Synonyms for DOUBLE are REAL and DOUBLE PRECISION.
It can represent a positive number between 1.18x 10−38 to 3.40x 1038
and a similar range of negative numbers. They can represent a positive number between 2.23x 10−308 to
1.80x 10308 and a similar range of negative numbers.

(CMSC127) Data Types in SQL 15 / 24 (CMSC127) Data Types in SQL 16 / 24


String and Text Types Char

CHAR is used to store fixed-length strings.


As in the employee database, CHAR is usually followed by a string
MySQL supports various string and text types.
length, for example CHAR(20).
The basic types are CHAR, VARCHAR, TEXT, BLOB, ENUM, and
If you do not specify a length, you will get a CHAR(1).
SET. We will discuss each of these in turn.
The maximum length of a CHAR is 255 characters.

(CMSC127) Data Types in SQL 17 / 24 (CMSC127) Data Types in SQL 18 / 24

Char Char

When CHARs are stored, they will always be the exact length you
Obviously, storing a CHAR takes up more space on disk than storing
specify.
an equivalent variable-length string.
This is achieved by padding the contents of the column with spaces.
The trade-off is that it is faster to retrieve rows from a table in which
These spaces are automatically stripped when the contents of a all the columns are of fixed widths (that is, CHAR, numeric, or date).
CHAR column are retrieved.

(CMSC127) Data Types in SQL 19 / 24 (CMSC127) Data Types in SQL 20 / 24


Varchar Text, Blob

The TEXT types are used for storing longer pieces of text than you
VARCHAR stores variable-length strings. can fit in a CHAR or VARCHAR.
You specify the width in parentheses after the type, for example, BLOB stands for Binary Large OBject.
VARCHAR(10). These types are the same except that BLOBs are intended to store
The range is 0 to 255. binary data rather than text.
Comparisons on BLOBs are case sensitive, and on TEXTs, they are
not.

(CMSC127) Data Types in SQL 21 / 24 (CMSC127) Data Types in SQL 22 / 24

Enum Date and Time

The date type stores a date.


This type allows you to list a set of possible values.
MySQL expects the date in ISO year-month-day order, avoiding
Each row can contain one value from the enumerated set.
trans-Atlantic arguments.
You declare an ENUM as follows:
Date is displayed as YYYY-MM-DD.
gender enum(’m’, ’f’)
Time is displayed as HH:MM:SS.
Enumerated types can also be NULL, so the possible values of gender
are m, f, NULL, or error. DateTime is a combination of the previous types. The format is
YYYY-MM-DD HH:MM:SS.

(CMSC127) Data Types in SQL 23 / 24 (CMSC127) Data Types in SQL 24 / 24

You might also like