Python notes: database design based on the Django framework

Outline

Needs, interface prototypes, various standards for database design based on "Project Proposal", "requirements specification", we are the first reference design

Design

1) find an entity

  • Entity is an entity - the basic object-relational model, is the real world abstract various things.
  • Anything that can be opened from each other and can be knowledgeable and things, concepts and other objects can be considered as an entity.
  • The basic list of entities is as follows:
    • member
    • category
    • Product
    • Order form
    • order details
    • Shipping address
    • Product Reviews
    • product picture
    • Links
    • Announce

2) to find property

  • Each entity has a set of characteristics or properties, known as attributes of the entity.
  • Attribute value of the entity is the main data stored in the database, a property is actually equivalent to a column in the table.
  • If a member entities: account number, password, real name, gender, address, zip code, phone, email, status, Member
  • Physical goods: product name, manufacturer, description, price, pictures, status, inventory, purchases, clicks, add time

3) find the relationship between

  • Each item belongs to a category, there may be multiple items under each category, and then the relationship category product was: 1: n (1 to-many relationship)
  • Each order belongs to a member, you can have multiple orders each member, the relationship between the members and the order is: 1: n (1-many relationship)
  • Each order details belong to an order under each order can have multiple purchases, then the relationship: 1: n (1-many relationship)

4) Draw ER FIG.

Online Paint: https: //www.processon.com/

5) Other notes

  • In the beginning of time, table design some more no less significant relationship, with the commencement of the project, there will be adjustments (add fields, add attributes, add tables, subtraction tables, sub-libraries, sub-tables, etc.)
  • When the project development, the formation of post-processing time once the database must be handed over to a person to handle (DBA to do), you will notice the update log table structure changes after completion, so that we sync the latest table

Logical Structure Design

On the basis of the relationship between the above entities, the conversion link between entities, attributes, entities and relationships mode.

1) to determine the relationship between mode

  • The conversion algorithm, the ER FIG there are five types of entities, relationships can be converted into four modes:
    • Member (id number, account number, password, real name, gender, shipping address, zip code, telephone, Email, state, registration time)
    • Product category (category id number, class name, parent category id, class path)
    • Goods (merchandise id, class id, product name, manufacturer details, description, price, image name, inventory, purchase quantity, clicks, status, add time)
    • Order (Order id number, membership id number, contact person, shipping address, zip code, telephone number, time of purchase, the total amount, status)
    • Order details (id number, order id number, item id number, product name, price, purchase amount)

2) eliminate redundant

  • Redundant data and redundant links easily undermine the integrity of the database, the database more difficult to maintain and should be eliminated.
  • Redundant data and redundant relationships of the system have been processed, the structural design concept herein described will not be excessive.

Physical Design

  • The last stage is to determine the database design database storage structures and access methods on the physical devices, i.e. the physical data model.
  • Design physical data model is actually in the design table structure.
  • In general, the entity corresponding to the table, attributes of the entity corresponding to the column (field) of the table, the relationship between the entity becomes bound table.

1) Design data table structure

Users Table 1.1: users

2.2 classification of goods: type

2.3 Product Information Table: goods

Orders Table 2.4: orders

2.5 Order Details table: detail (or order_detail better)

2) to create a data table through the data table structure

sql script

-- 会员信息表(后台管理员信息也在此标准,通过状态区分)
CREATE TABLE `users`(
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(32) NOT NULL,
  `name` varchar(16) DEFAULT NULL,
  `password` char(32) NOT NULL,
  `sex` tinyint(1) unsigned NOT NULL DEFAULT '1',
  `address` varchar(255) DEFAULT NULL,
  `code` char(6) DEFAULT NULL,
  `phone` varchar(16) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `state` tinyint(1) unsigned NOT NULL DEFAULT '1',
  `addtime` datetime DEFAULT NULL, 
  PRIMARY KEY (`id`),      
  UNIQUE KEY `username` (`username`)
)ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

-- 商品类别表
CREATE TABLE `type`(
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(32) DEFAULT NULL,
  `pid` int(11) unsigned DEFAULT '0',
  `path` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
)ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

-- 商品信息表
CREATE TABLE `goods`(
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `typeid` int(11) unsigned NOT NULL,
  `goods` varchar(32) NOT NULL,
  `company` varchar(50) DEFAULT NULL,
  `content` text,
  `price` double(6,2) unsigned NOT NULL,
  `picname` varchar(255) DEFAULT NULL,
  `store` int(11) unsigned NOT NULL DEFAULT '0', 
  `num` int(11) unsigned NOT NULL DEFAULT '0',
  `clicknum` int(11) unsigned NOT NULL DEFAULT '0',
  `state` tinyint(1) unsigned NOT NULL DEFAULT '1',
  `addtime` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `typeid` (`typeid`)
)ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

-- 订单信息表
CREATE TABLE `orders`(
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `uid` int(11) unsigned DEFAULT NULL,
  `linkman` varchar(32) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  `code` char(6) DEFAULT NULL,
  `phone` varchar(16) DEFAULT NULL,
  `addtime` datetime DEFAULT NULL,
  `total` double(8,2) unsigned DEFAULT NULL,
  `state` tinyint(1) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
)ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

-- 订单信息详情表
CREATE TABLE `detail`(
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `orderid` int(11) unsigned DEFAULT NULL,
  `goodsid` int(11) unsigned DEFAULT NULL,
  `name` varchar(32) DEFAULT NULL,
  `price` double(6,2) DEFAULT NULL,
  `num` int(11) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
)ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


-- 在user上表中添加一条后台管理员账户数据
insert into users values(null,'admin','管理员',md5('admin'),1,'北京市','100086','15311111111','[email protected]',0,now())

Security and confidentiality Design

  • This system uses a database login user name plus password safe way. Privileged user name is limited to only basic CRUD data capabilities.
  • Data for database applications generally have certain limitations, such restriction is called integrity.
  • Relational database system should ensure that the value of the input data types consistent with its provisions, and to ensure that the value is in the range supported by the system.
  • Relational database systems support three kinds of integrity: 域约束, 实体完整性约束and 关联完整性约束.
发布了417 篇原创文章 · 获赞 228 · 访问量 70万+

Guess you like

Origin blog.csdn.net/Tyro_java/article/details/104473445