创建mysql表格_创建MySQL表

创建mysql表格

创建mysql表格

创建MySQL表 (Create MySQL Tables)

To begin with, the table creation command requires the following details −

首先,表创建命令需要以下详细信息-

  • Name of the table

    表名
  • Name of the fields

    字段名称
  • Definitions for each field

    每个字段的定义

句法 (Syntax)

Here is a generic SQL syntax to create a MySQL table −

这是创建MySQL表的通用SQL语法-


CREATE TABLE table_name (column_name column_type);

Now, we will create the following table in the TUTORIALS database.

现在,我们将在TUTORIALS数据库中创建下表。


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, a few items need explanation −

在这里,一些项目需要解释-

  • Field Attribute NOT NULL is being used because we do not want this field to be NULL. So, if a user will try to create a record with a NULL value, then MySQL will raise an error.

    使用字段属性NOT NULL的原因是我们不希望该字段为NULL。 因此,如果用户尝试使用NULL值创建记录,则MySQL将引发错误。

  • Field Attribute AUTO_INCREMENT tells MySQL to go ahead and add the next available number to the id field.

    字段属性AUTO_INCREMENT告诉MySQL继续并将下一个可用数字添加到id字段。

  • Keyword PRIMARY KEY is used to define a column as a primary key. You can use multiple columns separated by a comma to define a primary key.

    关键字PRIMARY KEY用于将列定义为主键。 您可以使用由逗号分隔的多个列来定义主键。

从命令提示符创建表 (Creating Tables from Command Prompt)

It is easy to create a MySQL table from the mysql> prompt. You will use the SQL command CREATE TABLE to create a table.

从mysql>提示符创建MySQL表很容易。 您将使用SQL命令CREATE TABLE创建一个表。

(Example)

Here is an example, which will create tutorials_tbl

这是一个示例,它将创建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 a command until you give a semicolon (;) at the end of SQL command.

– MySQL不会终止命令,直到在SQL命令的末尾加上分号(;)为止。

使用PHP脚本创建表 (Creating Tables Using PHP Script)

To create new table in any existing database you would need to use PHP function mysql_query(). You will pass its second argument with a proper SQL command to create a table.

要在任何现有数据库中创建新表,您将需要使用PHP函数mysql_query() 。 您将使用适当SQL命令传递其第二个参数来创建表。

(Example)

The following program is an example to create a table using PHP script −

以下程序是使用PHP脚本创建表的示例-


<html>
   <head>
      <title>Creating MySQL Tables</title>
   </head>
   
   <body>
      <?php
         $dbhost = 'localhost:3036';
         $dbuser = 'root';
         $dbpass = 'rootpassword';
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);
         
         if(! $conn ) {
            die('Could not connect: ' . mysql_error());
         }
         echo 'Connected successfully<br />';
         $sql = "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 )); ";
         mysql_select_db( 'TUTORIALS' );
         $retval = mysql_query( $sql, $conn );
         
         if(! $retval ) {
            die('Could not create table: ' . mysql_error());
         }
         echo "Table created successfully\n";
         mysql_close($conn);
      ?>
   </body>
</html>

翻译自: https://www.tutorialspoint.com/mysql/mysql-create-tables.htm

创建mysql表格

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值