Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the statement of creating data table in mysql database

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to write statements about mysql database creation data tables. Xiaobian thinks it is quite practical, so share it with you as a reference. I hope you can gain something after reading this article.

In mysql, you can create a data table using the CREATE TABLE statement in the syntax format CREATE TABLE [table options][partitioning options];, where the table definition options consist of column names, column definitions, and possibly null descriptions, integrity constraints, or table indexes.

The process of creating a data table is the process of specifying attributes for data columns and enforcing constraints on data integrity, including entity integrity, referential integrity, and domain integrity. Next, let's look at the syntax for creating data tables.

basic syntax

In MySQL, tables can be created using the CREATE TABLE statement. The syntax is:

CREATE TABLE [table definition options][table options][partition options];

The format of Table Definition Option is:

[,…]

CREATE TABLE command syntax is more, it is mainly by table creation definition (create-definition), table options (table-options) and partition options (partition-options) composed of.

Here we describe a simple example of creating a new table, and then highlight some of the main syntax points in the CREATE TABLE command.

The main syntax and instructions for the CREATE TABLE statement are as follows:

CREATE TABLE: To create a table with a given name, you must have permissions on the table CREATE.

: Specifies the name of the table to be created, given after CREATE TABLE, and must conform to identifier naming conventions. The table name is specified as db_name.tbl_name to create tables in a specific database. Whether or not there is a current database, it can be created in this way. You can omit db-name when creating tables in the current database. If you use quoted distinguished names, you should quote database and table names separately. For example,'mydb'. 'mytbl' is legal, but 'mydb.mytbl' is illegal.

Table creation definition, consisting of column names (col_name), column definitions (column_definition), and possibly null descriptions, integrity constraints, or table indexes.

By default, tables are created into the current database. An error occurs if the table already exists, if there is no current database, or if the database does not exist.

Tip: When creating a table using CREATE TABLE, you must specify the following information:

The name of the table to be created is case-insensitive and keywords in the SQL language, such as DROP, ALTER, INSERT, etc., cannot be used.

The name and data type of each column (field) in the data table, separated by commas if creating multiple columns.

Creates a table in the specified database

The database table belongs to the database. Before creating the database table, you should use the statement "USE" to specify which database the operation is performed in. If no database is selected, a No database selected error will be thrown.

example

Create the employee table tb_emp1 with the structure shown in the following table.

Select database test_db to create table, create tb_emp1 data table, input SQL statement and run result as shown below.

mysql> USE test_db;Database changedmysql> CREATE TABLE tb_emp1 -> ( -> id INT(11), -> name VARCHAR(25), -> deptId INT(11), -> salary FLOAT -> );Query OK, 0 rows affected (0.37 sec)

After the statement is executed, a data table named tb_emp1 is created. Use the SHOW TABLES; statement to check whether the data table is successfully created, as shown below.

mysql> SHOW TABLES;+--------------------+| Tables_in_test_db |+--------------------+| tb_emp1 |+--------------------+1 rows in set (0.00 sec) How to write statements about mysql database to create data tables is shared here. I hope the above content can be of some help to everyone and can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report