MYSQL判断database、table是否存在

来源:互联网 发布:下载u盘数据恢复软件 编辑:程序博客网 时间:2024/05/12 17:55

-- 判断是否存在SAMPLEDB,若存在则DROP,否则CREATE
drop database if exists SAMPLEDB;
create database SAMPLEDB;
use SAMPLEDB;
-- 判断是否存在表CUSTOMERS、ORDERS,若存在则DROP,否则CREATE
drop table if exists CUSTOMERS;
drop table if exists ORDERS;

create table CUSTOMERS (
 ID bigint not null,
 NAME varchar(15),
 primary key(ID)
);
create table ORDERS (
 ID bigint not null,
 ORDER_NUMBER varchar(15),
 PRICE double precision,
 CUSTOMER_ID bigint,
 primary key (ID)
);
-- 给ORDERS表添加索引
alter table ORDERS add index IDX_CUSTOMER(CUSTOMER_ID), 
-- 给ORDERS表添加foreigh key约束
add constraint FK_CUSTOMER foreign key (CUSTOMER_ID) references CUSTOMERS (ID);

0 0
原创粉丝点击