Table
Table
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.
Parameters
table_name: It specifies the name of the table that you want to modify.
new_column_name: It specifies the name of the new column that you want to add to
the table.
column_definition: It specifies the data type and definition of the column (NULL or
NOT NULL, etc).
Example:
In this example, we add a new column "cus_age" in the existing table "cus_tbl".
Output:
See the recently added column:
Output:
Example:
In this example, we add two new columns "cus_address", and cus_salary in the existing
table "cus_tbl". cus_address is added after cus_surname column and cus_salary is
added after cus_age column.
Syntax:
Example:
Output:
Example:
Output:
6) RENAME table
Syntax:
Example:
Output:
See the renamed table:
The TRUNCATE command works the same as a DELETE command without using
a WHERE clause that deletes complete rows from a table. However, the TRUNCATE
command is more efficient as compared to the DELETE command because it removes
and recreates the table instead of deleting single records one at a time. Since this
command internally drops the table and recreates it, the number of rows affected by the
truncate statement is zero, unlike the delete statement that returns the number of
deleted rows.
This command does not maintain the transaction log during the execution. It deallocates
the data pages instead of rows and makes an entry for the deallocating pages instead
of rows in transaction logs. This command also locks the pages instead of rows; thus, it
requires fewer locks and resources.
The following points must be considered while using the TRUNCATE command:
o We cannot use the WHERE clause with this command so that filtering of records is not
possible.
o We cannot rollback the deleted data after executing this command because the log is
not maintained while performing this operation.
o We cannot use the truncate statement when a table is referenced by a foreign key or
participates in an indexed view.
o The TRUNCATE command doesn't fire DELETE triggers associated with the table that
is being truncated because it does not operate on individual rows.
Syntax
The following syntax explains the TRUNCATE command to remove data from the table:
In this syntax, first, we will specify the table name which data we are going to remove.
The TABLE keyword in the syntax is not mandatory. But it's a good practice to use it to
distinguish between the TRUNCATE() function and the TRUNCATE TABLE statement.
Next, we will add values to this table using the below statement:
Now, verify the table by executing the SELECT statement whether the records inserted
or not:
Now, execute the following statement that truncates the table customer using the
TRUNCATE syntax discussed above:
The UPDATE statement is used with the SET and WHERE clauses. The SET clause is
used to change the values of the specified column. We can update single or multiple
columns at a time.
Syntax
Following is a generic syntax of UPDATE command to modify data into
the MySQL table:
1. UPDATE table_name
2. SET column_name1 = new-value1,
3. column_name2=new-value2, ...
4. [WHERE Clause]
Parameter Explanation
The description of parameters used in the syntax of the UPDATE statement is given
below:
Play Video
Parameter Descriptions
column_nam It is the name of a column in which we want to perform updation with the new value using the
e SET clause. If there is a need to update multiple columns, separate the columns with a comma
operator by specifying the value in each column.
WHERE It is optional. It is used to specify the row name in which we are going to perform updation. If
Clause we omit this clause, MySQL updates all rows.
Note:
IGNORE: This modifier allows the statement to do not abort the execution even if errors
occurred. If it finds duplicate-key conflicts, the rows are not updated.
This query will update the email id of Java course with the new id as follows:
1.
In that case, we need to log into the MySQL server and disable foreign key checks
before executing the TRUNCATE statement as below:
1. SET FOREIGN_KEY_CHECKS=0;
Now, we are able to truncate tables. After execution, re-enable foreign key checks as
given below:
1. SET FOREIGN_KEY_CHECKS=1;
How to truncate all tables in MySQL?
The TRUNCATE statement in MySQL will delete only one table at a time. If we want to
delete more than one table, we need to execute the separate TRUNCATE statement.
The below example shows how to truncate multiple tables in MySQL:
We can also use the below SQL query that generates several TRUNCATE TABLE
commands at once using the table names in our database:
Step 1: Open the MySQL Command Line Client that appeared with a mysql> prompt.
Next, log in to the MySQL database server using the password that you have created
during the installation of MySQL. Now, you are connected to the MySQL server, where
you can execute all the SQL statements.
Step 2: Next, choose the specific database by using the command below:
Let us understand it with the example given below. Suppose we have a database name
"mystudentdb" that contains many tables. Then execute the below statement to list the
table it contains:
If we want to show or list the table name from different databases or database to which
you are not connected without switching, MySQL allows us to use the FROM or IN
clause followed by the database name. The following statement explains it more clearly:
Output:
Syntax
The following are the syntax to use pattern matching with show table command:
We can understand it with the example given below where percent (%) sign assumes
zero, one, or multiple characters:
Now, we are going to see how we can use the WHERE clause with the SHOW TABLES
command to list different types of tables (either Base or View type) in the selected
database:
Here, we can also see another example of Show Tables statement with the WHERE
clause: