This article uses examples to describe the basic operations of MySQL data tables, including table structure operations and field operations. Share with you for your reference, the details are as follows: This section describes:Table structure operations
Field Operations
First release time: 2018-02-18 21:31 Table structure operationsCreate a data table:
create table [if not exists] table name ( Field name data type, Field Name Data Type)[Table Options];
-- Before creating a table, you must specify a database. You can use use to specify which database subsequent operations are based on, or you can use the database name as a prefix to specify which database the data table is created in.
View the datasheet:View the data table to view the existing data table and the field information of the data table
-- View all tables show tables; -- View some tables show tables like 'Fuzzy Match'; -- View the table creation statement show create table data table name; -- Rotate to view the structure show create table data table name\G; -- View the table structure: View the field information in the table: Desc/desc table name; describe table name; show columns from table name;
show tables; legend:
Desc/describe /show columns from table name; Modify the data table structure:When modifying a table, you can only modify the table name and table options.
-- Modify the table name: rename table old table name to new table name; --Modify table options: Alter table table name table option [=] value;
rename table student to my_student; rename table class to my_class; -- Alter table my_student charset gbk; Alter table my_collation_bin collate =utf8_bin; Delete the data table:
Drop table tablename1, tablename2...;
drop table demo; drop table demodata;
Field operations:New fields:New fields are added based on the existing table.
Alter table table name add [column] field name data type [column attributes] [position];
Alter table table name add [column] field name data type [column attributes] [position]; Alter table demo add column id int first; Alter table demo add id int; Alter table demo add class int after age; Alter table demo add number int not null after age;
Modify the fields:Modifying a field generally involves modifying the field data type or field attributes
Alter table table name modify field name data type [attribute] [position];
Alter table my_student modify number char(10) after id; Alter table demo modify number int null; -- alter table student modify name varchar(20) not null; -- alter table student modify name varchar(20) not null primary key;
Rename fields:
Alter table table name change old field new field data type [attribute] [position];
alter table demo change class room varchar(10); Alter table my_student change sex gender varchar(10);
Delete a field:
Alter table table name drop field name;
Alter table my_student drop age; alter table demo drop room;
Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL transaction operation skills", "MySQL stored procedure skills", "MySQL database lock related skills summary" and "MySQL common function summary" I hope this article will be helpful to everyone's MySQL database design. You may also be interested in:
|
>>: Detailed explanation of jquery tag selector application example
Preface In a relatively complex large system, if ...
Preface As a heavy user of front-end frameworks, ...
The table structure is as follows. There are only...
The virtual machine is in use or cannot be connec...
mysqlslap Common parameter description –auto-gene...
Table of contents 1. Project Environment 2. Proje...
1. Write a simple Java program public class tests...
Table of contents Water wave effect Let's see...
1. IE8's getElementById only supports id, not ...
This article shares the specific code of vue+swip...
1: Download MySql Official website download addre...
Frequently asked questions Access denied for user...
Table of contents A JSON is built on two structur...
background Recently, I executed a DML statement d...
The recommended code for playing background music ...