mysql: create database

/***************************************
 *        DDL: Create Database         *
 ***************************************/

-- drop existing database if exists;
DROP DATABASE IF EXISTS fitbit_new;

-- create database;
CREATE DATABASE fitbit_new;

-- use database;
USE fitbit_new;
show tables;

/***************************************
 *           DDL: Create Table         *
 ***************************************/

/*        product          */
-- create table product;
DROP TABLE IF EXISTS product, client, sales;

describe sales;
-- alter table;

/*       client      */
-- create table client;

describe client;

/*        sales         */
-- create table sales;

-- create foreign key seperately;
alter table sales add
constraint fk_client_id_wanmen
	foreign key (client_id)
    references client (client_id)
    on delete cascade;

-- drop foreign key;
alter table sales drop
foreign key fk_client_id_wanmen;

-- add update cascade;
alter table sales drop
foreign key fk_client_id;
alter table sales drop
foreign key fk_product_id;
alter table sales add
constraint fk_product_id
	foreign key (product_id)
    references product (product_id)
    on delete cascade
    on update cascade;
alter table sales add
constraint fk_client_id
	foreign key (client_id)
    references client (client_id)
    on delete cascade
    on update cascade;

-- create view;

/**********************************************
 *        DML: Insert, Update, Delete         *
 **********************************************/

INSERT INTO product
VALUES
(1,‘E-ZIP‘,‘Zip‘,‘EVERYDAY‘,‘GREEN‘,‘Y‘,59.95),
(2,‘E-FLX‘,‘Flex‘,‘EVERYDAY‘,‘BLACK‘,‘N‘,99.95),
(3,‘A-BLZ‘,‘Blaze‘,‘ACTIVE‘,‘PURPLE‘,‘Y‘,199.95),
(4,‘P-SUG‘,‘Surge‘,‘PERFORMANCE‘,‘BLACK‘,‘Y‘,249.95);
select * from product;

-- update product;
update product
set product_id = 200
where product_id = 2;

-- cascade delete
delete from product
where product_id = 200;

INSERT INTO sales
(tran_id, date, product_id, client_id, price, quantity)
VALUES
(1,‘2016-6-1‘, 1, 1, 40, 10),
(2,‘2016-6-5‘, 1, 2, 30, 5),
(3,‘2016-6-8‘, 2, 1, 80, 8),
(4,‘2016-6-8‘, 2, 2, 70, 7),
(5,‘2016-6-13‘, 3, 2, 150, 5),
(6,‘2016-6-18‘, 3, 4, 150, 10),
(7,‘2016-6-20‘, 2, 4, 40, 15);
select * from sales;
-- update sales;
update sales
set date = ‘2016-06-21‘,price = 200
where date = ‘2016-06-20‘ and tran_id = 7;

INSERT INTO client
VALUES
(1,‘FITBIT‘,‘ONLINE‘),
(2,‘AMAZON‘,‘ONLINE‘),
(3,‘BESTBUY‘,‘OFFLINE‘),
(4,‘WALMART‘,‘OFFLINE‘)
;
select * from client;
-- create table SHIPPING;
DROP TABLE IF EXISTS shipping;

create table if not exists shipping(
	shipping_id int not null,
    tran_id int not null,
    tracking_no int,
    status enum(‘preparing‘, ‘shipped‘, ‘arrived‘),
    arrive_date date,
    eta	date,
    primary key (tran_id),
    constraint fk_tran_id
	foreign key (tran_id)
    references sales (tran_id)
    on delete cascade
    on update cascade
);

select * from shipping;

  

/***************************************  *        DDL: Create Database         *  ***************************************/
-- drop existing database if exists;DROP DATABASE IF EXISTS fitbit_new;
-- create database;CREATE DATABASE fitbit_new;
-- use database;USE fitbit_new;show tables;
/***************************************  *           DDL: Create Table         *  ***************************************/
/*        product          */-- create table product;DROP TABLE IF EXISTS product, client, sales;

describe sales;-- alter table;

/*       client      */-- create table client;

describe client;

/*        sales         */-- create table sales;

-- create foreign key seperately;alter table sales addconstraint fk_client_id_wanmenforeign key (client_id)    references client (client_id)    on delete cascade;
-- drop foreign key;alter table sales dropforeign key fk_client_id_wanmen;
-- add update cascade;alter table sales dropforeign key fk_client_id;alter table sales dropforeign key fk_product_id;alter table sales addconstraint fk_product_idforeign key (product_id)    references product (product_id)    on delete cascade    on update cascade;alter table sales addconstraint fk_client_idforeign key (client_id)    references client (client_id)    on delete cascade    on update cascade;
-- create view;

/**********************************************  *        DML: Insert, Update, Delete         *  **********************************************/
INSERT INTO product VALUES(1,‘E-ZIP‘,‘Zip‘,‘EVERYDAY‘,‘GREEN‘,‘Y‘,59.95),(2,‘E-FLX‘,‘Flex‘,‘EVERYDAY‘,‘BLACK‘,‘N‘,99.95),(3,‘A-BLZ‘,‘Blaze‘,‘ACTIVE‘,‘PURPLE‘,‘Y‘,199.95),(4,‘P-SUG‘,‘Surge‘,‘PERFORMANCE‘,‘BLACK‘,‘Y‘,249.95);select * from product;
-- update product;update productset product_id = 200where product_id = 2;
-- cascade deletedelete from productwhere product_id = 200;

INSERT INTO sales (tran_id, date, product_id, client_id, price, quantity)VALUES(1,‘2016-6-1‘, 1, 1, 40, 10),(2,‘2016-6-5‘, 1, 2, 30, 5),(3,‘2016-6-8‘, 2, 1, 80, 8),(4,‘2016-6-8‘, 2, 2, 70, 7),(5,‘2016-6-13‘, 3, 2, 150, 5),(6,‘2016-6-18‘, 3, 4, 150, 10),(7,‘2016-6-20‘, 2, 4, 40, 15); select * from sales;-- update sales;update salesset date = ‘2016-06-21‘,price = 200where date = ‘2016-06-20‘ and tran_id = 7;

INSERT INTO client VALUES(1,‘FITBIT‘,‘ONLINE‘),(2,‘AMAZON‘,‘ONLINE‘),(3,‘BESTBUY‘,‘OFFLINE‘),(4,‘WALMART‘,‘OFFLINE‘);select * from client;-- create table SHIPPING;DROP TABLE IF EXISTS shipping;
create table if not exists shipping(shipping_id int not null,    tran_id int not null,    tracking_no int,    status enum(‘preparing‘, ‘shipped‘, ‘arrived‘),    arrive_date date,    etadate,    primary key (tran_id),    constraint fk_tran_idforeign key (tran_id)    references sales (tran_id)    on delete cascade    on update cascade);
select * from shipping;

原文地址:https://www.cnblogs.com/tabCtrlShift/p/9234558.html

时间: 2024-08-01 05:49:48

mysql: create database的相关文章

MYSQL create database 和 create table 做了一些什么!

create database Studio; 这样就可以创建一个数据库了.他包涵一些什么呢? 可以看到它创建了一个文件夹,下面我们进去看一下它里面有一些什么东西. 还是先建一张表再进去吧,运行一下这个  create table Nums(X int not null); 内容分析: db.opt 文件. 复制到另一个地方,用计事本打开. 发现它保存的是字符集与排序规则信息. .frm 文件. 它保存整个表框架的定义,但是它保存的是密文. .idb文件. 它用来保存表中的数据.

mysql create database 指定utf-8编码

如下脚本创建数据库yourdbname,并制定默认的字符集是utf8CREATE DATABASE IF NOT EXISTS yourdbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;如果要创建默认gbk字符集的数据库可以用下面的sql:create database yourdb DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; 版权声明:本文为博主原创文章,未经博主允许不得转载.

MySQL Drop Database之后的恢复测试

工具介绍 工具的名字叫做:  undrop-for-innodb 代码地址: https://github.com/twindb/undrop-for-innodb 官方文档: https://recovery.twindb.com/ 介绍工具的一篇ppt  http://www.slideshare.net/akuzminsky/undrop-for-innodb 当MySQL 执行 Drop Table或者 Drop Database 的时候,InnoDB并没有删除数据,数据的Page还在磁

mysql5.7基础 create database if not exists... 判断数据库是否存在 若不存在则创建

礼悟:    公恒学思合行悟,尊师重道存感恩.叶见寻根三返一,江河湖海同一体.          虚怀若谷良心主,愿行无悔给最苦.读书锻炼养身心,诚劝且行且珍惜. 数据.数据,命根就在数据.操作数据库一定要谨慎小心.给最苦 这里的代码,看看就好,要有自己的判断.遇到抉择,要不耻上下问. mysql:5.7                     os:Windows7 x64 代码及效果 mysql> show databases; +--------------------+ | Databa

mysql5.7基础 create database...charset=... 创建一个新的数据库并指定它的字符编码

礼悟:    公恒学思合行悟,尊师重道存感恩.叶见寻根三返一,江河湖海同一体.          虚怀若谷良心主,愿行无悔给最苦.读书锻炼养身心,诚劝且行且珍惜. 数据.数据,命根就在数据.操作数据库一定要谨慎小心.给最苦 这里的代码,看看就好,要有自己的判断.遇到抉择,要不耻上下问. mysql:5.7                     os:Windows7 x64 代码及效果 mysql> create database newDB charset=utf8; Query OK, 1

基于Apache+php+mysql的许愿墙网站的搭建create database xyq; //创建xyq数据库

1.准备CentOS7与CentOS5的基础配置 2.在两台虚拟机中配置yum. 3.在CentOS7中安装httpd与php,在另一台虚拟机中安装mysql yum install mysql-server -y service mysqld restart  重启mysql 重启成功 4.将许愿墙网页放入CentOS7的/var/www/html 5.输入   mysql     进入数据库进行管理 6.输入create user 'kbz'@'192.168.100.104' identi

mysql之CREATE DATABASE Syntax(创建数据库)

一:语法 CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name #SCHEMA是DATABASE的同义词 [IF NOT EXITTS]可防止建库是已经存在报错 [create_specification] ... #可指定数据库的特征 create_specification: [DEFAULT] CHARACTER SET [=] charset_name #指定字符集 | [DEFAULT] COLLATE [=] collation_nam

MySQL rename database如何做?

虽然MySQL里面有rename database的语法,但是只是在5.1.7 to 5.1.23提供的,其他版本并没有,要想做rename操作该如何做呢?percona提供了一个shell #!/bin/bash # Copyright 2013 Percona LLC and/or its affiliates set -e if [ -z "$3" ]; then echo "rename_db <server> <database> <n

sql--select into,create database,create table,Constraints

SQL SELECT INTO 语句可用于创建表的备份复件.SELECT INTO 语句SELECT INTO 语句从一个表中选取数据,然后把数据插入另一个表中.SELECT INTO 语句常用于创建表的备份复件或者用于对记录进行存档. SQL SELECT INTO 语法 您可以把所有的列插入新表:SELECT *INTO new_table_name [IN externaldatabase] FROM old_tablename或者只把希望的列插入新表:SELECT column_name