简体   繁体   English

SQL 两个表的服务器外键分区

[英]SQL Server foreign key partitions for two tables

I'm new to databases and I don't understand how partitions work.我是数据库新手,我不明白分区是如何工作的。 I tried to understand some examples but I'm still lost.我试图理解一些例子,但我还是迷路了。 I just need to make a partition of table Persona basing me of idUbicacion , for each different foreign key I need the partition.我只需要根据idUbicacion对表Persona进行分区,对于每个不同的外键我都需要分区。 I'm thank for you help.我很感谢你的帮助。 :) :)

CREATE TABLE Ubicacion
(
    numero int identity(1,1) primary key NOT NULL,
    pais varchar(200) NOT NULL
    provincia varchar(200) NOT NULL,
);

CREATE TABLE Persona
(
    id int identity(1,1) primary key NOT NULL,
    nombre varchar(500),
    cedula int,
    nacionalidad varchar(200),
    idUbicacion int foreign key references Ubicacion(id) NOT NULL
);

If you are partitioning a table then you need to define a partitioning key which needs to be included in the primary key.如果要对表进行分区,则需要定义需要包含在主键中的分区键。 For example (this is not the right syntax just a pseudo code):例如(这不是正确的语法,只是一个伪代码):

CREATE TABLE Ubicacion(
    numero int identity(1,1) NOT NULL,
    pais varchar(200) NOT NULL,
    provincia varchar(200) NOT NULL,
    primary key (numero, provincia)
) PARTITION BY provincia;

Then you need to add this extra field to all your foreign keys if possible:然后,如果可能,您需要将此额外字段添加到所有外键中:

CREATE TABLE Persona(
    id int identity(1,1) primary key NOT NULL,
    nombre varchar(500),
    cedula int,
    nacionalidad varchar(200),
    provincia varchar(200) NOT NULL,
    idUbicacion int NOT NULL,
    foreign key (idUbicacion, provincia) references Ubicacion(id, provincia) 
);

Not easy right?不容易吧? So I have 2 recommendation for you:所以我有2个建议给你:

  1. Don't partition a primary table (dimension table) only a fact table.不要仅对事实表分区主表(维度表)。
  2. If you use partitioning then don't use foreign keys.如果您使用分区,则不要使用外键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM