0% found this document useful (0 votes)
23 views

Basic Steps in MySQL

Uploaded by

SM
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Basic Steps in MySQL

Uploaded by

SM
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 62

Basic steps in

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

Register_No Roll_no Student_name Address


2564101 1001 Abinaya Erode
Row or Record
2564102 1002 Meena Madurai
or Tuple
2564103 1003 Sasi Erode
Create database
1. Creating databases:
◦ CREATE DATABASE databasename;
2. Selecting databases:
◦ USE databasename;
3. Showing databases:
◦ SHOW DATABASES;
The basic syntax of the
CREATE TABLE
statement
CREATE TABLE table_name(column1 datatype,
column2 datatype, column3 datatype, ..... columnN
datatype, PRIMARY KEY( one or more columns ));
CREATE TABLE
Syntax:
CREATE TABLE table_name(column1 datatype, column2 datatype, column3
datatype, ..... columnN datatype, PRIMARY KEY( one or more columns ));

Register_No Roll_no Student_name Address


2564101 1001 Abinaya Erode
2564102 1002 Meena Madurai
2564103 1003 Sasi Erode
Simple table creation:

Register_No Roll_no Student_name Address


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;
Describe Table:
6. Insert on all columns
◦ INSERT INTO tablename VALUES (list of values);
7. To display all values inside the table:
◦ SELECT * FROM tablename;
The basic syntax of
the INSERT TABLE
statement
1. Insert on all columns
◦ INSERT INTO tablename VALUES (list of values);
2. Insert multiple rows
◦ INSERT INTO tablename VALUES (v11,v12,...),
(v21,v22,...), ... (vnn,vn2,...);
3. Insert on selected columns
◦ INSERT INTO tablename(columnname,.....
columnname) VALUES (column1value, .....,
columnNvalue);
1. Insert on all columns:
◦ INSERT INTO tablename VALUES (list of values);
2. Insert multiple rows
◦ INSERT INTO tablename VALUES (v11,v12,...), (v21,v22,...), ... (vnn,vn2,...);
3. Insert on selected columns
◦ INSERT INTO tablename(columnname,..... columnname) VALUES
(column1value, ....., columnNvalue);
Mysql datatype
◦ Numeric data types -
TINYINT,SMALLINT,MEDIUMINT,INT,BIGINT,DECIM
AL, FLOAT,DOUBLE,BIT
◦ Date and Time data types –
DATE,TIME,DATETIME,TIMESTAMP,YEAR
◦ String data types –
CHAR,VARCHAR,BINARY,VARBINARY,TINYBLOB,
BLOB, MEDIUMBLOB,
LONGBLOB,TINYTEXT,TEXT,MEDIUMTEXT,
LONGTEXT, ENUM,SET
◦ Spatial data types –
GEOMETRY,POINT,LINESTRING,POLYGON,
GEOMETRYCOLLECTION,MULTILINESTRING,
MULTIPOINT,MULTIPOLYGON
Numeric datatype:
• TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT, DECIMAL, FLOAT, DOUBLE, BIT

Datatype Type Description Range


int numeric To store data only in numeric format If signed ( -2147483648 to 2147483647)
If unsigned (0 to 255).
tinyint numeric To store data only in numeric format. MySQL If signed (-128 to 127)
does not have the built-in BOOLEAN or If unsigned (0 to 255)
BOOL data type. To represent boolean values,
MySQL uses the smallest integer type which
is TINYINT(1)

bigint numeric To store data only in numeric format If signed (-9223372036854775808 to


9223372036854775807)
If unsigned (0 to
18446744073709551615).
Signed vs Unsigned
◦ Unsigned can store only positive numbers
◦ signed can store negative number
◦ UNSIGNED ranges from 0 to n, while SIGNED ranges from about -n/2 to n/2.
◦ In this case, you have an AUTO_INCREMENT ID column, so you would not have
negatives.
◦ Thus, use UNSIGNED. If you do not use UNSIGNED for the AUTO_INCREMENT
column, your maximum possible value will be half as high (and the negative half of
the value range would go unused).
◦ The “unsigned” in MySQL is a data type. Whenever we write an unsigned to any
column that means you cannot insert negative numbers.
◦ Suppose, for a very large number you can use unsigned type.
◦ The maximum range with unsigned int is 4294967295.
◦ Note: If you insert negative value you will get a MySQL error.
TABLE OF
THE RANGES
OF VALUES
EACH
INTEGER
TYPE CAN
STORE:
Int (signed)
Int (unsigned)
Tinyint (signed)
Tinyint (unsigned)
Decimal
◦ column_name decimal(P, D)
◦ In the syntax above:
◦ P is the precision that represents the number of significant digits. The range of P is 1 to 65.
◦ D is the scale that that represents the number of digits after the decimal point. The range of D is 0 and 30. MySQL requires
that D is less than or equal to (<=) P.
◦ The default value of P is 10 in this case.
Example
◦ amount decimal(6,2)
◦ In this example, the amount column can store 6 digits with 2 decimal places; therefore, the range of the amount column is
from 9999.99 to -9999.99

◦ 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

Datatype Type Description Range

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

Datatype Type Description Range


timestamp Date and time values that contain both date and time. MySQL converts range of '1970-01-01 00:00:01'
TIMESTAMP values from the current time zone to UTC UTC to '2038-01-19 03:14:07'
for storage, and back from UTC to the current time zone UTC.
for retrieval. (This does not occur for other types such as
DATETIME). Display format - 'YYYY-MM-DD
hh:mm:ss'

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)

Now see the records;


At this point, the datetime and timestamp data types have same values.
Let change the timezone and see the result.
MySQL> SET TIME_ZONE ='Europe/Paris';
Query OK, 0 rows affected (0.00 sec)

Now execute the following command :


Sample Output:
MySQL> select * from tempdate;
+---------------------+---------------------+
| udatetime | utimestamp |
+---------------------+---------------------+
| 2013-06-29 16:55:18 | 2013-06-29 13:25:18 |
+---------------------+---------------------+
Date
'YYYY-MM-DD'

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)

1. Unique key is used to prevent duplication of data in a column or group of


columns in a table.
Unique key (multiple field)
◦ 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.
Table Creation with different datatypes:
Auto_increment
◦ Auto_increment allows a unique number to be generated automatically when a new
record is inserted into a table.
◦ Often this is the primary key field that we would like to be created automatically
every time a new record is inserted.
Rules for using Auto_increment attributes:
◦ Each table has only one Auto_Increment column whose datatype is typically the
integer.
◦ The Auto_increment column must be either Primary_key or Unique.
◦ The auto_increment must have a NOT NULL constraint, when you set the
auto_increment attribute to a column,MySQL automatically adds the NOT NULL
constraint to the column implicitly.
Altering Sequence:
◦ Syntax : Alter table tablename auto_increment=start_value;
• Null(empty) values and repeated(duplicate) values are not allowed in the primary key
column
Primary key Properties:
Table creation with different datatypes:
Not Null
By default tables can have null values.

But Not Null constraint is used to make the


users to enter values to the fields compulsory.

If a value is not entered, MySQL will not


validate the record.

You might also like