You are on page 1of 3

MySQL::MySQL5.7ReferenceManual::15.8.

1Creating
InnoDBTables
TocreateanInnoDBtable,usetheCREATETABLEstatement.Youdonotneedtospecifythe
ENGINE=InnoDBclauseifInnoDBisdefinedasthedefaultstorageengine,whichisthedefaultasofMySQL
5.5.YoumightstilluseENGINE=InnoDBclauseifyouplantousemysqldumporreplicationtoreplaythe
CREATETABLEstatementonaserverwherethedefaultstorageengineisnotInnoDB.
Defaultstorageengine=InnoDB.
CREATETABLEt1(aINT,bCHAR(20),PRIMARYKEY(a));
BackwardcompatiblewitholderMySQL.
CREATETABLEt2(aINT,bCHAR(20),PRIMARYKEY(a))ENGINE=InnoDB;
AnInnoDBtableanditsindexescanbecreatedinthesystemtablespace,inafilepertabletablespace,or
inageneraltablespace(introducedinMySQL5.7.6).Wheninnodb_file_per_tableisenabled,whichis
thedefaultsettingasofMySQL5.6.6,anInnoDBtableisimplicitlycreatedinanindividualfilepertable
tablespace.Conversely,wheninnodb_file_per_tableisdisabled,anInnoDBtableisimplicitlycreatedin
thesystemtablespace.WiththeintroductionofgeneraltablespacesinMySQL5.7.6,youcanuseCREATE
TABLE...TABLESPACEsyntaxtoexplicitlycreateanInnoDBtableinanyofthethreetablespacetypes.
WhenyoucreateanInnoDBtable,MySQLcreatesa.frmfileinadatabasedirectoryundertheMySQLdata
directory.Foratablecreatedinafilepertabletablespace,an.ibdfileisalsocreated.Atablecreatedinthe
systemtablespaceiscreatedintheexistingsystemtablespaceibdatafiles.Atablecreatedinageneral
tablespaceiscreatedinanexistinggeneraltablespace.ibdfile.
Internally,InnoDBaddsanentryforeachtabletotheInnoDBdatadictionary.Theentryincludesthe
databasename.Forexample,iftablet1iscreatedinthetestdatabase,thedatadictionaryentryis
'test/t1'.Thismeansyoucancreateatableofthesamename(t1)inadifferentdatabase,andthetable
namesdonotcollideinsideInnoDB.
ViewingthePropertiesofInnoDBTables
ToviewthepropertiesofInnoDBtables,issueaSHOWTABLESTATUSstatement:
mysql>SHOWTABLESTATUSFROMtestLIKE't%'\G;
***************************1.row***************************
Name:t1
Engine:InnoDB
Version:10
Row_format:Compact
Rows:0
Avg_row_length:0
Data_length:16384
Max_data_length:0
Index_length:0

Data_free:0
Auto_increment:NULL
Create_time:2015031615:13:31
Update_time:NULL
Check_time:NULL
Collation:latin1_swedish_ci
Checksum:NULL
Create_options:
Comment:
1rowinset(0.00sec)
Inthestatusoutput,youseetheRowformatpropertyoftablet1isCompact.TheDynamicorCompressed
rowformatisrequiredtakeadvantageofInnoDBfeaturessuchastablecompressionandoffpagestorage
forlongcolumnvalues.Tousetheserowformats,youcanenableinnodb_file_per_table(thedefaultas
ofMySQL5.6.6)andsetinnodb_file_formattoBarracuda,whichimplicitlycreatesInnoDBtablesinfile
pertabletablespaces:
SETGLOBALinnodb_file_per_table=1;
SETGLOBALinnodb_file_format=barracuda;
CREATETABLEt3(aINT,bCHAR(20),PRIMARYKEY(a))ROW_FORMAT=DYNAMIC;
CREATETABLEt4(aINT,bCHAR(20),PRIMARYKEY(a))ROW_FORMAT=COMPRESSED;
Or,youcanuseCREATETABLE...TABLESPACEsyntaxtocreateanInnoDBtableinageneraltablespace.
Generaltablespacessupportallrowformats.Formoreinformation,seeSection15.7.9,InnoDBGeneral
Tablespaces.
CREATETABLEt1(c1INTPRIMARYKEY)TABLESPACEts1ROW_FORMAT=DYNAMIC;
CREATETABLE...TABLESPACEsyntaxcanalsobeusedtocreateInnoDBtableswithaDynamicrow
formatinthesystemtablespace,alongsidetableswithaCompactorRedundantrowformat.
CREATETABLEt1(c1INTPRIMARYKEY)TABLESPACE=innodb_systemROW_FORMAT=DYNAMIC;
InnoDBtablepropertiesmayalsobequeriedusingtheInnoDBInformationSchemasystemtables:
SELECT*FROMINFORMATION_SCHEMA.INNODB_SYS_TABLESWHERENAME='test/t1'\G
***************************1.row***************************
TABLE_ID:45
NAME:test/t1
FLAG:1
N_COLS:5
SPACE:35
FILE_FORMAT:Antelope
ROW_FORMAT:Compact
ZIP_PAGE_SIZE:0
SPACE_TYPE:Single

1rowinset(0.00sec)
DefiningaPrimaryKeyforInnoDBTables
AlwayssetupaprimarykeyforeachInnoDBtable,specifyingthecolumnorcolumnsthat:
Arereferencedbythemostimportantqueries.
Areneverleftblank.
Neverhaveduplicatevalues.
Rarelyifeverchangevalueonceinserted.
Forexample,inatablecontaininginformationaboutpeople,youwouldnotcreateaprimarykeyon
(firstname,lastname)becausemorethanonepersoncanhavethesamename,somepeoplehave
blanklastnames,andsometimespeoplechangetheirnames.Withsomanyconstraints,oftenthereisnot
anobvioussetofcolumnstouseasaprimarykey,soyoucreateanewcolumnwithanumericIDtoserve
asallorpartoftheprimarykey.Youcandeclareanautoincrementcolumnsothatascendingvaluesare
filledinautomaticallyasrowsareinserted:
ThevalueofIDcanactlikeapointerbetweenrelateditemsindifferenttables.
CREATETABLEt5(idINTAUTO_INCREMENT,bCHAR(20),PRIMARYKEY(id));
Theprimarykeycanconsistofmorethanonecolumn.Anyautoinccolumnmustcome
first.
CREATETABLEt6(idINTAUTO_INCREMENT,aINT,bCHAR(20),PRIMARYKEY(id,a));
Althoughthetableworkscorrectlywithoutdefiningaprimarykey,theprimarykeyisinvolvedwithmany
aspectsofperformanceandisacrucialdesignaspectforanylargeorfrequentlyusedtable.Itis
recommendedthatyoualwaysspecifyaprimarykeyintheCREATETABLEstatement.Ifyoucreatethe
table,loaddata,andthenrunALTERTABLEtoaddaprimarykeylater,thatoperationismuchslowerthan
definingtheprimarykeywhencreatingthetable.

You might also like