MySQL是否支持表继承? [英] Does MySQL support table inheritance?

查看:1380
本文介绍了MySQL是否支持表继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PostgreSQL中有这个代码

I have this code in PostgreSQL

CREATE TABLE first (
  id serial,
  primary key(id)
);

CREATE TABLE second (primary key(id)) INHERITS (first);

MySQL的等效代码是什么?

What is the equivalent code for MySQL?

推荐答案

MySQL不支持表继承。逼近功能的唯一方法是使用外键(MySQL也不擅长):

MySQL does not support table inheritance. The only way to approximate the functionality is by using a foreign key (which MySQL isn't too good at either):

CREATE TABLE first (
  id serial,
  PRIMARY KEY (id)
);

CREATE TABLE second (
  parent integer REFERENCES first,
  PRIMARY KEY (parent)
);

显然,您必须将PostgreSQL继承版本中的任何视图和查询更改为常规多关系查询。

Obviously, you'd have to change any views and queries from the PostgreSQL "inheritance version" to regular multi-relation queries.

这篇关于MySQL是否支持表继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆