CREATE statement in MySQL
Hello and
welcome everyone. In this article, we will learn about CREATE statement in
MySQL. Let’s go.
MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/
C# basic course click on: https://www.udemy.com/learn-csharp-with-sagar-jaybhay/
Web API 2- .Net Core In depth In 5 Hours
click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/
The MySQL
CREATE statement is used to CREATE DATABASE and CREATE TABLE into the database.
CREATE DATABASE
You can
use CREATE DATABASE command for creating the database in MySQL. The database
will store your data in the table format. You can create one or more databases
as well as one or more table in the database. When you create database default
schema is applied.
Following
are the syntax for create a database.
CREATE DATABASE [ IF NOT EXISTS ]
database_name;
CREATE DATABASE is a MySQL
command for creating a database.
IF NOT EXISTS – In MySQL
server having one or more database. If the database is present on MySQL server
and you throw and command create database then it will throw an error, but if
you use IF NOT EXIT clause then if the database is not present it will create
database else it will not throw an error.
database_name – database
name is the name given to the database to identify that the database on the
server.
For example:
CREATE DATABASE IF NOT EXISTS employee_DB;
NOTE: If you want to see all the database names that present in MySQL
server, then you will use the following command:
SHOW DATABASE;
It will show you created a database
as well as all database names that present in MySQL server.
CREATE TABLE
You can
use CREATE TABLE command for creating the table in MySQL server database. The
table stores the data in the columns and rows format. You can create one or
more table in the database. Mainly related information stored in row column-wise
in the database, by looking just table you easily understand the data. There is
no limit to creating a table in the database. Following three things required to
create table command:
1.
The table name
2.
Field names
3.
Each field definitions.
Following
are the syntax for create table.
CREATE TABLE [ IF NOT EXISTS ]
table_name (
column_name
datatype (datatype_size) constraint_name
);
CREATE TABLE
is a MySQL command for create table in the database.
IF NOT EXISTS – In the
database having one or more table. If the table is present on the database and
you throw and command create a table then it will throw an error, but if you
use IF NOT EXIT clause then if the table is not present it will create table
else it will not throw an error.
table_name – table name is
the name given to the table to identify that the table in the database.
column_name – it is the
name of the field.
datatype (datatype_size) –
that is the datatype and datatype size defines the nature of the data to be
stored in the field.
constraint_name - The
constraint in MySQL is used to define rules to agree or limit what data can be
stored in columns on a table.
For example:
CREATE TABLE IF NOT EXISTS employee (
e_id
int (10) PRIMARY KEY,
e_firstname
varchar (50) NOT NULL,
e_lastname
varchar (50) NOT NULL,
e_age
int (10),
e_address
varchar (250)
e_sex
char (1)
e_phonenumber
int (15) NOT NULL
)
Here is PRIMARY KEY is used
to define a column as a primary key.
The NOT NULL constraint is
being used for we do not want this field to be NULL.
NOTE: If you want to see all the tables that present in your database,
then you will use the following command:
SHOW TABLE;
It will show you all the table
names that present in your database.
NOTE: If you want to see the table structure that you are already
created, then you will use the following command:
DESCRIBE table_name;
It will show you the table
structure that you give the table name.
In this
article, we have learned to CREATE DATABASE and CREATE TABLE command with
create a statement.
MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/
C# basic course click on: https://www.udemy.com/learn-csharp-with-sagar-jaybhay/
Web API 2- .Net Core In depth In 5 Hours
click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/
Comments
Post a Comment