--using the specified database Usetest;--Create a database with the name MydabCREATE DATABASEMydab;--Deleting a databaseDROP DATABASEMydab;--Create a tableCREATE TABLEt_mytable (--primary key is primary key column, element cannot be duplicated, cannot be empty--auto_increment Auto-complete column to ensure the primary key is not duplicatedIdINT PRIMARY KEYauto_increment, UserNameVARCHAR( -), SexVARCHAR(2), birthday DATE, phoneVARCHAR( One))DEFAULTCHARSET=UTF8;--default Format--Delete a tableDROP TABLEt_mytable;--Inserting RecordsINSERT intoT_mytable (Username,sex,birthday,phone)VALUES('Zhang San','male','1990-05-33','15775108922');--ShowSELECT * fromt_mytable;--Add a columnALTER TABLET_mytableADDEduVARCHAR(Ten);--Modify a column ALTER TABLEt_mytable change Edu eduVARCHAR( -); --Delete a column ALTER TABLET_mytableDROP COLUMNedu;--Timestamp is the default current time ALTER TABLET_mytableADDEntertimeTIMESTAMP; INSERT intoT_mytable (Username,sex,birthday,phone)VALUES('John Doe','male','1992-12-22','13409871234'); --default indicates that Edu is high school ALTER TABLET_mytableADDEduVARCHAR( -)DEFAULT 'High School'; INSERT intoT_mytable (username,sex,birthday,phone,edu)VALUES('Robau','male','1990-12-22','13409891234','Primary School');INSERT intoT_mytable (username,sex,birthday,phone,edu)VALUES('Lo Yuan','male','1990-12-22','13409891234','PhD');--Create an index, add an index to a table, sort the column, and increase the query speed;CREATE INDEXIndex_name ont_mytable (userName);--Delete IndexALTER TABLET_mytableDROP INDEXindex_name--adding constraints to wife;ALTER TABLET_mytableADDWifeVARCHAR( -)UNIQUE;--Modify a recordUPDATET_mytableSETWife='Luo June Bao' WHEREId=1;UPDATET_mytableSETWife='June Luo Bao' WHEREId=3;ALTER TABLEt_mytable change Edu eduVARCHAR( -)DEFAULT 'High School' not NULL;INSERT intoT_mytable (Username,sex,birthday,phone)VALUES('Monkey King','male','1990-12-22','13409891234');CREATE TABLEt_room (IDINT PRIMARY KEYauto_increment,addressVARCHAR( -), priceINT, ManidINT--foreign key column, foreign key column load from the table, use this column and the user table to contact)DEFAULTCHARSET=UTF8;--Add CONSTRAINT Fk_1 indicates that a FOREIGN key constraint is added, Fk_1 is a foreign KEY constraint name--FOREIGN Key (Manid) means adding foreign key constraints to that column--the Manid column in T_room must reference the value of the T_mytable primary key columnALTER TABLET_roomADD CONSTRAINTFk_1FOREIGN KEY(Manid)REFERENCESt_mytable (ID);--Delete a foreign key constraintALTER TABLET_roomDROP FOREIGN KEYFk_1;DROP TABLET_room;INSERT intoT_room (Address,price,manid)VALUES('No. 3rd Hongxing Road','500000',2);
MySQL some additions and deletions to change the search