We will show you step by step process to create a new table in MySQL
We will show you step by step process to create a new table in MySQL
A database is a collection of tables that store data together. You can use databases to organize and manage large amounts of data. Databases are used to store information such as customer records, inventory lists, employee details, and more.
If you need to add a new table to an existing database, you must first create a new table. This tutorial shows how to do so using the command line interface (CLI) of MySQL.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
CREATE TABLE persons (
person_id int,
last_name varchar(255),
first_name varchar(255),
address varchar(255),
city varchar(255)
);
The person_id column is of type int and will hold an integer.
The last_name
, first_name
, address
, and city
columns are of type varchar and will hold characters, and the maximum length for these fields is 255 characters.
The empty "Persons" table will now look like this:
person_id | last_name | first_name | address | city |
---|
You can insert data into a table by executing SQL commands. These commands are called statements. A statement consists of one or more clauses separated by semicolons. Each clause has its own name. The following is an example of inserting data into a table named “persons”:
insert into persons values(12, "Doe", "John", "Time Square", "New York");
The table will appear as follows after inserting the record
person_id | last_name | first_name | address | city |
---|---|---|---|---|
12 | Doe | John | Time Square | New York |
If you want to delete a row from a table, then use the DELETE command. This command deletes rows based on the WHERE clause. The syntax of the DELETE command is as follows:
DELETE FROM table_name
For example, to remove the first row from the persons table, we would execute the following command:
DELETE * FROM persons where person_id=12;
You can update a row in a table using UPDATE. The syntax of the UPDATE command is as follows:
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
update persons
set lastname = 'Walker'
where person_id = 12;
The last name column is updated post the change.
person_id | last_name | first_name | address | city |
---|---|---|---|---|
12 | Walker | John | Time Square | New York |