MySQL data types and constraints

First, create a table full syntax:

# Syntax: 
the Create the Table table name (
Field Name Type 1 [(width) constraints],
Field Name Type 2 [(width) constraints],
Field Name Type 3 [(width) constraints]
);

# Note: 
1 . In the same table, the same field name can not
 2 width and the constraints Alternatively, field names and types is required.
 3 after the last field is not a comma.!

# Added: 
# 1 refers to the width of the stored limit data 
create table userinfo (name char);
insert into userinfo values('jason');
"""
1. There is no safe mode version of the database, but will only be able to store data deposit into a j
2. The latest version of the database can not be stored directly error prompt: Data too long for column 'name' at row 1
"""

# 2 >>> null constraints acquaintance with null NUT 
Create Table T1 (ID int, char name Not null);
INTO T1 values INSERT ( . 1, ' J ' );   # normal memory 
INSERT INTO T1 values (2, null);   # error

# Summed up the difference between the type of constraint 
# Type: Limit field must be based on what type of data storage 
# constraints: constraints is to add an additional type of restriction beyond

Second, the basic data types

1, integer

create table t1(x tinyint);
INTO T1 values INSERT ( 128), (- 129 ); # being given in strict mode. Because of range

create table t2(x tinyint unsigned);   #unsigned 取消符号
T2 values INTO INSERT ( -1), (256 ); # being given in strict mode, the range over

create table t3 (x int unsigned); # being given in strict mode, the range over
insert into t3 values(4294967296);

Note: For integer, the data type of the memory width is not limiting, but display limitations, so when you create a table, if the field is used in integer type, completely without specifying the width of the display, the default display width, sufficient display the complete original data is stored.

 

Introduce strict mode and setting:

Show Variables like " % mode% " ;   # view the database variable name configuration comprising a mode configuration parameters 
# modify security mode 
SET the session # only the current user interface active 
SET Global   # globally valid 

SET Global the sql_mode = ' the STRICT_TRANS_TABLES ' 
# After modifying exit current client can re-login

 

2, float

# Storage limit 
a float (255,30 ) # minimum accuracy
(Double 255,30 ) # accuracies
decimal ( 255,30 ) # maximum accuracy

# Testing the accuracy 
Create Table T9 (X a float (255,30 ));      
create table t10(x double(255,30));
create table t11(x decimal(65,30));

insert into t9 values(1.111111111111111111111111111111);
insert into t10 values(1.111111111111111111111111111111);
insert into t11 values(1.111111111111111111111111111111);

 

3, character type

char , varchar

the Table T10 the Create (name char (4))   # exceed four characters being given, the four characters is not enough space to complement 
the Create the Table T11 (name VARCHAR (4))   # exceed four characters error, not enough to keep a few there are several four more

# Authentication storage limit 
INSERT INTO T12 values ( ' Hello ' );
insert into t13 values('hello');
# 验证存储长度
insert into t12 values('a'); #'a    '
insert into t13 values('a'); #'a'
select * from t12
the SELECT * from T13   # can not see the real results of 

the SELECT CHAR_LENGTH (name) from T12
CHAR_LENGTH the SELECT (name) from T13   # still can not see the real results

"" " It should first be sure that the hard disk stored data is absolutely true, but the time will automatically display the mysql Remove trailing spaces " "" 
# If you do not want to do mysql help you automatically remove trailing spaces of operation, need to add a mode of 
the SET , Ltd. Free Join sql_mode = " STRICT_TRANS_TABLES, PAD_CHAR_TO_FULL_LENGTH " ;
 # to exit the client to re-visit 
the SELECT CHAR_LENGTH (the X-) from T12; # 4 
the SELECT CHAR_LENGTH (the y-) from T13; # 1

# For the char type, mysql is stored in the data will be stored with spaces complement to your hard disk. But it will automatically take down the trailing spaces when reading out the results


"""
The difference between the use of char and varchar
"" " 
Name char ( . 5 )
 # disadvantages: wasted space 
# advantages: faster access speed 
egon alex lxx jxx txx  

VARCHAR name ( . 5 )
 # drawbacks: slow access speed 
# advantages: saving space 
1bytes + egon 1bytes + alex 1bytes + lxx 1bytes + jxx 1bytes + txx

 

4, the time type

date , time ,datetime ,year

create table student(
    id int,
  name char(16),
  born_year year,
  birth date,
  study_time time,
  reg_time datetime
);
insert into student values(1,'egon','2019','2019-05-09','11:11:00','2019-11-11 11:11:11');

 

5, enumeration and collection types

enum,set

create table user(
    id int,
  name char(16),
  gender enum('male','female','others')
);
insert into user values(1,'jason','xxx')  # 报错
insert into user values(2,'egon','female')  # 正确!


create table teacher(
    id int,
  name char(16),
  gender enum('male','female','others'),
  hobby set('read','sleep','sanna','dbj')
);
Teacher values INTO INSERT ( . 1, ' Egon ' , ' MALE ' , ' Read, SLEEP, DBJ ' )   # set may also be present only a

 

Third, the constraints

"""
PRIMARY KEY (PK) for the identity of the table's primary key field, record the unique identifier
FOREIGN KEY (FK) field identifies the foreign key for the table
NOT NULL identifier of the field can not be blank
UNIQUE KEY (UK) identification value of this field is the only
AUTO_INCREMENT field value identifies the automatic growth (integer type, and the primary key)
DEFAULT set a default value for the field

Unsigned UNSIGNED
ZEROFILL filled with 0
"""

 

supplement:

delete from tb1;
He stressed: all records above this command really can table are deleted, but not id will be reset to zero,
So close that section is not a command to clear the table, delete some of qualifying is used to delete records in the table
delete from tb1 where id > 10;

If you want to empty the table, use truncate tb1;
Action: resetting the entire table, id recording again from 0

 

Guess you like

Origin www.cnblogs.com/wujc3/p/11384928.html