Common database data types and constraints

Common data types

MYSQL database supports a plurality of types, it can be divided into three types: numeric, date / time character type and

Here we will list some common data types

1, numeric

Represents an integer data type (tinyint, smallint, mediumint, int) floating point data type (float, double)

 

usage:

1), plastic (Student ID)

To store different size difference value range

tinyint (m) value of [-128,127]

tinyint (m) unsigned (can be omitted) ZEROFILL values ​​[0,255]

m is defined as the field length, and when the zerofill with, insufficient accuracy of the data, automatically filled with a 0 For example: (001)

2), float (height)

float (m, z) m: total length field z: number of digits after the decimal point

The value [-3.4 x 10 - 38 , 3.4 x 10 38 ]

Disadvantages: loss of precision, lower compared to the double precision

2, character

It represents a character data type (char, varchar)

char (m) m affect memory length (name)

3, time and date type

It represents the date and time types (datetime, date, time, and year) time value

Mysql in constraints

1、not null

not null constraint that is non-empty, specifies the value of a row can not be null.

S to establish a table id to the int type, not null constraint

Id is null will insert data error

NOTE: A value of all types can be null, including int, float, dateTime data types empty string ( ") is not equal to null, 0 is not equal to null.

2、unique

unique represents a unique constraint. The only constraint is specified column or combination of columns in the table can not be repeated, to ensure the uniqueness of the data. But may be a plurality of null, the same table can have multiple unique constraints, a plurality of column constraints combination.

When creating a unique constraint, unique constraint name if you do not give it the default and the same column name. The only constraint is the default MySQL will create a unique index on the column;

 The only constraint is achieved by a unique index, in other words, a unique index is the basis for unique constraints. So, to build a unique constraint, it will automatically create a unique index, but to build a unique index, does not automatically create a unique constraint.

 

Construction of the table to add a unique constraint doc

 

Example insert data

Insert success. For explanation, when the time is null, unique index is not the only judgment of

3、primary key

effect:用来保证数据完整性

Features:

1), corresponding to a unique primary key constraint constraint constraint combination of non-null +, the column must be unique primary key constraint, null values ​​are not permitted;

2) Each table allows only one primary key, the primary key of constraint can be created at the column level, you can also create a table level;

3), MySQL primary key names are always PRIMARY, when you create a primary key constraint, the system will create a unique index on the corresponding combination where the column and row;

4、foreign key

Action: to ensure referential integrity between two tables or a constructed reference relationship between two fields or two fields of a table of two tables.

Features:

1), must be found or the master table is empty foreign key values ​​from the table.

2), when the recording table are referred to from the main table, the main table records will not be deleted, if you want to delete the data, the need to remove the dependency data record from the table, and then can delete data in the main table

 

Guess you like

Origin www.cnblogs.com/li8537/p/11403898.html