mysql 笔记 | |
---|---|
在MySQL数据库建立多对多的数据表关系 | http://blog.51cto.com/13145200724/1370753 |
请问数据库在创建表的时候如何设计表关系,一对一,一对多,多对多 请高手举例说明。谢谢!!! | https://zhidao.baidu.com/question/251111120.html |
CREATE TABLE `sys_users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`work_id` varchar(30) NOT NULL,
`username` varchar(30) DEFAULT NULL,
`real_name` varchar(30) NOT NULL,
`password` varchar(30) NOT NULL,
`locked` bit(1) NOT NULL,
`description` varchar(30) DEFAULT NULL,
`login` bit(1) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `userroleu` FOREIGN KEY (`id`) REFERENCES `user_role` (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
CREATE TABLE `role` (
`rid` int(11) NOT NULL AUTO_INCREMENT,
`rname` varchar(30) NOT NULL,
PRIMARY KEY (`rid`),
CONSTRAINT `userrole` FOREIGN KEY (`rid`) REFERENCES `user_role` (`rid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE `user_role` (
`uid` int(11) NOT NULL,
`rid` int(11) NOT NULL,
PRIMARY KEY (`uid`,`rid`),
KEY `rid` (`rid`),
KEY `uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*
Navicat MySQL Data Transfer
Source Server : conn2
Source Server Version : 50505
Source Host : localhost:3306
Source Database : vpsit
Target Server Type : MYSQL
Target Server Version : 50505
File Encoding : 65001
Date: 2019-01-29 02:00:55
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `boot`
-- ----------------------------
DROP TABLE IF EXISTS `boot`;
CREATE TABLE `boot` (
`id` int(11) DEFAULT NULL,
`bookname` varchar(1024) DEFAULT NULL,
`size` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of boot
-- ----------------------------
-- ----------------------------
-- Table structure for `course`
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`cour_id` int(11) NOT NULL AUTO_INCREMENT,
`NAME` varchar(50) DEFAULT NULL,
`CODE` varchar(30) DEFAULT NULL,
PRIMARY KEY (`cour_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of course
-- ----------------------------
-- ----------------------------
-- Table structure for `role`
-- ----------------------------
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
`rid` int(11) NOT NULL AUTO_INCREMENT,
`rname` varchar(30) NOT NULL,
PRIMARY KEY (`rid`),
CONSTRAINT `userrole` FOREIGN KEY (`rid`) REFERENCES `user_role` (`rid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO `role` VALUES ('1', '22222');
INSERT INTO `role` VALUES ('2', 'fefddvv');
-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`stu_id` int(11) NOT NULL AUTO_INCREMENT,
`NAME` varchar(30) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`class` varchar(50) DEFAULT NULL,
`address` varchar(100) DEFAULT NULL,
PRIMARY KEY (`stu_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student
-- ----------------------------
-- ----------------------------
-- Table structure for `stu_cour`
-- ----------------------------
DROP TABLE IF EXISTS `stu_cour`;
CREATE TABLE `stu_cour` (
`sc_id` int(11) NOT NULL AUTO_INCREMENT,
`stu_id` int(11) DEFAULT NULL,
`cour_id` int(11) DEFAULT NULL,
PRIMARY KEY (`sc_id`),
KEY `stu_FK1` (`stu_id`),
KEY `cour_FK2` (`cour_id`),
CONSTRAINT `cour_FK2` FOREIGN KEY (`cour_id`) REFERENCES `course` (`cour_id`),
CONSTRAINT `stu_FK1` FOREIGN KEY (`stu_id`) REFERENCES `student` (`stu_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of stu_cour
-- ----------------------------
-- ----------------------------
-- Table structure for `sys_users`
-- ----------------------------
DROP TABLE IF EXISTS `sys_users`;
CREATE TABLE `sys_users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`work_id` varchar(30) NOT NULL,
`username` varchar(30) DEFAULT NULL,
`real_name` varchar(30) NOT NULL,
`password` varchar(30) NOT NULL,
`locked` bit(1) NOT NULL,
`description` varchar(30) DEFAULT NULL,
`login` bit(1) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `userroleu` FOREIGN KEY (`id`) REFERENCES `user_role` (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of sys_users
-- ----------------------------
INSERT INTO `sys_users` VALUES ('1', '12', '1122', '111', '61', '', null, '');
-- ----------------------------
-- Table structure for `user_role`
-- ----------------------------
DROP TABLE IF EXISTS `user_role`;
CREATE TABLE `user_role` (
`uid` int(11) NOT NULL,
`rid` int(11) NOT NULL,
PRIMARY KEY (`uid`,`rid`),
KEY `rid` (`rid`),
KEY `uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user_role
-- ----------------------------
INSERT INTO `user_role` VALUES ('1', '1');
INSERT INTO `user_role` VALUES ('1', '2');
例1:学生选修课程
学生表 (主键 学生ID)
课程表 (主键 课程ID)
学生选课表 (联合主键 学生ID 课程ID)
学生表与课程表是实体数据表,学生选课表就是这两个实体对象间的对应关系,能过这个关系表可以建立起一个学生选修多门课程的关系。。。
例2:用户帐户权限分配
帐户信息表 (主键 帐户ID)
角色信息表 (主键 角色ID)
为了使一个帐户可被指定多重角色,还需要一个能将“帐户信息表”和“角色信息表”关联起来的“帐户角色表(联合主键 帐户信息ID 角色信息ID)”
另外 xampp确实好用