MySQL basic learning _ lesson 012 _ insert data into the table

MySQL inserts data into the table

 

Insert a single piece of data into the data table

Syntax format:

INSERT INTO 
    表名(字段名1,字段名2,字段名3,...) 
VALUES
    (值1,值2,值3,...);

Requirement: The number of fields and the number of values ​​are the same, and the data types should also correspond to the same

Example: Insert data into the t_testtable data table created in lesson 011

For details of lesson 011, please refer to: https://blog.csdn.net/weixin_43184774/article/details/115085959

method one:

INSERT INTO
    t_testtable(no,name,sex,classno,birth)
VALUES
    (1,'zhangsan','1','class001', '1990-06-11');

Way two:

Change the position of the field, but it should be noted that the value of values ​​must be consistent with the corresponding field

INSERT INTO 
    t_testtable(name,sex,classno,birth,no) 

VALUES
    ('lisi','2','class002', '1998-04-15',2);

Way three:

Insert only one field and the corresponding field value. The result is that there are no other values. If there is no NOT NULL restriction, it will be displayed as NULL.

INSERT INTO
    t_testtable(name)
VALUES
    ('wanglaoliu');

We can view the table structure of t_testtable by executing the desc t_testtable; command. At this time, we can see that the Default display is NULL, that is, when we do not assign values ​​to other fields, the default display is NULL

Note: If the Default of the sex field in the t_testtable data table is set to 1 by default, when only one field and the corresponding field value are inserted again, the sex field is displayed as 1 by default, and is no longer NULL.

CREATE TABLE t_testtable(
    no bigint,
    name VARCHAR(255) ,
    sex char(1) default 1,
    classno VARCHAR(255),
    birth CHAR(10)
);

Way four:

Omit the field name, but the number of columns and the order of the columns can not be confused

INSERT INTO
    t_testtable
VALUES
    (2,'zhuzhiqiang','1','class002', '1968-08-12');

 

2. Insert multiple pieces of data into the data table

When inserting multiple statements into the data table at one time, separate the values ​​and values ​​with English commas

INSERT INTO
    t_testtable(no,name,sex,classno,birth)
VALUES
    (1, '张三', '1', 'class001', '1991-01-11'),
    (2, '李四', '1', 'class002', '1992-02-12'),
    (3, '王五', '0', 'class003', '1993-03-13'),
    (4, '赵六', '1', 'class004', '1994-04-14'),
    (5, '田七', '0', 'class005', '1995-05-15');

Guess you like

Origin blog.csdn.net/weixin_43184774/article/details/115113360