如果表存在drop table然后创建它,如果它不存在则只创建它

时间:2013-11-22 22:52:32

标签: mysql

我很难过,我不知道该怎么做。

基本上我只想创建一个表,但是如果它存在则需要删除并重新创建,而不是截断,但如果它不存在则只需创建它。

有人能帮忙吗?

谢谢, 乔治

4 个答案:

答案 0 :(得分:250)

只需将DROP TABLE IF EXISTS `tablename`;放在CREATE TABLE声明之前。

如果该表存在,则该语句将删除该表,但如果不存在则不会抛出错误。

答案 1 :(得分:38)

只需使用DROP TABLE IF EXISTS

DROP TABLE IF EXISTS `foo`;
CREATE TABLE `foo` ( ... );

如果您有任何其他问题,请先尝试搜索MySQL documentation

答案 2 :(得分:4)

嗯...嗯。多年来,没有人提到一件微妙的事情。

尽管DROP TABLE IF EXISTS `bla`; CREATE TABLE `bla` ( ... );似乎是合理的,但是这会导致旧表已经消失而新表还没有创建的情况:此时某些客户端可能会尝试访问主题表。

更好的方法是创建一个全新的表并将其与旧表交换(表内容丢失):

CREATE TABLE `bla__new` (id int); /* if not ok: terminate, report error */
RENAME TABLE `bla__new` to `bla`; /* if ok: terminate, report success */
RENAME TABLE `bla` to `bla__old`, `bla__new` to `bla`;
DROP TABLE IF EXISTS `bla__old`;
  • 您应检查CREATE ...的结果,并且不要继续 ,因为失败意味着其他线程未完成 相同的脚本:要么是因为它在中间崩溃了,要么就是 还没有完成-亲自检查一下是个好主意。
  • 然后,您应该检查前RENAME ...的结果,而不要检查 继续成功:整个操作成功 完成甚至更多,下一个RENAME ...可以(并将)运行 如果另一个线程已经开始了相同的顺序(这是 最好不包括这种情况,否则请参见下面的锁定说明。
  • 第二RENAME ...原子地替换表定义,请参阅 MySQL manual 有关详细信息。
  • 最后,DROP ...只是清理了旧表, 显然。

用类似SELECT GET_LOCK('__upgrade', -1); ... DO RELEASE_LOCK('__upgrade');的形式包装所有语句,可以按顺序调用所有语句而无需进行错误检查,但是我认为这不是一个好主意:复杂性增加,MySQL中的锁定功能对于语句来说并不安全-基于复制。

如果表数据应在表定义升级后仍然存在...对于一般情况,有关比较表定义以找出差异并产生正确的ALTER ...语句的故事要复杂得多,例如,自动更新并非总是可能的。重命名列时。

注释1: 您可以使用相同的方法处理视图,在这种情况下,CREATE/DROP TABLE仅转换为CREATE/DROP VIEW,而RENAME TABLE保持不变。实际上,您甚至可以将表格变成视图,反之亦然。

CREATE VIEW `foo__new` as ...; /* if not ok: terminate, report error */
RENAME TABLE `foo__new` to `foo`; /* if ok: terminate, report success */
RENAME TABLE `foo` to `foo__old`, `foo__new` to `foo`;
DROP VIEW IF EXISTS `foo__old`;

注释2: MariaDB用户应该对CREATE OR REPLACE TABLE/VIEW感到满意,apply plugin: 'com.android.application' configurations { cleanedAnnotations compile.exclude group: 'org.jetbrains' , module:'annotations' } android { lintOptions { checkReleaseBuilds false // Or, if you prefer, you can continue to check for errors in release builds, // but continue the build even when errors are found: abortOnError false } compileSdkVersion 28 buildToolsVersion '28.0.3' defaultConfig { applicationId 'ch.workouttracker' minSdkVersion 24 targetSdkVersion 28 versionCode 1 versionName "0.5" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" resConfigs "de" // And any other languages you support } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } packagingOptions{ exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE-FIREBASE.txt' exclude 'META-INF/NOTICE' exclude 'META-INF/DEPENDENCIES' } defaultConfig { vectorDrawables.useSupportLibrary = true } productFlavors { } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } allprojects { repositories { maven { url = "https://maven.fabric.io/public" } } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') //Firebase implementation 'com.google.firebase:firebase-core:16.0.6' implementation 'com.google.firebase:firebase-messaging:17.3.4' implementation 'com.google.firebase:firebase-auth:16.1.0' implementation 'com.google.firebase:firebase-database:16.0.5' implementation 'com.google.firebase:firebase-storage:16.0.5' //Graphview implementation 'com.jjoe64:graphview:4.2.1' implementation 'org.apache.commons:commons-lang3:3.4' //Google Play implementation 'com.google.android.gms:play-services-location:16.0.0' implementation 'com.google.android.gms:play-services-maps:16.0.0' implementation 'com.google.android.gms:play-services-auth:16.0.1' implementation 'com.google.android.gms:play-services-identity:16.0.0' //FirebaseUI implementation 'com.firebaseui:firebase-ui-auth:4.1.0' implementation 'com.firebaseui:firebase-ui-storage:1.2.0' implementation 'com.facebook.android:facebook-android-sdk:4.27.0' implementation 'com.twitter.sdk.android:twitter-core:3.1.1' //Support implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:cardview-v7:27.1.1' implementation 'com.android.support:design:27.1.1' implementation 'com.android.support:support-core-utils:27.1.1' implementation 'com.android.support:support-media-compat:27.1.1 ' implementation 'com.android.support:design:27.1.1' implementation 'com.android.support:cardview-v7:27.1.1' implementation 'com.android.support:gridlayout-v7:27.1.1' implementation 'com.android.support:recyclerview-v7:27.1.1' implementation 'com.android.support:preference-v7:27.1.1' //Firebase implementation 'com.crashlytics.sdk.android:crashlytics:2.9.7' implementation 'com.google.firebase:firebase-plugins:1.1.5' } apply plugin: 'com.google.gms.google-services' com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true已经在乎主题问题了,这很好。

答案 3 :(得分:1)

我需要删除一个表并使用视图中的数据重新创建。 我是根据视图创建表的,这就是我所做的:

DROP TABLE <table_name>;
CREATE TABLE <table_name> AS SELECT * FROM <view>;

以上使用MySQL MariaDb为我工作。

相关问题