Detailed example of creating and deleting tables in MySQL

Detailed example of creating and deleting tables in MySQL

The table creation command requires:

  • The name of the table
  • Field Name
  • Define each field (type, length, etc.)

grammar

Following is the general SQL syntax used to create a MySQL table:

CREATE TABLE table_name (column_name column_type);

Now, we will create the following tables in the test database.

create table tutorials_tbl(
  tutorial_id INT NOT NULL AUTO_INCREMENT,
  tutorial_title VARCHAR(100) NOT NULL,
  tutorial_author VARCHAR(40) NOT NULL,
  submission_date DATE,
  PRIMARY KEY ( tutorial_id )
);

Here, some data items need explanation:

The field uses the NOT NULL attribute because we do not want the value of this field to be NULL. Hence, if user will try to create a record with NULL value then MySQL will generate an error.

The AUTO_INCREMENT attribute of the field tells MySQL to automatically increment the id field to the next available number.

The keyword PRIMARY KEY is used to define this column as a primary key. You can define multiple columns to define a primary key by using commas to separate them.

1. Create a table through the command prompt

From the mysql> prompt, it is easy to create a MySQL table. Use the SQL command CREATE TABLE to create a table.

Example

Here is an example that creates a table: tutorials_tbl

root@host#mysql -u root -p
Enter password:
mysql> use TUTORIALS;
Database changed
mysql> CREATE TABLE tutorials_tbl(
  -> tutorial_id INT NOT NULL AUTO_INCREMENT,
  -> tutorial_title VARCHAR(100) NOT NULL,
  -> tutorial_author VARCHAR(40) NOT NULL,
  -> submission_date DATE,
  -> PRIMARY KEY ( tutorial_id )
  -> );
Query OK, 0 rows affected (0.16 sec)
mysql>

Note: MySQL does not terminate the command until a semicolon (;) is given to indicate the end of the SQL command.

It is very easy to delete existing MySQL tables but you have to be very careful while deleting any existing table as the data lost after deleting the table cannot be recovered.

grammar

This is the generic SQL syntax used to delete a MySQL table:

DROP TABLE table_name ;

1. Delete the table from the command prompt

This requires only executing the DROP TABLE SQL command at the MySQL> prompt.

Example

Here is an example, which will delete the table: tutorials_tbl

root@host#mysql -u root -p
Enter password:
mysql> use test;
Database changed
mysql> DROP TABLE tutorials_tbl
Query OK, 0 rows affected (0.8 sec)
mysql>

Summarize

The above is a detailed example of how to create and delete a table in MySQL. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • MySQL table deletion operation implementation (differences between delete, truncate, and drop)
  • How to delete table data in MySQL
  • Delete a field in the MySQL table from the command line
  • Tutorial on how to delete a table in MySQL
  • MySQL create and delete table operation command example explanation
  • Three ways to delete a table in MySQL (summary)

<<:  In-depth understanding of the matching logic of Server and Location in Nginx

>>:  In-depth understanding of slot-scope in Vue (suitable for beginners)

Recommend

How to change the host name in Linux

1. View the current host name [root@fangjian ~]# ...

Briefly talk about mysql left join inner join

Preface I have been busy developing a cold chain ...

MySQL 5.7.33 installation process detailed illustration

Table of contents Installation package download I...

SSM VUE Axios Detailed Explanation

Table of contents How to display SQL log? ? Descr...

MySQL 5.6 installation steps with pictures and text

MySQL is an open source small relational database...

How to install Jenkins on CentOS 8

To install Jenkins on CentOS 8, you need to use t...

jQuery implements accordion small case

This article shares the specific code of jQuery t...

8 Reasons Why You Should Use Xfce Desktop Environment for Linux

For several reasons (including curiosity), I star...

Basic structure of HTML documents (basic knowledge of making web pages)

HTML operation principle: 1. Local operation: ope...

Solution for installing opencv 3.2.0 in Ubuntu 18.04

Download opencv.zip Install the dependencies ahea...

HTML (css style specification) must read

CSS style specifications 1. Class Selector 2. Tag...

Nginx reverse proxy to go-fastdfs case explanation

background go-fastdfs is a distributed file syste...