A: Table
Table has fields, is key, a value field value, the data line is a row describing the attribute value of a series of things.
# Syntax: Create Table table name ( field name 1 Type [ (width) constraints ] , field name 2 types of [ (width) constraints ] , Field Name Type 3 [ (width) constraints ] ); # Note: 1 . in the same table, the same field names is not 2 . constraints optional width and 3 . field names and types is required
Two: Data Types
Numeric types
Date Time Type
String type
enum and set types
1. Value Type
Integer general use int
int not standardized by age, but generally used int
11 phone number, int enough, usually with char, character type
Numerical ranges expressed int width is not constrained, constrained only by the scope of, behind which the digital display is only constraint of different widths, with versions also may be bad
So do not add int width constraints, the default is 11 on it, there is a symbol, the figure was only 10, save any phone number
Decimal general use decimal represent money, float double is not exact, double a little more than quasi-byte float so much.
create table float_tb( -> f1 float, -> d1 double);
May see inconsistent length and precision float double representation, but rarely used so many double accurate to one decimal place, the money also to the points, two decimal places
decimal (65,30) 30 bits may decimal place, the reason for such a standard, because the string is used to deposit the underlying
We can see you do not specify the decimal precision to retain only the integer part, and will carry, so when prices are defined, decimal (11,2) to million to the minutes
2. Date Time Type
It indicates the type of date and time is a time value DATETIME, DATE, TIMESTAMP, TIME, and YEAR.
Each type has a value of time effective range and a "zero" value of "zero" when the value of the specified unlawful MySQL can not be represented.
TIMESTAMP specific types of automatic update feature, which will be described later.
timestamp only to 2038, so it is rarely used, but with datatime minutes and seconds to represent the year, month, day
The default timestamp data type is not null, as long as the insert data or update data, timestamp will automatically update time
create table t1(y year,d date,dt datetime,ts timestamp);
Insert year with an error, it may be a version of the problem, this version can not, but you can see insert y field, ts automatically inserted
Under linux can
timestamp is automatically updated
A look behind the timestamp added a NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
CREATE TABLE `t2` ( `y` year(4) DEFAULT NULL, `d` date DEFAULT NULL, `dt`datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`t` time DEFAULT NULL, `ts` timestamp ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
In the 5.5 version to 5.6.4, for DEFAULT CURRENT_TIMESTAMP clause, designated only on TIMESTAMP type column.
5.6.5 From the start (including 5.7), DEFAULT CURRENT_TIMESTAMP clause can be assigned to the TIMESTAMP or DATETIME type column.
If you can not be built through the table, select version (); check version
3. String
Commonly used char (10), up to 255 characters in length, varchar65535 character length, the former fixed length character string, a character pass your specified character is open space, here 10
varchar (15) variable-length string, based on the number of characters transmitted to a corresponding open space, ABC transfer, then only the open three characters in length
The former space is wasted, but the efficiency is relatively high, which is less wasted space, but inefficient, because the process of determining the character length
Phone number ID number are used char, a lot of sites to name a few user password on a few, are used in the underlying char
Comments, microblogging, status with varchar
5.ENUM and type SET
Chinese name called ENUM enumerated type, its value range to be displayed by enumeration mode when creating tables. ENUM allows only a single value selected from the value set, but can not take a plurality of values .
SET and ENUM very similar, but also a String object, which can contain 0-64 members. According to various members, it is different on storage. set type can allow an arbitrarily selected set of values or a plurality of elements are combined . Out of range of the content will not allow the injection, the repeated values will be automatically de-emphasis.
MySQL > Create Table T10 (name char ( 20 is ), Gender enum ( ' FEMALE ' , ' MALE ' )); Query the OK, 0 rows affected ( 0.01 sec) # Select enum ( ' FEMALE ' , ' MALE ' ) in a gender entry as the value to be normally inserted MySQL > iNSERT INTO T10 values ( ' Nezha ' , ' MALE ' ); Query the OK, . 1affected Row ( 0.00 sec) # can not be inserted into the ' MALE, FEMALE ' two values, can not be inserted does not belong to ' MALE, FEMALE ' value MySQL > INSERT INTO T10 values ( ' Nezha ' , ' MALE, FEMALE ' ); ERROR 1265 ( 01000 ): the Data truncated for column ' Gender ' AT Row . 1 MySQL > Create Table T11 (name char ( 20 is ), HobbySET ( ' smoke ' , ' drink ' , ' hot head ' , ' overturn ' )); Query the OK, 0 rows affected ( 0.01 sec) # can select any SET ( ' smoke ' , ' drink ' , ' hot head ' , ' roll ' item) in, and comes to a weight function MySQL > INSERT INTO T11 values ( ' Yuan ' , ' hot head, drinking,Hot head '); Query the OK, . 1 Row affected ( 0.01 sec) MySQL > SELECT * from T11; + - ---- + --------------- + | name | Hobby | + - - ---- + --------------- + | Yuan | drinking, hot head | + - ---- + ------------ + --- . 1 Row in SET ( 0.00 sec) # can not select SET ( ' smoke ' , ' drink ' , 'Hot head ' , ' overturn ' ) in the entry, MySQL > INSERT INTO T11 values ( ' Alex ' , ' hot head, roll, see sister ' ); ERROR 1265 ( 01000 ): the Data truncated for column ' Hobby ' AT Row . 1