6.1. Inserting Data
6.1.插入数据
When a table is created, it contains no data. The first thing to do before a database can be of much use is to insert data. Data is conceptually inserted one row at a time. Of course you can also insert more than one row, but there is no way to insert less than one row. Even if you know only some column values, a complete row must be created.
表创建的时候,没有数据。在使用数据库之前的第一件事,是插入数据。从概念上讲,数据一次只能插入一行。当然,您也可以插入多行,但是无法插入少于一行。 即使您只知道某几个列的值,但插入的时候也必须创建完整的行。
To create a new row, use the INSERT command. The command requires the table name and column values. For example, consider the products table from Chapter 5:
使用INSERT命令创建行。该命令需要有表名和列值。例如,假设表products如下:
CREATE TABLE products (
product_no integer,
name text,
price numeric
);
An example command to insert a row would be:
插入一行数据的示例如下:
INSERT INTO products VALUES (1, 'Cheese', 9.99);
The data values are listed in the order in which the columns appear in the table, separated by commas.Usually, the data values will be literals (constants), but scalar expressions are also allowed.
列值以逗号分隔,按照表列的顺序排列。通常,数据值应该是常量,但范围表达式也可以。
The above syntax has the drawback that you need to know the order of the columns in the table. To avoid this you can also list the columns explicitly. For example, both of the following commands have the same effect as the one above:
上例语法有个缺点,就是你必须知道表的列顺序。可以通过显式的指定表名来解决此问题。例如,以下两个命令均可以实现上例中的效果:
INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese',9.99);
INSERT INTO products (name, price, product_no) VALUES ('Cheese',9.99, 1);
Many users consider it good practice to always list the column names.
很多用户更倾向于在插入数据的时候列出表名。
If you don't have values for all the columns, you can omit some of them. In that case, the columns will be filled with their default values. For example:
如果你没有针对所有列的值,那么可以忽略这些列。这样,列会被其默认值填充,例如:
INSERT INTO products (product_no, name) VALUES (1, 'Cheese');
INSERT INTO products VALUES (1, 'Cheese');
The second form is a PostgreSQL extension. It fills the columns from the left with as many values as are given, and the rest will be defaulted.
第二个格式是PostgreSQL特有的。它会根据给定值的个数,从左填充到列中,剩余的列填充默认值。
For clarity, you can also request default values explicitly, for individual columns or for the entire row:
为了清楚一些,也可以显式的指定为某列或所有列插入默认值:
INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese',DEFAULT);
INSERT INTO products DEFAULT VALUES;
You can insert multiple rows in a single command:
也可以在一个命令中插入多行:
INSERT INTO products (product_no, name, price) VALUES
(1, 'Cheese', 9.99),
(2, 'Bread', 1.99),
(3, 'Milk', 2.99);
It is also possible to insert the result of a query (which might be no rows, one row, or many rows):
也可以将查询结果插入表中(可能没有数据,一行或多行):
INSERT INTO products (product_no, name, price)
SELECT product_no, name, price FROM new_products
WHERE release_date = 'today';
This provides the full power of the SQL query mechanism (Chapter 7) for computing the rows to be inserted.
这提供了SQL查询机制(第7章)的全部功能,用于计算要插入的行。
Tip
小贴士
When inserting a lot of data at the same time, consider using the COPY command. It is not as flexible as the INSERT command, but is more efficient. Refer to Section 14.4 for more information on improving bulk loading performance.
可以考虑使用COPY命令同时插入大量数据。虽然不如 INSERT命令灵活,但比其高效。关于如何提高大批量加载的性能的更多信息,请参见14.4节。

6.1. Inserting Data相关推荐

  1. Inserting Data Into Tables Using Direct-Path INSERT

    Oracle Database inserts data into a table in one of two ways:(向表中插入数据有两种方法) During conventional INSE ...

  2. A2-02-24.DML- Inserting Data into A Table Using MySQL INSERT Statement

    转载自:http://www.mysqltutorial.org/mysql-insert-statement.aspx Home / Basic MySQL Tutorial / Inserting ...

  3. mysql中不同事务隔离级别下数据的显示效果--转载

    事务是一组原子性的SQL查询语句,也可以被看做一个工作单元.如果数据库引擎能够成功地对数据库应用所有的查询语句,它就会执行所有查询,如果任何一条查询语句因为崩溃或其他原因而无法执行,那么所有的语句就都 ...

  4. Level/levelup-2-API

    https://github.com/Level/levelup Special Notes What happened to db.createWriteStream() levelup(db[, ...

  5. PostgreSQL分区表的执行计划

    开始 比如说我已经做好了对分区表的规则: postgres=# CREATE OR REPLACE FUNCTION ptest_insert_trigger() RETURNS TRIGGER AS ...

  6. cve-2016-6662 mysql远程代码执行/权限提升 漏洞

    I. VULNERABILITY MySQL <= 5.7.15 远程代码执行/权限提升 (0day) 5.6.33 5.5.52 克隆mysql的同样受影响, 包括: MariaDB Perc ...

  7. spring: 使用Spring提供的JDBC模板(使用profiles选择数据源/使用基于JDBC驱动的数据源)...

    Spring提供的JDBC框架负责管理资源和异常处理,从而可以简化开发者的JDBC代码.开发者只需要编写写入和读取数据库相关的代码即可. 正如在之前的小节中论述过的,Spring将数据库访问过程中的模 ...

  8. mysql如何配置hbm.xml_配置数据库映射文件hbm.xml

    在数据库映射方面,guzz支持类似hibernate的领域对象class-table映射.也支持ibatis类似的在guzz.xml中映射. hibernate的hbm.xml映射 guzz兼容hib ...

  9. mysql的基准测试_mysql基准测试 -benchmarks

    测试环境 基准测试结果 : [root@192-168-103-43 sql-bench]# ./test-insert --server=mysql --user=root --password=h ...

最新文章

  1. 源代码查看工具 Source Navigator 使用心得
  2. Unity5 AssetBundle系列——简单的AssetBundleManager
  3. Windows 10 Technical Preview 安装体验及变化
  4. iOS进阶之类别(Category)(9)
  5. mybatis添加记录时返回主键id
  6. tomcat 在eclipse中可以启动,独立不能启动
  7. ldap基本dn_LDAP 中 DN CN DC OU
  8. SpringBoot2.0 基础案例(08):集成Redis数据库,实现缓存管理
  9. GitHub还是GitLab?谈谈两者的区别
  10. 群同态基本定理证明_自由群的定义及相关
  11. (十二)洞悉linux下的Netfilteriptables:iptables命令行工具源码解析【下】
  12. 马拉车算法(Manacher's Algorithm)
  13. mysql修改数据库字段数值_mysql修改数据库字段的操作
  14. 洞见科技创始人姚明:隐私计算让数据穿上“防护服”的同时,还能充分释放数据价值...
  15. 什么叫做云计算?云计算基础学习路线
  16. win10打开计算机出现马赛克,图片有马赛克怎么去除?win10给图片去除马赛克的方法...
  17. 互联网协议 — 802.1q VLAN 虚拟局域网协议
  18. Microsoft Visual SourceSafe
  19. tl-wdr7300虚拟服务器怎么设置,TP-Link TL-WDR7300路由器wifi密码怎么设置?(电脑)...
  20. 亚马逊运营爆款产品技巧-跨境知道

热门文章

  1. 最近所学的Json以及ajax的应用
  2. python注释快捷键alt_python快捷键的使用【摘抄】
  3. 智能媒体管理产品文档转换/预览功能介绍(1)---Cloud Native架构
  4. casio计算用计算机面板,《CASIOfx—4850P计算器土木工程测量计算程序开发和应用》.pdf...
  5. mysql微擎用户名密码_微擎后台管理员密码忘记怎么办?教你一个简单的方法
  6. 帝国cms如何域名html的专题,帝国cms整站更换新老域名详细操作方法
  7. 启用微软e5子账户的outlook邮箱,解决 qyi 续订程序无法刷新令牌问题
  8. 五子棋游戏Java代码简单实现(含活动图和类图设计)
  9. 【课设分享】基于51单片机的恒温控制器系统
  10. Involution-Convolution