mysql 创建表 create table详解

摘要:
123456789101112131415161718192021222324252627282930313233343536338394404142434445464749505152535455575855960616263646566676870717273747577798081828283848586878899091929394
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
说明:此文件包括了blog数据库中建立全部的表的Mysql语句.
 
在sql语句中注意“约束的概念":
1.实体完整性约束(主键--唯一且非空) primary key()
    违约处理:No action(拒绝运行)
 
2.參照完整性约束(外键约束)foregin key() references tableName(filedName) [on delete|update casecade | no action]
  违约处理:级联更新或拒绝运行
 
3.用户自己定义完整性约束(not null,unique,check短语)
      违约处理:拒绝运行
 
//增加列语法
//【alter table blog_article add columName type constraint
//增加约束样例
//【alter table blog_article add CONSTRAINT foreign key(category_Name) references blog_category(category_Name) on delete cascade on update cascade
 
 
问题:怎样让相冊,相片,文章公用一个评论表?
 
create database blog;
 
create table blog_user
(
  user_Name char(15) not null check(user_Name !=''),
  user_Password char(15) not null,
  user_emial varchar(20) not null unique,
  primary key(user_Name)         
 
)engine=innodb default charset=utf8 auto_increment=1;
 
 
 
 
create table blog_category
(
 category_Name char(18) not null check(category_Name!=''),
 category_Date datetime not null,
 primary key(category_Name)
)engine=innod default charset=utf8 auto_increment=1;
 
 
 
 
create table blog_article
(
  article_Id int unsigned not null  auto_increment,
  article_title varchar(20) not null unique,
  article_content longtext not null,
  article_date datetime not null,
  article_readTime int unsigned not null default 0,
  user_Name char(15) not null,
  category_Name char(18) not null,
  primary key(article_Id),
  foreign key(user_Name) references blog_user(user_Name) on delete cascade on update cascade,
  foreign key(category_Name) references blog_category(category_Name) on delete cascade on update cascade
)engine=innodb default charset=utf8 auto_increment=1;
 
 
 
 
 
 
CREATE TABLE blog_comment (
  comment_Id int(10) unsigned NOT NULL AUTO_INCREMENT,
  comment_Content varchar(90) NOT NULL,
  comment_Date datetime NOT NULL,
  article_Id int(10) unsigned NOT NULL,
  user_Name char(15) NOT NULL,
  PRIMARY KEY (comment_Id),
  foreign key(article_Id) references blog_article(article_Id) on delete cascade on update cascade,
  foreign key(user_Name) references blog_user(user_Name) on delete cascade on update cascade
)engine=innodb default charset=utf8 auto_increment=1;
 
 
 
create table blog_photoAlbum
(
  photoAlbum_Name char(20) not null check(photoAlbum_Name!=''),
  photoAlbum_Date datetime not null,
  primary key(photoAlbum_Name)
)engine=innodb default charset=utf8;
 
 
 
 
create table blog_photograph
(
  photograph_Name varchar(20) not null check(photograph_Name!=''),
  photograph_Date datetime not null,
  photoAlbum_Name char(20)  not null,
  photoURL varchar(90) not null,
  foreign key(photoAlbum_Name) references blog_photoAlbum(photoAlbum_Name) on delete cascade on update cascade
)engine=innodb default charset=utf8;

 

免责声明:文章转载自《mysql 创建表 create table详解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇PHP类和对象函数实例详解ps教程分享:一定要记住这20种PS技术!下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

规则引擎.Net Core

.Net Core 环境下构建强大且易用的规则引擎 https://www.cnblogs.com/chenug/p/9160397.html 本文源码: https://github.com/jonechenug/ZHS.Nrules.Sample 引言 1.1 为什么需要规则引擎 在业务的早期时代,也许使用硬编码或者逻辑判断就可以满足要求。但随着业务的...

使用Cucumber+Rspec玩转BDD(7)——测试重构

使用Cucumber+Rspec玩转BDD(7)——测试重构 2009年4月5日 星期日 ### 温故知新 ###在前面的六个章节中,我们循序渐进地完善了一个用户帐号系统,这样的系统一般都会作为一个独立的模块交付。在交付这个模块之前,还需要进一步地做些重构工作。在这篇文章中,笔者将会围绕测试重构展开。源码下载:http://github.com/4...

数据库MySQL/Postgres/Redis异步操作

数据库异步操作 基于 aiomysql 异步操作mysql数据库     异步操作 MySQL 的话,需要使用一个 aiomysql,直接 pip install aiomysql 入门案例 # -*- coding: utf-8 -*- # 导入异步操作的工具类库 import asyncio import aiomysql.sa as aio_sa...

Centos8 腾讯云 安装 certbot Nginx 通过 certbot 为网站自动配置 SSL 证书并续期

centos8 https://certbot.eff.org/lets-encrypt/centosrhel8-nginx.html centos 7 https://certbot.eff.org/lets-encrypt/centosrhel7-nginx.html https://blog.51cto.com/wzlinux/2385116 网上很...

python用户管理系统

学Python这么久了,第一次写一个这么多的代码(300多行,重复的代码挺多的,比较水),但是也挺不容易的 自定义函数+装饰器,每一个模块写的一个函数 很多地方能用装饰器(逻辑跟不上,有的地方没用),包括双层装饰器(不会),很多地方需要优化,重复代码太多 我还是把我的流程图拿出来吧,虽然看着比上次的垃圾,但是我也做了一个小时,不容易! 好像是挺丑的(表示...

MySQL笔记——用户管理

MySQL服务的默认端口是3306 用户权限管理 关于mysql数据库中的user表:   user表是MySQL最重要的权限表之一,在用户登录就是匹配user表中的Host、User、Password这三个字段,当三个字段同时匹配时才能允许登陆。   user表中以priv结尾的字段就是决定了用户的权限,这些字段默认都是N。 关于mysql数据库中的u...