day12: data types and constraints

First, understand the type of data

  Data type refers to the data type of a column in the table can contain. MySQL data type used to support a large number of the specified data type column can contain, according to some common characteristic, these data types can be divided into the following types: numeric, string, date

1, Value: value of type for storing digital data. For example, in a given scene, and we rented a room or several rooms available for rent for numeric data types. This data type may be further divided into the following categories: integer data types, decimal data type, Bit data type

Integer data type is used to store an integer value. May be an integer positive or negative, therefore, the use of which contained one kind of integer data type declaration may be acceptable acceptable negative value. Integer or fractional part is absolutely no decimal point, therefore, declared as an integer column will never accept the value with a fractional part. Commonly used integer type is: int

With decimal data type for storing a value with an integer part and a fractional part. Thus, using one decimal data type declaration may accept the value of the column with no fractional part, without the value of the integer value portion and comprising two portions. The same can be positive or negative. Decimal common types: float and double

bit data type for storing integer and fractional values ​​except. Common bit data types: bit

 

2, the string: to store a character or binary values, the common character types: char and varchar

3, Date: date data type for storing a time value or date. Thus, it should accept a date data type column associated with either the date or time value. Common types date: DATE (date data), time (time data), year (year of data)

 

Second, create a table

The basic syntax:

create table table name (

  Field Name Field Type (field length),

  Field Name Field Type (field length),

  ...

  Field Name Field Type (field length)

);

Application constraints:

Constraint definition must comply with the rules, in order to maintain data integrity. Constraints can be defined in the process of creating a table, you can also be added later. After creating tables add constraints, it checks to determine whether the existing data constraint violation. If data violates the constraint to be added, then the constraint is not applied to a specified column.

Constraint type:

  Primary key constraint, the only constraint, foreign key constraint

Primary key constraint: one column or set of columns defined, all rows value that uniquely identifies a column in the table. These columns are called the primary key column. Further, the primary key column can not contain a NULL value, since it is used to uniquely identify a row in a table. Columns defined with a primary key: primary key, for example:

 

create  table  student(

  sid  varchar(55)  primary  key  not  null,

  sname  varchar(55)  not  null,

  sage  varchar(55)  not  null

);

or

create  table  student(

  sid  varchar(55)   not  null,

  sname  varchar(55)  not  null,

  sage  varchar(55)  not  null,

     primary  key( sid)

);

The only constraint: The only constraint is used to enforce uniqueness of non-primary key column. Primary key constraint column automatically include restrictions on uniqueness. The only constraint is similar to the primary key constraint, but it is the only column of the defined constraints allow NULL values. A plurality of unique constraints may be added to the table. However, the table can have only one primary key, which may be based on one or a group of columns. The only constraint is defined by: unique, for example:

create  table  teacher(

  time varchar (55) Unique,

  tname  varchar(55),

  take VARCHAR (55)

);

or:

create  table  teacher(

  time varchar (55),

  tname  varchar(55),

  take varchar (55),

  unique (time)

);

Foreign key constraints: when a data dependency table data in another table, the foreign key constraint maintain the consistency of the data in these tables. The foreign key table is always referred to the primary key column in the other table.

 

CREATE TABLE department (
id INT auto_increment PRIMARY KEY,
dname CHAR (255),
INDEX (id)
);

CREATE TABLE employee (
did INT,
username CHAR (255),
PASSWORD CHAR (255),
FOREIGN KEY (did) REFERENCES department (id)
);

 

Guess you like

Origin www.cnblogs.com/wuguiyu/p/11572728.html