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
  1. Note that the statement ends with a semicolon (;) As a terminator, the definition of the last field ends without a comma.
  2. Data typeint(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
  3. NOT NULL indicates that the entire field cannot be NULL and is a data constraint.
  4. AUTO_INCREMENT indicates that the primary key automatically grows.

Modify table structure

  1. . 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
  2. Change the field name to user_age
    ALTER TABLE user RENAME COLUMN age to user_age
    Copy the code
  3. 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
  4. 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

  1. The fewer tables you have, the better
  2. The fewer fields in the table, the better: The more fields, the greater the likelihood of data redundancy.
  3. 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
  4. The more primary and foreign keys you use, the better