Table type (storage engine) selection
MySQL storage engine support, including MyISAM, InnoDB, BDB, MERGE, EXAMPLE, NDB, Cluster, ARCHIVE, CSV, BLACKHOLE, FEDERATED and other things which InnoDB and BDB provide security table, other storage engines are non-transaction-safe tables.
View the current default storage engine
show variables like 'table_type';
SHOW ENGINES;
When you create a table can be a table set up new storage engine by adding keywords ENGINE
CREATE TABLE ai(
i bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY(i)
)ENGINE=MyISAM DEFAULT CHARSET=gbk;
CREATE TABLE country(
country_id samllint unsigned not null auto_increment,
country varchar(30) not null,
last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)engine=InnoDB DEFAULT CHARSET=gbk;
Change the storage engine
ALTER TABLE ai engine = INNODB
show create table ai
Results are as follows, the display changes to InnoDB engine
CREATE TABLE `ai` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`bin` bit(1) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
Select the appropriate storage engine
InnoDB
InnoDB storage engine provided with a commit, rollback, crash recovery capabilities of transaction-safe processing.
1 auto grow row
CREATE table autoincre_demo
(i smallint not null auto_increment,
name varchar(10), PRIMARY key(i)
)engine=innodb;
insert into autoincre_demo values(1,'1'),(0,'2'),(null,'3')
select * from autoincre_demo;
Statement forces set the initial value of the automatic growth column, the default starting at 1.
You can use LAST_INSERT_ID () to query the current value of the last inserted record thread used, if one is inserted more than one record, then the automatic return of the growth column is the first record to use.
select LAST_INSERT_ID();
INSERT into autoincre_demo(name) VALUES('5'),('6'),('7')
For InnoDB tables, columns must be indexed automatically increase. If it is a composite index, it must be the first column of the composite index.
2. The foreign key constraint
CREATE TABLE `country` (
`country_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`country` varchar(50) NOT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`country_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
create table city(
city_id smallint unsigned not null auto_increment,
city varchar(50) not null,
country_id smallint unsigned not null,
last_update timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY key(city_id),
key idx_fx_country_id(country_id),
constraint 'fk_city_country' foreign key(country_id) references country(country_id) on DELETE restrict on update CASCADE
)engine=INNODB default charset=utf8;
3. storage
type of data
CHAR和VARCHAR
CREATE TABLE vc1(v VARCHAR(4), c CHAR(4));
insert into vc1 VALUES('ab ','ab ');
select concat(v,'+'),concat(c,'+') from vc1;
Since CHAR is a fixed length, the processing speed is much faster than VARCHAR, but the drawback is a waste of storage space
TEXT and BLOB
The main difference between the two can be used to store binary BLOB data, such as photos; and TEXT can only save character data.
To optimize the operation of the table
insert into table1 VALUES(1,repeat('haha',20));
insert into table1 VALUES(2,repeat('haha',20));
insert into table1 VALUES(3,repeat('haha',10));
insert into table1 select * from table1;
insert into table1 select * from table1;
optimize table table1;
Queries measures to increase the use of synthetic index text field
create table table2(id varchar(100),context blob,hash_value varchar(40));
insert into table2 values(1,repeat('beijing',2),md5(context));
insert into table2 values(2,repeat('beijing1',2),md5(context));
insert into table2 values(3,repeat('beijing2',2),md5(context));
select * from table2;
If you want to query "beijing2 beijing2" records can be queried by the hash value.
select * from table2 where hash_value=md5(repeat('beijng1',2))
Floating-point and fixed-point
Floating point values used generically to denote fractional part comprising, when a field is defined as floating-point, if the accuracy of data exceeds the actual accuracy is inserted into the column, the value will be rounded off to insert the actual definition precision value.
CREATE table table3(f float(8,1));
insert into table3 VALUES(1.23456);
SELECT * from table3;
CREATE table test(c1 float(10,2), c2 decimal(10,2));
insert into test values(131072.32,131072.32);
select * from test;
Date type selection
character set
show character set;
Query the current character set and collation server:
show variables like 'character_set_server';
Create a new database with a new character sets:
create database databasename default charset gbk;