Mysql database (3) Mysql table structure management

  1. MySQL data types

  1. Number Type

  (1) Integer data types include TINYINT/BIT/BOOL/SMALLINT/MEDIUMINT/INT/BIGINT

  (2) Floating point data types include FLOAT/DOUBLE/DECIMAL

  Principles: 1. Choose the smallest available type, if the value never exceeds 127, use TINYINT rather than INT.

      2. For everything that is entirely numeric, you can choose an integer type.

        3. The floating point type is used for numbers that may have a fractional part. Such as unit price of goods, online shopping delivery amount, etc.

  2. String type

  (1) Common text string types include CHAR (values ​​from 1 to 255) and VARCHAR (variable-length strings)

  (2) TEXT and BLOB types. Their size can be changed, the TEXT type is suitable for storing long text, and the BLOB type is suitable for storing binary data, supporting any data such as text, sound, and images.

  (3) Special types SET and ENUM.

  Principles: 1. From the perspective of speed, to select a fixed column, you can use the CHAR type.

      2. To save space, use dynamic columns, you can use the VARCHAR type.

      3. To make a selection of the contents of the column, you can use the ENUM type.

      4. To allow more than one entry in a column, the SET type can be used.

      5. If the content to be searched is not case-sensitive, you can use the TEXT type.

      6. If the content to be searched is case sensitive, you can use the BLOB type.

  3. Date and time data types

  (1)DATE:YYYY-MM-DD

  (2)TIME:HH:MM:SS

  (3)DATETIME:YYYY-MM-DD HH:MM:SS

  (4) TIMESTAMP: Time label, the display format used when processing the report depends on the value of M.

  (5) YEAR: The year can specify two-digit and four-digit formats.

  2. Create the table

mysql> use db_library;
Database changed
mysql> CREATE TABLE tb_bookinfo(
    -> barcode varchar(30),
    -> bookname varchar(70),
    -> typeid int(10) unsigned,
    -> author varchar(30),
    -> ISBN varchar(20),
    -> price float(8,2),
    -> page int(10) unsigned,
    -> bookcase int(10) unsigned,
    -> inTime date,
    -> del tinyint(1) DEFAULT'0',
    -> id int(11) NOT NULL
    -> );
Query OK, 0 rows affected (0.03 sec)

   1. Set the default storage engine

mysql> CREATE TABLE tb_booktype(
    -> id int(10) unsigned NOT NULL,
    -> typename varchar(30),
    -> days int(10) unsigned
    -> )ENGINE=MyISAM;
Query OK, 0 rows affected (0.01 sec)

   2. When setting the auto-increment type field, it is best to set it as the primary key (note the difference between backticks and single quotes, the backticks are the one below Esc)

mysql> CREATE TABLE tb_booktype1(
    -> id int(10) unsigned NOT NULL AUTO_INCREMENT,
    -> typename varchar(30),
    -> days int(10) unsigned,
    -> PRIMARY KEY (`id`)
    -> );
Query OK, 0 rows affected (0.03 sec)

   3. Set the character set

mysql> CREATE TABLE tb_booktype2(
    -> id int(10) unsigned NOT NULL AUTO_INCREMENT,
    -> typename varchar(30),
    -> days int(10) unsigned,
    -> PRIMARY KEY (`id`)
    -> )DEFAULT CHARSET=GBK;
Query OK, 0 rows affected (0.02 sec)

   4. Copy the table structure to create a copy of the data table tb_bookinfo tb_bookinfobak in the database db_library.

  

 

  

Guess you like

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