Database---Operation of Tables

1. Create a table
Format:
create table tablename(
field name 1 type (length) constraint,
field name 2 type (length) constraint,
...
);
Example:
CREATE TABLE stu(
id INT PRIMARY KEY , #primary key: set as the primary key, by The data modified as the primary key cannot be repeated or null.
NAME VARCHAR(100)
);

2. View all tables in the database;
SHOW TABLES;

desc table name; view table structure
DESC stu;

3. Delete table
DROP TABLE database name;
DROP TABLE stu02;

4. Modify the table structure format
ALTER TABLE database name ADD column name type (length) constraints;
ALTER TABLE stu ADD sex VARCHAR(2); #Add a column to the table.

ALTER TABLE database name MODIFY column name type (100) constraints;
ALTER TABLE stu MODIFY sex VARCHAR(100); #Modify column attributes (type, length, constraints) in the table.

ALTER TABLE database name CHANGE column name new column name type (length) constraints;
ALTER TABLE stu CHANGE sex newSex VARCHAR(255); #Modify the column name of the table, and set the type, length and constraints for the new column name.

ALTER TABLE table name DROP column name; #delete column in table
ALTER TABLE stu DROP newSex;

RENAME TABLE table name TO new table name; #modify table name
RENAME TABLE stu TO newStu;

ALTER TABLE table name CHARACTER SET character set; #modify The character set of the table
ALTER TABLE newStu CHARACTER SET GBK;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325955175&siteId=291194637