MySQL table properties, indexes, constraints

Source: Internet
Author: User

1. Table Properties

Basic syntax for creating a table:

CREATE TABLE "If not exists" Table name (field list, index or constraint list ")" Table option List "

Where the field list is formatted as follows:

Field Name Type "Property List",

Field Name Type "Property List" ...

Each property in the property list is separated by a space.

Common Field Properties:

Auto_increment Set field values to grow automatically for integer types
Primary key Sets the field as the primary key, at which point the value of the field can "uniquely determine" a row of data
Unique key The SET field is unique and is not duplicated throughout the data table
NOT NULL The SET field cannot be null, and if you do not specify the property, the value of the field is nullable by default.
Default Set the default value of the field, and when you insert the data, the default value is populated if you do not specify the field's value.
Comment Setting a description for a field

Note: Primary key and unique key both determine the uniqueness of the field, the fields decorated by these two properties can uniquely determine a row of data, the difference is that primary key cannot be null (when primary key is specified, the default specified not Null property), and the unique key can be null. It can be said that primary key is a special unique key.

Code:

/* CREATE TABLE, note properties */
Mysql> Create Tableitem_properties_table ( -IdintAuto_incrementPrimary Key, -Namevarchar( -) not NULL Unique Key, -Pwdvarchar( -) not NULL, -Agetinyint default -, -Emailvarchar( -) Comment'Email' - ); Query OK,0Rows Affected (0.02sec)
/* View the table structure primarily to view the Comment*/mysql>Show FullColumns fromitem_properties_table;+-------+-------------+-----------------+------+-----+---------+----------------+----------------------------- ----+--------------+|Field|Type|Collation| Null | Key | Default |Extra| Privileges |Comment|+-------+-------------+-----------------+------+-----+---------+----------------+----------------------------- ----+--------------+|Id| int( One)| NULL |NO|Pri| NULL |Auto_increment| Select,Insert,Update,References | ||Name| varchar( -)|Utf8_general_ci|NO|UNI| NULL | | Select,Insert,Update,References | ||Pwd| varchar( -)|Utf8_general_ci|NO| | NULL | | Select,Insert,Update,References | ||Age| tinyint(4)| NULL |YES| | - | | Select,Insert,Update,References | ||Email| varchar( -)|Utf8_general_ci|YES| | NULL | | Select,Insert,Update,References |Email|+-------+-------------+-----------------+------+-----+---------+----------------+----------------------------- ----+--------------+5Rowsinch Set(0.00sec) MySQL>

2. Index

MySQL table properties, indexes, constraints

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.