元组可以直接添加进数据库吗,元组没有顺序插入数据库表中吗?

I am trying to insert 10 values of the format "typename_" + i where i is the counter of the loop in a table named roomtype with attributes typename (primary key of SQL type character varying (45)) and samplephoto (it can be NULL and I am not dealing with this for now). What seems strange to me is that the tuples are inserted in different order than the loop counter increments. That is:

typename_1

typename_10

typename_2

typename_3

...

I suppose it's not very important but I can't understand why this is happening. I am using PostgreSQL 9.3.4, pgAdmin III version 1.18.1 and Eclipse Kepler.

The Java code that creates the connection (using JDBC driver) and makes the query is:

import java.sql.*;

import java.util.Random;

public class DBC{

Connection _conn;

public DBC() throws Exception{

try{

Class.forName("org.postgresql.Driver");

}catch(java.lang.ClassNotFoundException e){

java.lang.System.err.print("ClassNotFoundException: Postgres Server JDBC");

java.lang.System.err.println(e.getMessage());

throw new Exception("No JDBC Driver found in Server");

}

try{

_conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/hotelreservation","user", "0000");

ZipfGenerator p = new ZipfGenerator(new Random(System.currentTimeMillis()));

_conn.setCatalog("jdbcTest");

Statement statement = _conn.createStatement();

String query;

for(int i = 1; i <= 10; i++){

String roomtype_typename = "typename_" + i;

query = "INSERT INTO roomtype VALUES ('" + roomtype_typename + "','" + "NULL" +"')";

System.out.println(i);

statement.execute(query);

}

}catch(SQLException E){

java.lang.System.out.println("SQLException: " + E.getMessage());

java.lang.System.out.println("SQLState: " + E.getSQLState());

java.lang.System.out.println("VendorError: " + E.getErrorCode());

throw E;

}

}

}

But what I get in pgAdmin table is:

f56a263a13ac316d17d881a56201a33f.png

解决方案

This is a misunderstanding. There is no "natural" order in a relational database table. While rows are normally inserted in sequence to the physical file holding a table, a wide range of activities can reshuffle physical order. And queries doing anything more than a basic (non-parallelized) sequential scan may return rows in any opportune order. That's according to standard SQL.

The order you see is arbitrary unless you add ORDER BY to the query.

pgAdmin3 by default orders rows by the primary key (unless specified otherwise). Your column is of type varchar and rows are ordered alphabetically (according to your current locale). All by design, all as it should be.

To sort rows like you seem to be expecting, you could pad some '0' in your text:

...

typename_0009

typename_0010

...

The proper solution would be to have a numeric column with just the number, though.

You may be interested in natural-sort. You may also be interested in a serial column.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值