Click on the understandingCRMEB more merchantsOther open source projects:To learn moreLittle Sister QR codeCreate a table structure
CREATE TABLE user (
user_id int(11) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL
);
Copy the code
- Note that the statement ends with a semicolon (;) As a terminator, the definition of the last field ends without a comma.
- Data type
int(11)
Represents an integer type with an 11-bit display length. The parameter 11 in parentheses represents the maximum effective display length, independent of the value range contained in the type.varchar(255)
Represents a variable string type of maximum length 255 - NOT NULL indicates that the entire field cannot be NULL and is a data constraint.
- AUTO_INCREMENT indicates that the primary key automatically grows.
Modify table structure
- . Add a field, for example, I add an age field to the table of type int(11)
ALTER TABLE user ADD (age int(11)); Copy the code
- Change the field name to user_age
ALTER TABLE user RENAME COLUMN age to user_age Copy the code
- Change the data type of the field to float(3,1) for player_age
ALTER TABLE user MODIFY (user_age float(3,1)); Copy the code
- Delete the field, delete the player_age field you just added
ALTER TABLE user DROP COLUMN user_age; Copy the code
Three. Table design principles
- The fewer tables you have, the better
- The fewer fields in the table, the better: The more fields, the greater the likelihood of data redundancy.
- The fewer fields in a table that have a federated primary key, the better: the primary key is set to determine uniqueness, and when a field cannot be uniquely identified, the federated primary key is used
- The more primary and foreign keys you use, the better