MYSQL中,请问判断数据库是否存在这么做?判断数据表是否存在这么做?判断表中的列是否存在这么做?
如果不存在就创建一个。
请各位指教,能否写详细点,谢谢
如果不存在就创建一个。
请各位指教,能否写详细点,谢谢
解决方案 »
- 关于dc_mysql里的myi和myd文件
- mysql5.*,为什么这么写有语法错误了?
- 求教!!id int primary key auto_increment是什么意思?
- mysql 存储过程请教
- 请问在VC中利用libmysql.dll提供的API,如何存储blob类型的数据?
- 关于mysql数据库 中文乱码的问题
- 在一个表查询一个含有特定字符串的字段的sql语句
- 应该是挺基础的2个问题,在线等,给分100,谢谢!
- 数据库中有id字段,怎样实现每添加一条记录id自动加1
- mysql-error :1016
- Lock wait timeout exceeded; try restarting transaction
- 求sql语句将一个字段。按指定字符分成几个字段
如果是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是否不存在,不存在则创建
只不过在一句中实现