目 录
设置timestrap默认值——点击standard checks,在选项卡里设置default值。
修改SQL语句+成功执行(0 errors、0 warnings)
PowerDesigner软件
大佬博客:PowerDesigner个人使用总结
5张数据表之间的依赖关系图
多对多关系 --> 拆分数据表
不同的用户可以操作不同的栏目,多对多-->拆分数据表-->两个“一对多”
logs_info:logs_content——记录操作内容(增删改加、登录信息)。
设置数据表的自动增长主键
设置timestrap默认值——点击standard checks,在选项卡里设置default值。
设置数据表之间的关系——Reference
创建5张数据表并生成(数据库脚本)sql语句
- 操作步骤:Database -> Generate Database
/*==============================================================*/
/* DBMS name: MySQL 5.0 */
/* Created on: 2020/11/19 15:03:44 */
/*==============================================================*/
drop table if exists item_user;
drop table if exists logs_info;
drop table if exists news_info;
drop table if exists news_item;
drop table if exists user_info;
/*==============================================================*/
/* Table: item_user */
/*==============================================================*/
create table item_user
(
item_user_id int not null,
user_id int,
item_id int,
primary key (item_user_id)
);
/*==============================================================*/
/* Table: logs_info */
/*==============================================================*/
create table logs_info
(
logs_id int not null,
user_id int,
logs_content char(10),
primary key (logs_id)
);
/*==============================================================*/
/* Table: news_info */
/*==============================================================*/
create table news_info
(
news_id int not null auto_increment comment '新闻主键',
item_id int not null,
news_title varchar(255) comment '新闻标题',
news_image varchar(255) comment '新闻图片',
news_content text comment '新闻内容',
create_time datetime comment '创建时间',
update_time timestamp comment '更新时间',
primary key (news_id)
);
alter table news_info comment '新闻信息表';
/*==============================================================*/
/* Table: news_item */
/*==============================================================*/
create table news_item
(
item_id int not null auto_increment comment '新闻栏目id',
item_name varchar(255) not null comment '新闻名称',
create_time datetime not null comment '创建时间',
update_time timestamp not null default CURRENT_TIMESTAMP comment '修改时间',
primary key (item_id)
);
alter table news_item comment '新闻栏目表';
/*==============================================================*/
/* Table: user_info */
/*==============================================================*/
create table user_info
(
user_id int not null auto_increment comment '用户主键ID',
user_name varchar(255) not null comment '用户名',
user_pwd varchar(255) binary not null comment '用户密码',
create_time datetime comment '创建时间',
update_time timestamp(0) comment '更改时间',
primary key (user_id)
);
alter table user_info comment '用户信息表';
alter table item_user add constraint FK_Reference_2 foreign key (user_id)
references user_info (user_id) on delete restrict on update restrict;
alter table item_user add constraint FK_Reference_3 foreign key (item_id)
references news_item (item_id) on delete restrict on update restrict;
alter table logs_info add constraint FK_Reference_1 foreign key (user_id)
references user_info (user_id) on delete restrict on update restrict;
alter table news_info add constraint FK_Reference_4 foreign key (item_id)
references news_item (item_id) on delete restrict on update restrict;
SQLyog数据库管理软件
修改SQL语句+成功执行(0 errors、0 warnings)
/*==============================================================*/
/* Table: item_user */
/*==============================================================*/
CREATE TABLE item_user
(
item_user_id INT NOT NULL COMMENT '用户栏目关系主键',
user_id INT COMMENT '用户主键ID',
item_id INT COMMENT '栏目ID',
create_time DATETIME COMMENT '创建时间',
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (item_user_id)
);
ALTER TABLE item_user COMMENT '栏目与用户之间的关系表';
/*==============================================================*/
/* Table: logs_info */
/*==============================================================*/
CREATE TABLE logs_info
(
logs_id INT NOT NULL,
user_id INT,
logs_content CHAR(10),
create_time DATETIME COMMENT '创建时间',
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (logs_id)
);
/*==============================================================*/
/* Table: news_info */
/*==============================================================*/
CREATE TABLE news_info
(
news_id INT NOT NULL AUTO_INCREMENT COMMENT '新闻主键',
item_id INT NOT NULL,
news_title VARCHAR(255) COMMENT '新闻标题',
news_image VARCHAR(255) COMMENT '新闻图片',
news_content TEXT COMMENT '新闻内容',
create_time DATETIME COMMENT '创建时间',
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (news_id)
);
ALTER TABLE news_info COMMENT '新闻详情信息表';
/*==============================================================*/
/* Table: news_item */
/*==============================================================*/
CREATE TABLE news_item
(
item_id INT NOT NULL AUTO_INCREMENT COMMENT '新闻栏目id',
item_name VARCHAR(255) NOT NULL COMMENT '栏目名称',
create_time DATETIME COMMENT '创建时间',
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (item_id)
);
ALTER TABLE news_item COMMENT '新闻栏目表';
/*==============================================================*/
/* Table: user_info */
/*==============================================================*/
CREATE TABLE user_info
(
user_id INT AUTO_INCREMENT COMMENT '用户主键ID',
user_name VARCHAR(255) NOT NULL COMMENT '用户名',
user_pwd VARCHAR(255) BINARY NOT NULL COMMENT '用户密码',
create_time DATETIME COMMENT '创建时间',
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (user_id)
);
ALTER TABLE user_info COMMENT '用户信息表';
ALTER TABLE item_user ADD CONSTRAINT FK_Reference_2 FOREIGN KEY (user_id)
REFERENCES user_info (user_id) ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE item_user ADD CONSTRAINT FK_Reference_3 FOREIGN KEY (item_id)
REFERENCES news_item (item_id) ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE logs_info ADD CONSTRAINT FK_Reference_1 FOREIGN KEY (user_id)
REFERENCES user_info (user_id) ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE news_info ADD CONSTRAINT FK_Reference_4 FOREIGN KEY (item_id)
REFERENCES news_item (item_id) ON DELETE RESTRICT ON UPDATE RESTRICT;
不用管理创建时间和修改时间:
- create_time datetime not null DEFAULT CURRENT_TIMESTAMP,
- update_time timestamp not null DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
备份数据库的数据
备份数据导出的sql文件
/*
SQLyog Ultimate v11.22 (64 bit)
MySQL - 5.5.56 : Database - news_manager
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`news_manager` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `news_manager`;
/*Table structure for table `item_user` */
DROP TABLE IF EXISTS `item_user`;
CREATE TABLE `item_user` (
`item_user_id` int(11) NOT NULL COMMENT '用户栏目关系主键',
`user_id` int(11) DEFAULT NULL COMMENT '用户主键ID',
`item_id` int(11) DEFAULT NULL COMMENT '栏目ID',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (`item_user_id`),
KEY `FK_Reference_2` (`user_id`),
KEY `FK_Reference_3` (`item_id`),
CONSTRAINT `FK_Reference_3` FOREIGN KEY (`item_id`) REFERENCES `news_item` (`item_id`),
CONSTRAINT `FK_Reference_2` FOREIGN KEY (`user_id`) REFERENCES `user_info` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='栏目与用户之间的关系表';
/*Data for the table `item_user` */
/*Table structure for table `logs_info` */
DROP TABLE IF EXISTS `logs_info`;
CREATE TABLE `logs_info` (
`logs_id` int(11) NOT NULL,
`user_id` int(11) DEFAULT NULL,
`logs_content` char(10) DEFAULT NULL,
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (`logs_id`),
KEY `FK_Reference_1` (`user_id`),
CONSTRAINT `FK_Reference_1` FOREIGN KEY (`user_id`) REFERENCES `user_info` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `logs_info` */
/*Table structure for table `news_info` */
DROP TABLE IF EXISTS `news_info`;
CREATE TABLE `news_info` (
`news_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '新闻主键',
`item_id` int(11) NOT NULL,
`news_title` varchar(255) DEFAULT NULL COMMENT '新闻标题',
`news_image` varchar(255) DEFAULT NULL COMMENT '新闻图片',
`news_content` text COMMENT '新闻内容',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (`news_id`),
KEY `FK_Reference_4` (`item_id`),
CONSTRAINT `FK_Reference_4` FOREIGN KEY (`item_id`) REFERENCES `news_item` (`item_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='新闻详情信息表';
/*Data for the table `news_info` */
/*Table structure for table `news_item` */
DROP TABLE IF EXISTS `news_item`;
CREATE TABLE `news_item` (
`item_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '新闻栏目id',
`item_name` varchar(255) NOT NULL COMMENT '栏目名称',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (`item_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='新闻栏目表';
/*Data for the table `news_item` */
/*Table structure for table `user_info` */
DROP TABLE IF EXISTS `user_info`;
CREATE TABLE `user_info` (
`user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户主键ID',
`user_name` varchar(255) NOT NULL COMMENT '用户名',
`user_pwd` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '用户密码',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='用户信息表';
/*Data for the table `user_info` */
insert into `user_info`(`user_id`,`user_name`,`user_pwd`,`create_time`,`update_time`) values (1,'霸刀宋一','111','2020-11-19 15:22:44','2020-11-19 15:22:47'),(2,'爱神丁二','222','2020-11-19 15:23:28','2020-11-19 15:23:53');
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
数据表text字段
create table news_info:news_content text comment '新闻内容'——text 大文本
数据表主键说明
小型数据库:主键字段可以使用int。
大型数据库(数据太多):分库分表。
多表连接查询
需要在数据表中插入数据!
SELECT user_info.`user_name`,news_item.`item_name` FROM user_info
INNER JOIN item_user ON user_info.`user_id` = item_user.`user_id`
INNER JOIN news_item ON item_user.`item_id` = news_item.`item_id`
WHERE user_name = '张三';
SELECT user_info.`user_name`,news_item.`item_name`,news_info.`news_title` FROM user_info
INNER JOIN item_user ON user_info.`user_id` = item_user.`user_id`
INNER JOIN news_item ON item_user.`item_id` = news_item.`item_id`
INNER JOIN news_info ON news_info.`item_id` = news_item.`item_id`
WHERE user_name = '张三';
今天老师主要讲了数据库的一些操作,收获甚多,谢谢观看~
来源:oschina
链接:https://my.oschina.net/u/4341660/blog/4731967