SQL / establish a database / foreign key primary key &

SET FOREIGN_KEY_CHECKS = 0; # cancel a foreign key constraint, Mysql, if established between the table and the table of foreign key constraints, you can not delete the table and modify table structure. 

TABLE custinfo` the IF EXISTS `the DROP; 
the CREATE TABLE custinfo`` ( 
  `custID` VARCHAR (. 19) the COMMENT the NOT NULL 'customer number', 
  ` name` VARCHAR (10) the COMMENT the NOT NULL 'name', 
  `sex` VARCHAR (10) NOT NULL COMMENT 'sex', 
  `phone` VARCHAR (20) NOT NULL COMMENT 'phone number', 
  ` ID` VARCHAR (19) NULL the DEFAULT the COMMENT 'ID number', 
  `districtID` VARCHAR (19) NULL the DEFAULT the COMMENT 'area ID ', 
  `age` VARCHAR (. 6) the COMMENT the NOT NULL' aged ', 
  a PRIMARY KEY (` custID`), 
   KEY FK_districtID_1` `(` districtID`),

- ---------------------------- 
- Records of CustInfo 
- ------------- --------------- 
the BEGIN; 
the INSERT the INTO `custinfo` the VALUES ( '. 1', 'Yan eleven', 'M', '13645667783', '567891321242345618', '10101', '18 is'); 
the INSERT the INTO `custinfo` the VALUES ( '2', 'Zhu three shots', 'M', '13,645,667,890', '567891984242345618', '20101', '28'); 
the INSERT the INTO` custinfo` the VALUES ( '3', 'ginger people', 'M', '13,642,345,799', '567891322489345618', '20101', '18 is'); 
the INSERT the INTO `custinfo` the VALUES ('. 4 ',' Andy ',' M ',' 13612345690 ',' 567891989909345618 ',' 30201 ',' 28 '); 
the INSERT the INTO `custinfo` the VALUES ('. 5 ''Joe Smith three', 'F', '13,643,455,799', '567891322489349898', '30201', '18 is'); 
the INSERT the INTO `custinfo` the VALUES ( '. 6', 'Liusi Si', 'M', '13643215690' , '567891989909349007', '30201', '28'); 
a COMMIT;

- ---------------------------- 
- the Table Structure for `cardinfo` 
- ---------- ------------------ 
the DROP TABLE `cardinfo` the IF EXISTS; 
the CREATE TABLE cardinfo`` ( 
  `acct_no` VARCHAR (. 19) the COMMENT the NOT NULL 'accounts', 
  ` VARCHAR balance` (19) NOT NULL COMMENT 'account balances', 
  `acct_status` VARCHAR (19) NOT NULL COMMENT 'account status', 
  ` openDate` VARCHAR (19) NOT NULL COMMENT 'open time card', 
  `openmoney` VARCHAR (. 19) the NOT NULL COMMENT 'open her account', 
  `custID` VARCHAR (19) the NOT NULL COMMENT 'customer number', 
  ` districtID` (19) the NOT NULL the COMMENT VARCHAR 'accounts where the district ID', 
  PRIMARY KEY ( `acct_no`), 
  KEY` FK_districtID` ( `districtID`), 
  KEY` FK_custID` ( `custID`),
  CONSTRAINT `FK_districtID` FOREIGN KEY (`districtID`) REFERENCES `area_dim` (`districtID`),
  CONSTRAINT `FK_custID` FOREIGN KEY (`custID`) REFERENCES `custinfo` (`custID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='银行卡信息表';

-- ----------------------------
-- Records of cardinfo
-- ----------------------------
BEGIN;
INSERT INTO `cardinfo` VALUES ('1010357612121001', '888888', '1', '2019-07-19 08:49:37', '1.00', '1', '10101');
INSERT INTO `cardinfo` VALUES ('1010357612121002', '888888', '1', '2019-07-18 08:49:37', '2.00', '2', '20101');
INSERT INTO `cardinfo` VALUES ('1010357612121003', '888888', '0', '2019-07-19 08:49:37', '3.00', '3', '20101');
INSERT INTO `cardinfo` VALUES ('1010357612121004', '888888', '1', '2019-07-19 08:49:37', '1.00', '1', '10101');
INSERT INTO `cardinfo` VALUES ('1010357612121005', '888888', '1', '2019-07-18 08:49:37', '2.00', '2', '20101');
COMMIT;

DROP TABLE IF EXISTS `area_dim`;
CREATE TABLE `area_dim` (
  `districtID` varchar(19) NOT NULL COMMENT '区域ID',
  `area_name` varchar(19) NOT NULL COMMENT '区域名',
  `city_id` varchar(19) NOT NULL COMMENT '城市ID',
  `city_name` varchar(19) NOT NULL COMMENT '城市名',
  `province_id` varchar(19) DEFAULT NULL COMMENT '省ID',
) = ENGINE the InnoDB the DEFAULT the CHARSET = UTF8 the COMMENT = 'region code value table';
  a PRIMARY KEY (` districtID`)
  province name',`province_name` varchar (19) DEFAULT NULL

- ---------------------------- 
-- Records of area_dim
-- ----------------------------
the BEGIN; 
the INSERT the INTO `area_dim` the VALUES ( '10101', 'Erqi' ' 101 ',' Zhengzhou City ',' 1 ',' Henan '); 
INSERT INTO `area_dim` VALUES (' 20101 ',' high-tech zones ',' 201 ',' Qingdao ',' 2 ',' Shandong Province '); 
the INSERT the INTO `area_dim` the VALUES (' 30201 ',' New ',' 301 ',' Shijiazhuang ',' 3 ',' Hebei '); 
a COMMIT; 
the sET = FOREIGN_KEY_CHECKS. 1; # constraint provided outside the subject

NOTE: When the key 2 is set to the outer table of the same field twice to different foreign key name.

Guess you like

Origin www.cnblogs.com/Python-sql007/p/11241495.html