SELECT * FROM information_schema.SCHEMATA where SCHEMA_NAME='databasename';
库、表可以 create database if not exists test;create table if not exists test(id int,name varchar(20)...);列要从系统表 OR 在语言环境中取得
判断列: select * from information_schema.columns where table_schema='库名' and table_ name='表名' and column_name='列名';
select * from information_schema.`TABLES` where TABLE_SCHEMA='databasename' and TABLE_NAME='tablename';select * from information_schema.`COLUMNS` where TABLE_SCHEMA='databasename' and TABLE_NAME='tablename' and COLUMN_NAME='columnname'
这个倒是容易create database IF NOT EXISTS mydatabaseName;create table IF NOT EXISTS myTableName 对于列,则只能先判断再添加了。
假设你是5以上 库: SELECT * FROM information_schema.SCHEMATA where SCHEMA_NAME='tt'; 表: sELECT table_name, table_type, engine FROM information_schema.tables WHERE table_schema = 'zz' and table_name='tt' ORDER BY table_name DESC; 列: select * from information_schema.columns where table_schema='tt' and table_ name='newtt' and column_name='列名';
一般情况下,都是通过自己对系统表进行判断来确定,mysql下版本5以上也有对应系统表,如下:判断库: select * from information_schema.SCHEMATA where SCHEMA_NAME='库名' ;判断表: select * from information_schema.tables where table_schema='库名' and table_n ame='表名';判断列: select * from information_schema.columns where table_schema='库名' and table_ name='表名' and column_name='列名';------------------------ 当然,在mysql下,判断库或表也有其特殊的方法,如:判断库db_test是否不存在,不存在则创建: create database if not exists db_test;判断库aaa下表tb_test是否存在,不存在则创建: create table if not exists aaa.tb_test(id int),name varchar(20)...);
如你说的,在mssql下,对系统表可以直接用 if not exists (select 1 from sysobjects where xtype='T' and object_name='tb_name') create table tb_name ..... else print 'tb_name已存在'但在mysql下,你想通过系统表直接这样判断,目前是不支持的,只能: declare v_i int; select count(*) into v_i from information_schema.tables where table_schema='库名' and table_name='表名'; if v_i =0 then create table tb_name ..... else select '表已存在'; end if;但其实mysql下,有更简单的支持方法,就是: 判断库aaa下表tb_test是否存在,不存在则创建: create table if not exists aaa.tb_test(id int,name varchar(20)...);
上面提到的例子,即说明目前mysql下不支持类似 if not exists (select 1 from sysobjects where xtype='T' and object_name='tb_name') 这样的写法
谢谢各位 最后想问下一个逻辑问题 既然在系统表里面都判断好这个数据库、表、列是否存在了,为什么创建的时候又来个create database IF NOT EXISTS mydatabaseName;呢? 因为很明显,我的思路是先判断是否存在,不存在就创建。 我觉得是不是先在系统库里面判断好是否存在,之后直接create database XXX;???? 哪样的思路合理点呢?效率更高点? 我的MYSQL版本是5.1
" 当然,在mysql下,判断库或表也有其特殊的方法,如:判断库db_test是否不存在,不存在则创建: create database if not exists db_test;判断库aaa下表tb_test是否存在,不存在则创建: create table if not exists aaa.tb_test(id int),name varchar(20)...);" --------------- 不用你再判断,其会自动判断,最简单的你自己动手试一下就很明白了
create database IF NOT EXISTS mydatabaseName 其实这段已经做了: 判断库db_test是否不存在,不存在则创建 只不过在一句中实现
因为很多用户喜欢直接用 create database IF NOT EXISTS create database IF NOT EXISTS 只需要和数据库交互一次,你的应用程序只需要通过网络发一次指令。
如果是5以上,有系统表,也可以用ADO、ADOX取得表、列名
SELECT * FROM information_schema.SCHEMATA where SCHEMA_NAME='databasename';
create database if not exists test;create table if not exists test(id int,name varchar(20)...);列要从系统表 OR 在语言环境中取得
select * from information_schema.columns where table_schema='库名' and table_
name='表名' and column_name='列名';
where TABLE_SCHEMA='databasename' and TABLE_NAME='tablename';select * from information_schema.`COLUMNS`
where TABLE_SCHEMA='databasename'
and TABLE_NAME='tablename'
and COLUMN_NAME='columnname'
可是3楼哥们,你只说了判断数据库是否存在,那么数据库里面判断表是否存在,判断列是否存在这么做呢?
这个倒是容易create database IF NOT EXISTS mydatabaseName;create table IF NOT EXISTS myTableName
对于列,则只能先判断再添加了。
库:
SELECT * FROM information_schema.SCHEMATA where SCHEMA_NAME='tt';
表:
sELECT table_name, table_type, engine
FROM information_schema.tables
WHERE table_schema = 'zz' and table_name='tt'
ORDER BY table_name DESC;
列:
select * from information_schema.columns where table_schema='tt' and table_
name='newtt' and column_name='列名';
select * from information_schema.SCHEMATA where SCHEMA_NAME='库名' ;判断表:
select * from information_schema.tables where table_schema='库名' and table_n
ame='表名';判断列:
select * from information_schema.columns where table_schema='库名' and table_
name='表名' and column_name='列名';------------------------
当然,在mysql下,判断库或表也有其特殊的方法,如:判断库db_test是否不存在,不存在则创建:
create database if not exists db_test;判断库aaa下表tb_test是否存在,不存在则创建:
create table if not exists aaa.tb_test(id int),name varchar(20)...);
if not exists (select 1 from sysobjects where xtype='T' and object_name='tb_name')
create table tb_name .....
else
print 'tb_name已存在'但在mysql下,你想通过系统表直接这样判断,目前是不支持的,只能:
declare v_i int;
select count(*) into v_i from information_schema.tables where table_schema='库名' and table_name='表名';
if v_i =0 then
create table tb_name .....
else
select '表已存在';
end if;但其实mysql下,有更简单的支持方法,就是:
判断库aaa下表tb_test是否存在,不存在则创建:
create table if not exists aaa.tb_test(id int,name varchar(20)...);
if not exists (select 1 from sysobjects where xtype='T' and object_name='tb_name')
这样的写法
最后想问下一个逻辑问题
既然在系统表里面都判断好这个数据库、表、列是否存在了,为什么创建的时候又来个create database IF NOT EXISTS mydatabaseName;呢?
因为很明显,我的思路是先判断是否存在,不存在就创建。
我觉得是不是先在系统库里面判断好是否存在,之后直接create database XXX;????
哪样的思路合理点呢?效率更高点?
我的MYSQL版本是5.1
当然,在mysql下,判断库或表也有其特殊的方法,如:判断库db_test是否不存在,不存在则创建:
create database if not exists db_test;判断库aaa下表tb_test是否存在,不存在则创建:
create table if not exists aaa.tb_test(id int),name varchar(20)...);"
---------------
不用你再判断,其会自动判断,最简单的你自己动手试一下就很明白了
其实这段已经做了:
判断库db_test是否不存在,不存在则创建
只不过在一句中实现