MySQL Stored Procedure to create a table?

MySQLMySQLi Database

Following is the query to create a stored procedure that creates a table. Here, we are creating a table with three columns, one of them is Id −

mysql> DELIMITER //
   mysql> CREATE PROCEDURE Stored_Procedure_CreatingTable()
   BEGIN
      create table DemoTable
      (
      Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
      UserFirstName varchar(20),
      UserLastName varchar(20)
      );
   END;
   //
   Query OK, 0 rows affected (0.16 sec)
mysql> DELIMITER ;

Now you can call stored procedure with the help of CALL command −

mysql> call Stored_Procedure_CreatingTable();
Query OK, 0 rows affected (0.18 sec)

Let us check the description of table using DESC command −

mysql> desc DemoTable;

This will produce the following output −

+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| Id            | int(11)     | NO   | PRI | NULL    | auto_increment |
| UserFirstName | varchar(20) | YES  |     | NULL    |                |
| UserLastName  | varchar(20) | YES  |     | NULL    |                |
+---------------+-------------+------+-----+---------+----------------+
3 rows in set (0.04 sec)
raja
Published on 20-May-2019 16:02:19
Advertisements