Series of SQL insert data bit (D)

Bit by bit [SQL] for the analysis of a series of articles summarizes the bits and pieces of the actual development, from the most simple SQL queries to query the comprehensive analysis
in the analysis of SQL, will be analyzed at the same time related operations mybatis, Hibernate in
clicks see details

This section describes the data inserted in the database

1 insert a new record

Inserted into the table of a new record.
Such as adding a new user data to the table, you can write this

insert into t_user (user_id,user_name,user_age,user_address,user_phone)
values(32,'小不点',18,"龙城大街一号院",192303023994)

In MySQL and DB2 may be selected insert a single row, or insert more rows

insert into t_user (user_id,user_name,user_age,user_address,user_phone)
values
(32,'小不点',18,"龙城大街一号院",192303023994)(33,'小李子',20,"龙城大街二号院",192303023995)

insert statement allows you to create a new row in the database table, in all types of database systems, insert statement syntax exactly the same, of course, is the simple wording

insert into t_user 
values('山西省','小不点',"龙城大街一号院",18,32,192303023994,)

Note that if the target field into the row is not listed, you must insert all of the columns in the table.

2 insert a default value

When definition table can define default values ​​for some columns, now insert a row to default values, without specifying the value of each column.

2.1 The default value for the specified column when creating a table
creat table t_user (user_id default 0)

All database systems support explicitly specify the default keyword insert a column value, some databases also support other ways to solve this problem

Use the default values ​​when data is inserted 2.2
insert into t_user (user_id,user_name,user_age,user_address,user_phone)
values(32,'小不点',18,default,default)

When not all columns in the table are the interpolated values ​​can be used to explicitly specify the name of the default value of the column, as described above wrote user_address, user_phone using default

Oracle8i database and earlier versions do not support the default keyword, insert a default value can not be displayed in Oracle9 c previous database

In MySQL, if all the columns in the table defines the default values, you can use an empty value to solve the problem

insert into t_user values()

In this case, all columns will be set to their default values

PostgreSQL and SQL Service in support of the default values ​​clause

insert into t_user default values 

default values ​​clause all columns set to their default values ​​itself.

default keyword in the list of values ​​for the corresponding column insert the default value, the default value is defined when you create the table, all of the database can use some keywords.

3 with a NULL value instead of the default

Insert data in a column defines the default value, and requires no matter what the default value of the column is that you can set this value to NULL

create table t_user (user_id defalut 0,user_flag varchar(100))

Now insert a data

insert into t_user (user_id,user_flag) values (null,"今天心与心的交流了一下")

When the insertion of the statement, explicitly specify a null, but in actual development, not all people know that you can explicitly specify a null value in the insert statement, all the usual practice is to insert the data, you do not need to insert a column when the data does not specify a column as described above may be modified to insert null

insert into t_user (user_flag) values ("今天心与心的交流了一下")

4 from one table to another table rows replication

Copy data from one table to another table, the first step is to query data from the first table, the second step replication complex query data may also be simple in this process, but the end result is to copy the
example copy the data into a table in the table t_user table t_dep_user

insert into t_dep_user (dep_user_name,dep_user_flag)
select user_name,user_flag from t_user 

In the method used here is followed by a query after the insert statement, the query returns the row you want to get.

5 from a plurality of tables to another table rows replicated

Is different from the above, a query from the data table, then the data is copied to the table with a plurality, such as copying data in the table t_user t_user_2, t_user_3, t_user_4 in:

In MySQL, PostgreSQL and SQL Server does not support this multi-table insert operation

In Oracle can write

insert all 
    when user_age < 18 then 
        into t_user_2(user_name,user_age,user_flag) values (dep_user_name,dep_user_age,dep_user_flag)
    when user_age >60 then 
        into t_user_3(user_name,user_age,user_flag) values (dep_user_name,dep_user_age,dep_user_flag)
    else 
        into t_user_4(user_name,user_age,user_flag) values (dep_user_name,dep_user_age,dep_user_flag)
    select dep_user_name,dep_user_age,dep_user_flag from t_user 

In DB2, all goals can be coupled with a union all


///先创建表
create table t_user_2(user_name varchar(10),user_age integer check (user_age <18),user_flag varchar(44))
create table t_user_3(user_name varchar(10),user_age integer check (user_age >=18 and user_age <=60),user_flag varchar(44))
create table t_user_4(user_name varchar(10),user_age integer check (user_age >60),user_flag varchar(44))

///插入数据 

insert into (
 select * from t_user_2 union all
 select * from t_user_3 union all
 select * from t_user_4 
) select * from t_user 

6 Copy Table Structure

In practice, you want to create a new table, set the table with the same structure as an existing table, you can consider copying the table structure only
in DB2, using the create table command with a like clause

create table t_user_2 like t_user

In Oracle, MySQL and PostgreSQL, the use of a sub-query does not return any rows

create table t_user_2 as select * from t_user where 1=0

Here where 1 = 0 leads to the query does not return any rows, so that the final table is generated according to create column names in the select clause of the query empty table


Complete

发布了356 篇原创文章 · 获赞 182 · 访问量 46万+

Guess you like

Origin blog.csdn.net/zl18603543572/article/details/104450520