mysql 创建表 create table详解

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 说明:此文件包括了blog数据库中建立全部的表的Mysql语句.

说明:此文件包括了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详解,如需转载请自行联系原博主。

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
5月前
|
SQL 关系型数据库 MySQL
Mysql基础第二十四天,创建表和操纵表
Mysql基础第二十四天,创建表和操纵表
51 0
Mysql基础第二十四天,创建表和操纵表
|
5月前
|
存储 关系型数据库 MySQL
在阿里云的AnalyticDB MySQL版中使用CREATE TABLE语句来创建内表
在阿里云的AnalyticDB MySQL版中使用CREATE TABLE语句来创建内表【1月更文挑战第16天】【1月更文挑战第78篇】
345 3
|
5月前
|
存储 关系型数据库 MySQL
MySQL技能完整学习列表5、数据库操作——1、创建数据库和表——2、修改表结构(ALTER TABLE)
MySQL技能完整学习列表5、数据库操作——1、创建数据库和表——2、修改表结构(ALTER TABLE)
219 0
|
11天前
|
SQL 关系型数据库 MySQL
MySQL 8.0报错--1118-Row size too large. The maximum row size for the used table type, not counting BLOBs,is 8126,
MySQL 8.0报错--1118-Row size too large. The maximum row size for the used table type, not counting BLOBs,is 8126,
MySQL 8.0报错--1118-Row size too large. The maximum row size for the used table type, not counting BLOBs,is 8126,
|
2月前
|
关系型数据库 MySQL Java
【Azure 应用服务】应用服务连接 Azure MySQL 一直失败,报错 Create connection error
【Azure 应用服务】应用服务连接 Azure MySQL 一直失败,报错 Create connection error
|
2月前
|
存储 关系型数据库 MySQL
在 MySQL 中使用创建表
【8月更文挑战第11天】
76 0
|
3月前
|
SQL 安全 关系型数据库
MySQL创建视图(CREATE VIEW)13
【7月更文挑战第13天】创建视图是指在已经存在的 MySQL 数据库表上建立视图。视图可以建立在一张表中,也可以建立在多张表中。
52 1
|
3月前
|
关系型数据库 MySQL 数据库
Mysqlbug-Could not create or access the registry key needed for the MySQL applicationto, TIMESTAMP w
Mysqlbug-Could not create or access the registry key needed for the MySQL applicationto, TIMESTAMP w
|
4月前
|
关系型数据库 MySQL 数据库
【MySQL】MySQL数据库的delete from table和truncate table之间的区别
【MySQL】MySQL数据库的delete from table和truncate table之间的区别
301 1
|
5月前
|
关系型数据库 MySQL
MySQL创建表出现 Specified key was too long; max key length is 767 bytes
MySQL创建表出现 Specified key was too long; max key length is 767 bytes
58 2
下一篇
无影云桌面