Mysql Create and delete data table commands and syntax detailed _mysql

Source: Internet
Author: User
Tags mysql create php script

Creating data Tables
The following information is required to create a MySQL datasheet:

    • Table name
    • table field Name
    • Define each table field

Grammar

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

CREATE TABLE table_name (column_name column_type);

In the following example, we will create a datasheet in the tutorials database tutorials_tbl:

TUTORIALS_TBL (
  tutorial_id INT not null auto_increment,
  tutorial_title VARCHAR (MB) not NULL,
  Tutorial_ Author VARCHAR () not NULL,
  submission_date date,
  PRIMARY KEY (tutorial_id)
);

Instance resolution:
If you don't want the field to be null, you can set the property to not NULL, and the error occurs if the data entered for the field is null when you manipulate the database.
The auto_increment definition is listed as an attribute, which is typically used for primary keys, and the value is automatically increased by 1.
The PRIMARY key keyword is used to define a column as a primary key. You can use multiple columns to define primary keys, separated by commas.
To create a table from a command prompt
The mysql> Command window makes it easy to create a MySQL datasheet. You can create a datasheet using SQL statement creation table.
Instance
The following is an instance of creating a datasheet 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 NOT NULL,
  -> tutorial_author VARCHAR (=) NOT null,
  -> submission_date date,->
  PRIMARY KEY (tutorial_id)
  ->);
Query OK, 0 rows affected (0.16 sec)
Mysql>

Note: The MySQL command Terminator is a semicolon (;).
create a datasheet using a PHP script
You can use PHP's mysql_query () function to create a data table with existing databases.
The function has two parameters that return TRUE if the execution succeeds, or FALSE.
Grammar

BOOL mysql_query (SQL, connection);

Instance
The following example uses the PHP script to create the data table:

 
 


Delete data table

It's easy to delete data tables in MySQL, but you need to be careful when you delete a table, because all the data disappears after the delete command is executed.
Grammar
The following is a common syntax for removing MySQL data tables:

DROP TABLE table_name;

To delete a datasheet in a command prompt window
to delete a datasheet SQL statement as a drop table in the mysql> Command Prompt window:
Instance
The following instance deletes the datasheet tutorials_tbl:

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

To delete a datasheet using a PHP script
PHP uses the mysql_query function to remove the MySQL data table.
The function has two parameters that return TRUE if the execution succeeds, or FALSE.
Grammar

BOOL mysql_query (SQL, connection);

Instance
The following example uses a PHP script to delete the datasheet tutorials_tbl:

 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.