Basic Steps in MySQL
Basic Steps in MySQL
MySQL
1. Creating databases:
◦ CREATE DATABASE databasename;
2. Selecting databases:
◦ USE databasename;
3. Showing databases:
◦ SHOW DATABASES;
4. Create table:
◦ CREATE TABLE table_name(column1 datatype,
column2 datatype, column3 datatype, ..... columnN
datatype, PRIMARY KEY( one or more columns ));
5. To display fields in tablename:
◦ DESCRIBE tablename;
6. Insert on all columns
◦ INSERT INTO tablename VALUES (list of values);
7. To display all values inside the table:
BASIC QUERY IN ◦ SELECT * FROM tablename;
MYSQL
Column or Field or Attribute
◦ For example, DECIMAL(19,9) has 9 digits for the fractional part and 19-9 = 10 digits for integer part.
• DECIMAL(6,2) has 6 digits for the fractional part and 6-2 = 4 digits for integer part.
String data types
• CHAR, VARCHAR, BINARY, VARBINARY, TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB,
TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT, ENUM, SET
varchar string To store letters, numbers, and special The length can be any
characters value from 0 to 65,535
char string To store letters, numbers, and special The length can be any value from 0
characters to 255.
enum string ENUM, you are creating a list of items maximum of 65,535 distinct
from which the value must be selected (or elements
it can be NULL).
Date and Time data types
• DATE, TIME, DATETIME, TIMESTAMP, YEAR
datetime Date and time Contain both date and time. Display format - 'YYYY-MM- range is '1000-01-01 00:00:00'
DD hh:mm:ss' to '9999-12-31 23:59:59'.
Timestamp
◦ The MySQL TIMESTAMP is a temporal data type that holds the combination of date and time. The format of a TIMESTAMP
is YYYY-MM-DD HH:MM:SS which is fixed at 19 characters.
◦ The TIMESTAMP value has a range from '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
◦ When you insert a TIMESTAMP value into a table, MySQL converts it from your connection’s time zone to UTC for storing.
◦ When you query a TIMESTAMP value, MySQL converts the UTC value back to your connection’s time zone. Note that this
conversion does not take place for other temporal data types such as DATETIME.
◦ Note: Coordinated Universal Time or Universal Time Coordinated (UTC).
Datetime vs Timestamp
◦ The major difference between DATETIME and TIMESTAMP is that TIMESTAMP values are converted from the current time
zone to UTC while storing, and converted back from UTC to the current time zone when retrieved.
◦ The datetime data type value is unchanged.
The following command displays current time zone information :
MySQL> create table tempdate (udatetime datetime, utimestamp timestamp);
Query OK, 0 rows affected (0.32 sec)
MySQL> insert into tempdate values ((now()), (now()));
Query OK, 1 row affected (0.05 sec)
In this format:
•YYYY represents a four-digit year e.g., 2018.
•MM represents a two-digit month e.g., 01, 02, and 12.
•DD represents a two-digit day e.g., 01, 02, 30.
Date
Time
MySQL recognizes various time formats besides the 'HH:MM:SS' format
that we mentioned earlier.
MySQL allows you to use the 'HHMMSS' format without delimiter ( : ) to
represent time value. For example, '08:30:00' can be rewritten as '083000‘.
CREATE TABLE
Syntax:
CREATE TABLE table_name(column1 datatype, column2 datatype, column3
datatype, ..... columnN datatype, PRIMARY KEY( one or more columns ));
Simple table creation:
◦ According to the official website information: starting from version
8.0.17,TINYINT, SMALLINT, MEDIUMINT, INT, and The display width of BIGINT
type will be invalid.
◦ MySQL Server 8.0.17 deprecated the display width for
the TINYINT, SMALLINT, MEDIUMINT, INT, and BIGINT data
types (except for the display width for signed TINYINT(1)).
Describe table:
Primary key
◦ Primary key is column or set of columns that is used for
identifying a particular row. Every table must have a
primary key and it should be unique.
Properties:
◦ Null(empty) values and repeated(duplicate) values are not
allowed in the primary key column
◦ Even though more than one column can be used as primary
key is called composite primary key.
Table Creation with Primary key (Single
field):
◦ Primary key defining in last:
◦ Primary key defining near to column:
Table Creation with Multiple Primary key:
◦ Composite primary key
Unique key:
◦ Unique key is used to prevent duplication of data in a column
or group of columns in a table.
◦ If unique key is assigned to more than one column, then it is
called composite unique key.
◦ It is also like primary key. But it allow null values in the key
column.
Unique key (one field)