首页 \ 问答 \ 使用外键在mysql中工作(Working in mysql with foreign keys)

使用外键在mysql中工作(Working in mysql with foreign keys)

我想用外键创建一个mysql数据库。 但是当我插入一些测试数据时,我注意到当我查看表tblcontact和tbladdress时,我的外键为null。 我知道这是一个基本问题,但你能提出一些建议吗?

CREATE TABLE tblCustomers (
customerID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
vat VARCHAR(30) NOT NULL,
customerVisible varchar(1) NOT NULL DEFAULT 'T'
); 


CREATE TABLE tblContact (
contactID INT NOT NULL  AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(100),
phone VARCHAR(100),
customerID int,
CONSTRAINT FK_customerID FOREIGN KEY (customerID) REFERENCES tblCustomers(customerID) 
);

CREATE TABLE tblAddress (
addressID INT NOT NULL  AUTO_INCREMENT PRIMARY KEY,
street VARCHAR(100),
houseNumber VARCHAR(15),
city VARCHAR (100),
country VARCHAR (100),
customerID int,
CONSTRAINT FK_customerIDa  FOREIGN KEY (customerID) REFERENCES tblCustomers(customerID)
);

 INSERT INTO tblCustomers (firstname, lastname,vat) VALUES ("John","Doe","UV45856855");
INSERT INTO tblContact (email, phone) VALUES ("0000001","Johndoe@gmail.com");
INSERT INTO tblAddress (street,housenumber,city,country) VALUES ("berkenlaan","1a","Harelbeke","Belgie");

I want to create a mysql database with foreign keys. But when i insert some test data i notice that when i look in tables tblcontact and tbladdress my foreign key is null. I know its a basic question but can you give some suggestions?

CREATE TABLE tblCustomers (
customerID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
vat VARCHAR(30) NOT NULL,
customerVisible varchar(1) NOT NULL DEFAULT 'T'
); 


CREATE TABLE tblContact (
contactID INT NOT NULL  AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(100),
phone VARCHAR(100),
customerID int,
CONSTRAINT FK_customerID FOREIGN KEY (customerID) REFERENCES tblCustomers(customerID) 
);

CREATE TABLE tblAddress (
addressID INT NOT NULL  AUTO_INCREMENT PRIMARY KEY,
street VARCHAR(100),
houseNumber VARCHAR(15),
city VARCHAR (100),
country VARCHAR (100),
customerID int,
CONSTRAINT FK_customerIDa  FOREIGN KEY (customerID) REFERENCES tblCustomers(customerID)
);

 INSERT INTO tblCustomers (firstname, lastname,vat) VALUES ("John","Doe","UV45856855");
INSERT INTO tblContact (email, phone) VALUES ("0000001","Johndoe@gmail.com");
INSERT INTO tblAddress (street,housenumber,city,country) VALUES ("berkenlaan","1a","Harelbeke","Belgie");

原文:https://stackoverflow.com/questions/29181369
更新时间:2022-05-02 10:05

最满意答案

您的问题可以从一些示例数据中受益,但假设我已正确解释您,那么您可能正在寻找以下内容:

var1 <- as.factor(c("A", "B", "A", "A", "A", "A"))
var2 <- as.factor(c("A", "B", "A", "C", "A", "D", "A", "K", "A"))

var3 <- var2[var2 %in% var1]

var3
# > var3
# [1] A B A A A A
# Levels: A B C D K

Your question could benefit from some example data, but assuming I've interpreted you correctly, then you might be looking for the following:

var1 <- as.factor(c("A", "B", "A", "A", "A", "A"))
var2 <- as.factor(c("A", "B", "A", "C", "A", "D", "A", "K", "A"))

var3 <- var2[var2 %in% var1]

var3
# > var3
# [1] A B A A A A
# Levels: A B C D K

相关问答

更多
  • 从vec开始,你可以做... comb_cases = do.call(expand.grid, lapply(vec, function(x) c("", x))) Var1 Var2 Var3 1 2 a 3 b 4 a b 5 c 6 a c 7 b c 8 a b c 空集合有空行,因为可能应该有。 从 ...
  • 就像是 dum.match<-rbind(expand.grid(c(mydf[1,2:3]),c(mydf[2,2:3])),expand.grid(c(mydf[2,2:3]),c(mydf[1,2:3]))) newmydf<-cbind(mydf,paste(mydf$A,mydf$B)%in%paste(dum.match$Var1,dum.match$Var2)) > newmydf name A B paste(mydf$A, mydf$B) %in% paste(dum.match$V ...
  • 我就是这样做的。 这个想法是创建一个距离矩阵,因此您可以将数据聚类成行之间距离为零的行组。 首先,让我们删除(暂时)具有两个或更多破折号的行: two.dashes <- apply(dataf, 1, function(x)sum(x == '-') >= 2) subdata <- dataf[!two.dashes,] 然后,让我们计算一个距离矩阵。 mydist.fun <- function(i, j, x = subdata) { row.i <- x[i, ] row.j <- ...
  • 我不确定这种“教育练习”的价值,但为了编程,这将是我的方法: 首先,让我们创建一些示例预测变量名称。 我在你的例子中使用5个预测变量,但对于10,你显然需要用10替换5。 X = paste0("x",1:5) X [1] "x1" "x2" "x3" "x4" "x5" 现在,我们可以通过combn获得组合。 例如,一次一个变量: t(combn(X,1)) [,1] [1,] "x1" [2,] "x2" [3,] "x3" [4,] "x4" [5,] "x5" 一次两个变量: ...
  • 您的问题可以从一些示例数据中受益,但假设我已正确解释您,那么您可能正在寻找以下内容: var1 <- as.factor(c("A", "B", "A", "A", "A", "A")) var2 <- as.factor(c("A", "B", "A", "C", "A", "D", "A", "K", "A")) var3 <- var2[var2 %in% var1] var3 # > var3 # [1] A B A A A A # Levels: A B C D K Your questio ...
  • 为了更简短的解释和更普遍(正如你提到的行数可以更大10)我建议你可以自己实现以下逻辑:开始时,初始设置。 n <- 10 k <- 3 df <- as.data.frame( lapply(1:n, function(x) x <- k:1 ) ) 下面的代码创建了10个元素集的所有唯一组合,其中顺序无关紧要。 all <- as.matrix( expand.grid(df) ) all_sorted <- t( apply(all, 1, sort) ) all_sorted_unique <- ...
  • 听起来你想要expand.grid 。 expand.grid( marker_a = c(0, 1), marker_b = c(0, 1), marker_c = c(0, 1), marker_d = c(0, 1) ) #> marker_a marker_b marker_c marker_d #> 1 0 0 0 0 #> 2 1 0 0 ...
  • 这是一个使用循环的迭代解决方案。 给你所有可能的数字排列,最多加1,它们之间的距离是N的倍数。这里的想法是将所有数字从0到1(它们之间的距离是N的倍数),然后对于每个一个包括在新列中的所有数字,当添加时不超过1.冲洗并重复,除了在最后一次迭代中,您只需添加完成行的数字行的总和。 就像人们在评论中指出的那样,如果你想要N = 1/499 *,它会给你一个非常大的矩阵。 我注意到,对于N = 1/200,它已经花了大约2,3分钟,所以N = 1/499可能需要太长时间。 * seq(from = 0, to = ...
  • 您可以使用aggregate执行此操作: set.seed(144) dat = data.frame(email=sample(c("yahoo", "gmail"), 10000, replace=T), browser=sample(c("mozilla", "ie"), 10000, replace=T), country=sample(c("usa", "canada"), 10000, replace=T), ...
  • 尝试以下方法: x <- c("a","b","c","d","e") d1 <- combn(x,3) # All combinations d1 # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] # [1,] "a" "a" "a" "a" "a" "a" "b" "b" "b" "c" # [2,] "b" "b" "b" "c" "c" "d" "c" "c" "d" "d" # [3, ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。