[英]Postgresql: how to create table only if it does not already exist?
在Postgresql中,如果表格尚不存在,我怎么能创建一个条件呢?
代码示例赞赏。
我不确定它何时被添加,但为了完整起见,我想指出在版本9.1(可能之前)中, IF NOT EXISTS
。 IF NOT EXISTS
只会创建表,如果它还不存在。
例:
CREATE TABLE IF NOT EXISTS users.vip
(
id integer
)
如果表不存在,这将在模式users
创建名为vip
的表。
create or replace function update_the_db() returns void as
$$
begin
if not exists(select * from information_schema.tables
where
table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
and table_name = 'your_table_name_here') then
create table your_table_name_here
(
the_id int not null,
name text
);
end if;
end;
$$
language 'plpgsql';
select update_the_db();
drop function update_the_db();
只需创建表,不要担心它是否存在。 如果它不存在,它将被创建; 如果确实存在,则不会修改表。 您始终可以检查SQL查询的返回值,以查看执行create语句时表是否存在。
我想检查一下pg_class表可能对你有帮助,比如说:
SELECT COUNT (relname) as a FROM pg_class WHERE relname = 'mytable'
if a = 0 then (CREATE IT)
问候。
这是一个老问题。 我只是带回来提出另一个答案。 注意: 其他更好的答案已经存在,这仅用于教育目的 。
最简单的方法是做别人说的话; 如果要保留现有数据,请执行CREATE TABLE;如果需要新创建的表,则执行DROP IF EXISTS,然后执行CREATE TABLE。
另一种方法是查询系统表的存在并从那里继续。
SELECT true FROM pg_tables WHERE tablename = <table> [AND schemaname = <schema>];
正在使用:
-- schema independent:
SELECT true FROM pg_tables WHERE tablename = 'foo';
-- schema dependent:
SELECT true FROM pg_tables WHERE tablename = 'foo' AND schemaname = 'bar';
如果它匹配,你将有一个真值,否则它应该返回一个空数据集。 您可以使用该值来确定是否需要执行CREATE TABLE。
如果您正在运行Postgresql 9.1+,Skalli会给出最佳答案。
如果像我一样你需要使用Postgresql 8.4,你可以使用带有'duplicate_table'异常catch的函数。
当表存在时,这将忽略生成的错误并继续生成其他错误。
以下是Postgresql 8.4.10的一个示例:
CREATE FUNCTION create_table() RETURNS VOID AS
$$
BEGIN
CREATE TABLE my_table_name(my_column INT);
EXCEPTION WHEN duplicate_table THEN
-- Do nothing
END;
$$
LANGUAGE plpgsql;
我在创建表之前用来检查表是否存在(Java和PostgreSQL)。 我希望这可以帮助别人。 此处未实现create table部分,只是检查表是否已存在。 传递与数据库和tableName的连接,它应返回表是否存在。
public boolean SQLTableExists(Connection connection, String tableName) {
boolean exists = false;
try {
Statement stmt = connection.createStatement();
String sqlText = "SELECT tables.table_name FROM information_schema.tables WHERE table_name = '" + tableName + "'";
ResultSet rs = stmt.executeQuery(sqlText);
if (rs != null) {
while (rs.next()) {
if (rs.getString(1).equalsIgnoreCase(tableName)) {
System.out.println("Table: " + tableName + " already exists!");
exists = true;
} else {
System.out.println("Table: " + tableName + " does not appear to exist.");
exists = false;
}
}
}
} catch (SQLException sqlex) {
sqlex.printStackTrace();
}
return exists;
}
最简单的答案是:
catch{
#create table here
}
如果不存在,则创建表,如果存在则生成错误。 并且错误被抓住了。
http://www.postgresql.org/docs/8.2/static/sql-droptable.html
DROP TABLE [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
尝试在表上运行查询。 如果它抛出异常,则捕获异常并创建一个新表。
try {
int a = db.queryForInt("SELECT COUNT(*) FROM USERS;");
}
catch (Exception e) {
System.out.print(e.toString());
db.update("CREATE TABLE USERS (" +
"id SERIAL," +
"PRIMARY KEY(id)," +
"name varchar(30) NOT NULL," +
"email varchar(30) NOT NULL," +
"username varchar(30) NOT NULL," +
"password varchar(30) NOT NULL" +
");");
}
return db;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.