search
HomeDatabaseMysql TutorialMySQL creates data table
MySQL creates data tableDec 07, 2016 pm 01:19 PM
mysql

Creating a MySQL data table requires the following information:

Table name

Table field name

Define each table field

The following is the general SQL syntax for creating a MySQL data table:

CREATE TABLE table_name (column_name column_type);

The following example will create the data table runoob_tbl in the RUNOOB database:

runoob_tbl(
runoob_id INT NOT NULL AUTO_INCREMENT,
runoob_title VARCHAR(100) NOT NULL,
runoob_author VARCHAR(40) NOT NULL,
submission_date DATE,
PRIMARY KEY ( runoob_id )
);

Example analysis:

If you don’t want the field to be NULL, you can set the field’s attribute to NOT NULL. When operating the database, if the data entered in this field is NULL, an error will be reported.

AUTO_INCREMENT is defined as an auto-incrementing attribute, generally used for primary keys, and the value will automatically increase by 1.

PRIMARY KEY keyword is used to define columns as primary keys. You can use multiple columns to define the primary key, separated by commas.

Create a table through the command prompt

You can easily create a MySQL data table through the mysql> command window. You can use the SQL statement CREATE TABLE to create a data table.

The following is an example of creating a data table runoob_tbl:

root@host# mysql -u root -p
Enter password:**********
mysql> use RUNOOB;
Database changed
mysql> CREATE TABLE runoob_tbl(
-& gt; runoob_id into null auto_increment,
-& gt; runoob_title varchar (100) not null,
-& gt; runoob_autHor Varchar (40) not null,
- & gt; su; bmission_date date,
-& gt; Primary Key (runoob_id)
-> );
Query OK, 0 rows affected (0.16 sec)
mysql>

Note: The MySQL command terminator is a semicolon (;).

Use PHP script to create a data table

You can use PHP's mysql_query() function to create a data table in an existing database.

This function has two parameters and returns true when executed successfully, otherwise it returns false.

bool mysql_query(sql, connection);

Parameter Description

sql Required. Specifies the SQL query to be sent. Note: The query string should not end with a semicolon.

connection Optional. Specifies the SQL connection identifier. If not specified, the last opened connection is used.

Example

The following example uses PHP script to create a data table:

Create MySQL data table< ;/title><p></p> <p></p>

$dbhost ='localhost:3036';

$dbuser ='root';

$dbpass ='rootpassword ';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn )

{

die('Connection failed: '. mysql_error());

}

echo 'Connection successful
';

$sql ="CREATE TABLE runoob_tbl( ".

"runoob_id INT NOT NULL AUTO_INCREMENT, ".

"runoob_title VARCHAR(100) NOT NULL, ".

"runoob_author VARCHAR(40) NOT NULL, ".

"submission_date DATE, ".

"PRIMARY KEY ( runoob_id )); ";

mysql_select_db('RUNOOB');

$retval = mysql_query( $sql, $conn );

if(! $retval )

{

die('Data table creation failed: '. mysql_error());

}

echo "Data table created successfully";

mysql_close( $conn);

?>

Run results:

MySQL creates data table

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
图文详解mysql架构原理图文详解mysql架构原理May 17, 2022 pm 05:54 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

mysql怎么去掉第一个字符mysql怎么去掉第一个字符May 19, 2022 am 10:21 AM

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.