×

MySQL Table Query

MySQL table query

A database stored a lot of data and divided them into different relations known as tables. Each database can contain more than one table. These tables are created by using SQL commands. The MySQL database is created using the specific language (SQL language) because it can be readable by MySQL easily. After creating the table, several types of operations like insertion, updation, deletion of data, and many other MySQL functions can be performed in the table.

Let's see all these operations in detail.

Create MySQL table

CREATE statement inMySQL is used to create tables in theselected database. A table can contain several rows and columns. Each column has its own data type. These columns can be divided into several keys: primary key, foreign key, candidate key, etc. All have some specific functions in the table.

We must have to select the database before creating a table where you have to create the table.

We can use the following syntax for selecting the database:

USE databasename;

Heredatabasename is the name of the database.

After selecting the database, we can create the table.

The syntax for creating a table in MySQL is given below:

CREATE TABLE tablename (column1 datatype1, column2 datatype2, column3 datatype3, …………..);

In the above syntax, tablename is the name of the table. Column1, column2, column 3, …  are the column you want to add to the table. The datatype in the syntax is the column datatype. The entirecolumns havetheir specific data type.

For example: Suppose you have a database SCHOOL. Here you want to create a table name as STUDENT having columns like STU_NAME, STU_ID, STU_ROLL_NO, STU_ADDRESS, STU_CONTACT, and STU_AGE.All these columns contain specific data types like STU_ID contain VARCHAR, STU_ROLL_NO contain INT, etc.

We can create this table by using the below statement in MySQL:

CREATE TABLE STUDENT (STU_ID VARCHAR(32),STU_NAME VARCHAR(32),STU_ROLL_NO INT(11),STU_CITY VARCHAR(32),STU_CONTACT INT(11),STU_AGE INT(11));

The table SCHOOL has been successfully created using the above syntax.

Show MySQL table

We know that the database contains a lot of tables. These tables can be displayed using a show statement. The syntax for showing the tables:

SHOW TABLES;

For example, you have a database SCHOOL. If we want to know that how many tables are available in this database, you can execute the below command:

SHOW TABLES;

Let’s see the result in MySQL.

There is only one table available in this database, so only one table is shown in the list.

Rename MySQL table

Suppose you want to change the table's name or, by mistake, you had given the wrong name to the table or some spelling mistake.In that case, we need to change the name or rename the table. To do this, we can use the syntax as follows:

ALTER TABLE tablename RENAME TO newtablename;

Here tablename is the name of the existing table, and newtablename is the name you want to give to the table.

For example, You have a table STUDENT in the database SCHOOL. You want to change the name of the table from STUDENT to CLASS. Change the name of the table with the help of the given syntax.

ALTER TABLE STUDENT RENAME TO CLASS;

Here is the result:

Here we see that the name of the table has been changed to CLASS.

Insert MySQL table

After creating a table, we need to insert values into this table. To insert the value (data) in the table, we use the insert statement. The syntax for inserting data in the table is given below:

INSERT INTO table_name VALUES (value1, value2, value3,………..);

In the syntax, the Valuesclause indicates the data that you want to insert in the table. The value that we are inserting will be in the same format as the columns are arranged in the table. Suppose column STU_ID is present in the first column of the table, then value1 will contain the ID value only according to the datatype.

For example, We have a table CLASS and have to insert values in the table.

Command to insert value in the table:

INSERT INTO TABLE VALUES (“A1”,”RAM”, 098765,”JAIPUR”, 0543-5676, 20);

Use this command in MySQL and see the result.

The value has been inserted successfully.

Alter MySQL table

We have a database, and it contains several tables. If you want to add some extra columns in the given table or delete any table, we use the alter statement.This statement is also used to change the data type of the selected column.We use ALTER statement to add or delete columns and modify the existing column's data type. There is a different syntax for every condition. Let's discuss it one by one.

Add column in the table.

Syntax to add column in the table:

ALTER TABLE table_name ADD columnnamedatatype;

Here table_name is the table's name in which you want to add the column, columnname is the name of the column that you want to add to the table, and datatypeis the column's data type.

For example, in the database SCHOOL, we have a table STUDENT having columns STU_ID, STU_NAME, STU_ROLL_NO, STU_CITY, STU_CONTACT, and STU_AGE. We are going to add an extra column name as STU_DATE_OF_BIRTH.

We can do this as follows:

ALTER TABLE SCHOOL ADD STU_DATE_OF_BIRTH INT (11);

Before adding the column, the table looks like below:

After adding the column (STU_DATE_OF_BIRTH), thetable looks like below.

We can see that the column has been added successfully.

Delete column:

Syntax to delete a column from the table:

ALTER TABLE table_name DROP COLUMNcolumnname;

Here table_name is the table's name, and columnname is the name of the column that you want to delete.

For example, we have a table STUDENT with many columns, and we want to delete the STU_DATE_OF_BIRTH column from the table STUDENT. Let's try this in MySQL.

Command to delete a column from the table:

ALTER TABLE STUDENT DROP COLUMN STU_DATE_OF_BIRTH;

In MySQL, before deleting the column.

After deleting the column, our table is shown below. Here, we can see thecolumn has been deleted successfully.

Modify the data type of the column.

Syntax to modify the data type of the column.

ALTER TABLE table_name MODIFYcolumnnamedatatype;

Here table_name is the name of the table and columnname is the column's name, and the datatype is the data type of the column.

For example, we want to change the data type of column STU_ROLL_NO from INT to VARCHAR. We can use the below command to change the data type:

ALTER TABLE STUDENT MODIFY STU_ROLL_NO VARCHAR (32);

In MySQL, before changing the data type.

After changing the data type, we can see the data type of the column STU_ROLL_NO changed successfully.

Copy MySQL table

Sometimes, we need to copy the data of one table to another table. For that, we need to use the SELECT syntax. This syntax will copy all the data of the table to another table. It is noted that the column name and data typeof both the table should be the same.

Syntax to copy the data from one table to another:

SELECT * INTO new_table_name FROMold_table_name;

                                Or

SELECT * INTO new_table_name FROM old_table_name WHERE condition;

For example, we have a table STUDENT and another table CLASS. We have to copy the data of table STUDENT to the table CLASS.

Command to copy the data.

SELECT * INTO CLASS FROM STUDENT;

Truncate MySQL table

Suppose you want to delete all the table rows, but the tablestructure, columns, and data type should remain there. For this, we use a truncate statement in SQL. Truncate statement used to delete all the rows of the selected table.

Syntax to delete all the data from the table:

TRUNCATE TABLE table_name;

Here table_name is the name of the table.

For example, we have a table STUDENT. If we want to delete all the data present in the table, we use the syntax as below:

TRUNCATE TABLE STUDENT:

The below image shows that after using the truncate statement, all the row gets deleted, and the output shown is an empty set.

Drop MySQL table

Drop statement deletes the table from the database. This statement deletes all the data along with the table structure.

The syntax used to delete the table:

DROP TABLE table_name;

Here table_name is the name of the table.

For example, we have a database SCHOOL that contains two tables, student and class. Let’s delete table STUDENT from the database in MySQL using this syntax.

DROP TABLE STUDENT;

In the below image, before using the drop command, we had two tables. But after using the command,the table student has been deleted.

Note: We cannot get the table again, so be careful while using this command.


Related Topics

MySQL Advance table Query

MySQL Advance table Query The table is created by index, rows, and columns in the MySQL database. The user saves their information in matrix format. The database requires a query to...

14 minutes read.

MySQL: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 minutes read.

MySQL RTRIM() function

In this context, we will learn how we can use the MySQL RTRIM() function with proper syntax and good examples. Introduction of MySQL RTRIM() function It is the function in MySQL that...

2 minutes read.

MySQL Queries

MySQL Queries MySQL supports SQL queries in the MySQL interface. These queries help to interact data with the application. MySQL uses create database, user database, create a table, truncate table, and...

13 minutes read.

MySQL LOG() function

In this context, we will learn how we can use the MySQL LOG () function with proper syntax and good examples. Introduction of MySQL LOG() function The LOG() function in MySQL is...

3 minutes read.

MySQL Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL Error 1046 - No Database Selected

What is MySQL? MySQL is a Relational Database Management System (RDBMS). SQL in MySQL is the abbreviation of "Structured Query Language", which Oracle developed it in 1995. It is one of...

1 minute read.

MySQL LENGTH() Function

In this context, we will learn how we can use the MySQL LENGTH() function with proper syntax and good examples. Introduction of MySQL LENGTH() function This function in MySQL is used to...

2 minutes read.

MySQL PERIOD_ADD() function

In this context, we will learn how we can use the MySQL PERIOD_ADD() function with proper syntax and good examples. Introduction of MySQL PERIOD_ADD() function In MySQL, PERIOD_ADD() function will help to...

2 minutes read.

MySQL variable

The MySQL variable is essential for storing data in the table. The variable declares data with a specific name or label to avoid confusion. This data label is used in...

6 minutes read.

MySQL Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL COUNT function

The count function returns the number of rows in the table. This function shows either the entire rows count or the required row count of the table. It determines the...

5 minutes read.

MySQL DATE_ADD() function

In this context, we will learn how we can use the MySQL DATE_ADD() function with proper syntax and good examples. Introduction of MySQL DATE_ADD() function In MySQL, the DATE_ADD() function is used...

3 minutes read.

MySQL GREATEST function

This statement displays the greatest values of the table. The GREATEST function returns a null value when the table contains a null value. The GREATEST function needs a minimum of...

2 minutes read.

MySQL Subquery

The subquery is a MySQL query used to manage data operations. Mainly subquery helps to retrieve data with the necessary condition. It creates a nested query with two different queries....

5 minutes read.

MySQL FORMAT() function

In this context, we will learn how we can use the MySQL FORMAT() function with proper syntax and good examples. Introduction of MySQL FORMAT() function This function in MySQL helps to format...

2 minutes read.

MySQL SEC_TO_TIME() function

In this context, we will learn how we can use the MySQL SEC_TO_TIME() function with proper syntax and good examples. Introduction of MySQL SEC_TO_TIME() function This function in MySQL is used to...

2 minutes read.

MySQL LOCATE() function

In this context, we will learn how we can use the MySQL LOCATE() function with proper syntax and good examples. Introduction of MySQL LOCATE() function LOCATE() function in MySQL is used for...

3 minutes read.

MySQL RAND() function

In this context, we will learn how we can use the MySQL RAND() function with proper syntax and good examples. Introduction of MySQL RAND() function Basically, in MySQL the RAND() function is...

3 minutes read.

MySQL DATE_FORMAT() function

In this context, we will learn how we can use the MySQL DATE_FORMAT() function with proper syntax and good examples. Introduction of MySQL DATE_FORMAT() function DATE_FORMAT() function in MySQL is used to...

3 minutes read.