使用外键在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
相关问答
更多-
R根据组合填充值(R fill values depending on combination)[2023-04-13]
从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 空集合有空行,因为可能应该有。 从 ... -
在r组合锻炼(combination workout in r)[2022-02-02]
就像是 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 <- ...
-
逻辑回归:如何尝试R中的每个预测变量组合?(Logistic regression: how to try every combination of predictors in R?)[2022-04-08]
我不确定这种“教育练习”的价值,但为了编程,这将是我的方法: 首先,让我们创建一些示例预测变量名称。 我在你的例子中使用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" 一次两个变量: ... -
R中的可变组合(Variable combination in R)[2023-11-27]
您的问题可以从一些示例数据中受益,但假设我已正确解释您,那么您可能正在寻找以下内容: 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 <- ...
-
在r中获得组合百分比(Getting combination Percentages in r)[2022-05-19]
听起来你想要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 ... -
R中的序列组合矩阵(Sequence Combination Matrix in R)[2022-08-08]
这是一个使用循环的迭代解决方案。 给你所有可能的数字排列,最多加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), ...
-
R中没有重复的组合(Combination without repetition in R)[2021-10-11]
尝试以下方法: 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, ...