Creating Databases
Creating Tables
MySQL Data Types
MySQL Clauses
WHERE AND ORDER By Clause
Retrieve Data from Single Tables
MySQL implements a database as a directory that stores all files in the form of a table.
We can create a database mainly in two ways:
1.MySQL Command Line Client
2. MySQL Workbench
We can create a new database in MySQL by using the CREATE DATABASE statement with the below syntax:
CREATE DATABASE [IF NOT EXISTS] database_name
[CHARACTER SET charset_name]
[COLLATE collation_name];
Creating Databases
Database name: It is the name of a new database that should be unique in the MySQL server instance. The IF NOT EXIST clause avoids an error when we create a database that already exists.
Charset Name: It is the name of the character set to store every character in a string. MySQL database server supports many character sets. If we do not provide this in the statement, MySQL takes the default character set.
Collation Name: It is optional that compares characters in a particular character set.
Code Example:
CREATE DATABASE employee;
Show databases;
SELECT Database is used in MySQL to select a particular database to work with. This query is used when multiple databases are available with MySQL Server.
You can use SQL command USE to select a particular database.
Syntax:
USE database_name;
Code example:
USE database1;
We can drop/delete/remove a MySQL database quickly with the MySQL DROP DATABASE command.
It will delete the database along with all the tables, indexes, and constraints permanently.
Therefore, we should have to be very careful while removing the database in MySQL because we will lose all the data available in the database.
If the database is not available in the MySQL server, the DROP DATABASE statement throws an error.
We can drop an existing database in MySQL by using the DROP DATABASE statement with the below syntax:
DROP DATABASE [IF EXISTS] database_name;
In MySQL, we can also use the below syntax for deleting the database. It is because the schema is the synonym for the database, so we can use them interchangeably.
DROP SCHEMA [IF EXISTS] database_name;
Drop Databases
Database_name: It is the name of an existing database that we want to delete from the server. It should be unique in the MySQL server instance.
If Exists: It is optional. It is used to prevent from getting an error while removing a database that does not exist.
Code Example:
SHOW DATABASES;
DROP DATABASE database1;
A table is used to organize data in the form of rows and columns and used for both storing and displaying records in a structured format. It is similar to worksheets in spreadsheet applications like MS Excel.
A table creation command requires three things:
Name of the table
Names of fields
Definitions for each field
MySQL allows us to create a table into the database by using the CREATE TABLE command.
SYNTAX:
CREATE TABLE [IF NOT EXISTS] table_name(
column_definition1,
column_definition2,
........,
table_constraints
);
Code Example:
CREATE TABLE employees (
NAME VARCHAR (20),
ID VARCHAR(15),
ADDRESS VARCHAR(50)
);
SHOW TABLES;
DESCRIBE employee;
The MySQL ALTER statement is used when you want to change the name of your table or any table field. It is also used to add or delete an existing column in a table.
The ALTER statement is always used with "ADD", "DROP" and "MODIFY" commands according to the situation.
Syntax for adding single column:
ALTER TABLE table_name
ADD new_column_name column_definition
[ FIRST | AFTER column_name ];
SYNTAX:
ALTER TABLE table_name
ADD new_column_name column_definition
[ FIRST | AFTER column_name ],
ADD new_column_name column_definition
[ FIRST | AFTER column_name ], ... ;
The MODIFY command is used to change the column definition of the table.
SYNTAX:
ALTER TABLE table_name
MODIFY column_name column_definition
[ FIRST | AFTER column_name ];
SYNTAX:
ALTER TABLE table_name
DROP COLUMN column_name;
SYNTAX:
ALTER TABLE table_name
CHANGE COLUMN old_name new_name
column_definition
[ FIRST | AFTER column_name ]
SYNTAX:
ALTER TABLE table_name
RENAME TO new_table_name;
The TRUNCATE statement in MySQL removes the complete data without removing its structure. It is a part of DDL or data definition language command.
Generally, we use this command when we want to delete an entire data from a table without removing the table structure.
SYNTAX:-
TRUNCATE [TABLE] table_name;
We cannot use the WHERE clause with this command so that filtering of records is not possible.
We cannot rollback the deleted data after executing this command because the log is not maintained while performing this operation.
We cannot use the truncate statement when a table is referenced by a foreign key or participates in an indexed view.
The TRUNCATE command doesn't fire DELETE triggers associated with the table that is being truncated because it does not operate on individual rows.
DESCRIBE means to show the information in detail.
DESCRIBE command to show the structure of our table, such as column names, constraints on column names, etc. The DESC command is a short form of the DESCRIBE command. Both DESCRIBE and DESC command are equivalent and case sensitive.
Syntax
{DESCRIBE | DESC} table_name;
MYSQL uses a Drop Table statement to delete the existing table. This statement removes the complete data of a table along with the whole structure or definition permanently from the database.
Syntax:
DROP TABLE table_name;
OR,
DROP TABLE schema_name.table_name;