Home>Article>Database> Introduction to Mysql data table operation methods

Introduction to Mysql data table operation methods

不言
不言 forward
2019-01-08 10:01:32 4395browse

This article brings you an introduction to the Mysql data table operation method. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Data table interpolation

The most important operation in operating the data table is to save our website data and user data. Let’s take a look at the command rules first:

INSERT [INTO] tbl_name [(col_name,col_name,...)] VALUES(val,val,...)

From the above rules we can see that [(col_name,col_name,...)] can be filled in or not. The difference is; if not filled in [(col_name, col_name,...)] must pass in the values of all fields at once. Fill in [(col_name, col_name,...)] to pass in the corresponding col_name value. (Recommended course:MySQL Tutorial)

So, let’s check the structure of the data tableuserwe built last time. After passing the value, enter the command

SHOW COLUMNS FROM user;

Introduction to Mysql data table operation methods

We can see that there are four fieldsusename,age,passwrod,gz, Let’s interpolate all fields at once.

INSERT user VALUES('Tom',25,'abc123456',12000);

Running command~successful.

Introduction to Mysql data table operation methods

##Now let’s giveusename,passwrodInterpolation between two fields

INSERT user (usename,passwrod) VALUES('Jieke','101010');

Run command~success

Introduction to Mysql data table operation methods

Find the record value

We have just inserted several values, now let’s query whether the value of the field is the same as what we inserted.

Find record value command rules:

SELECT expr,... FROM tbl_name;
Enter the command:

//当然实际上的查询命令非常,现在只是演示简单查找命令 SELECT * FROM user;

Introduction to Mysql data table operation methods

You can see that the values just inserted exist. in the datasheet.

Null and non-null values

In the website registration information, there are settings for required fields and fillable fields. This setting is also available in mysql, which is null and non-null values

NULL,NOT NULL. Now let's create a new data table and create fields.

CREATE TABLE newuser( name VARCHAR(20) NOT NULL, age TINYINT UNSIGNED NULL )

We set two fields above, thenamefield cannot be empty, and theagefield can be empty.Now let’s insert the field value

INSERT newuser (name,age) VALUES('Timo',null);

Introduction to Mysql data table operation methods

The value was inserted successfully.

**Now, let’s try what happens if the value of the
namefield is NULL.

INSERT newuser (name,age) VALUES(NULL,NULL);

Introduction to Mysql data table operation methods

You can see the error,

Columns 'name' cannot be nullThe name field cannot be set to a null value. So the null and non-null values we set have successfully taken effect.

Default value

We can set the default value for the field

DEFAULT. When inserting a record, if there is no explicit value assigned, the set default value will be automatically assigned.
Now let’s re-create a data table tb2 and set the default value forsexinname,sex. Enter the command line:

CREATE TABLE tb2( name VARCHAR(20), sex ENUM('1','2','3') DEFAULT '3' );

Insert record name, do not insert record for sex

INSERT tb2(name) VALUES('ggb');

Introduction to Mysql data table operation methods

The insertion is successful, we record the output in the data table and see ifsexhas a value

Introduction to Mysql data table operation methods
##You can see that the

sex

value is 3, which has been assigned the default value.

The above is the detailed content of Introduction to Mysql data table operation methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete