Database design a circle of friends

Keywords: Database MySQL

The structure of the circle of friends really gives me a headache - how to implement it in a multi-layer architecture and how to write the script; And the ghost virtual machine, kind of learning is not good at self castration. Do it from the beginning, then.

Each image has its own URL: uniform resource location address protocol: / / hostname [: Port] / path / [; parameters] [? Query] #fragment. Hostname uses DNS domain name or IP address, and port can be saved.
CDN content delivery network: I don't know why.

The basic structure of my circle of friends: Publishing {content, comments, likes}, users, timeline.

Implementation of batch processing:
Dynamic: Publishing dynamic

Comments:

  • Post comments
  • Delete comment

User:

  • User registration
  • User logout
    View:
  • When users search for friends, they search by name, which involves the problem of the same name.

Chapter 3 content relational database standard language

SQL Structured Query Language
Features: 1. Comprehensively define DDL, operate DML and control DCL functions; 2. Highly non procedural: the access path is automatically completed by the system; 3. Set oriented operations: relational algebra, set algebra.
Out of architecture mode – view, base table, mode – base table, in architecture mode – storage file

data type
Relationship between schema and basic table: the search path contains a list of schemas, similar to the working environment.

Mode 1 CREATE SCHEMA *** AUTHORIZATION *** CASCADE/RESTRICT{} ;
Mode II CREATE TABLE ***.ss(***);

Table RDBMS stores schema information and integrity constraints into the data dictionary.
Index improves query efficiency: RDBMS data access is based on attribute value (rather than pointer) query, and the mapping from logical layer to physical layer is realized through index. B + tree: dynamic balance / HASH index: fast.

The view is a virtual table

Chapter V content database integrity

What does integrity do- Correctly reflect the displayed data
Factors of data distortion?
Internal causes: 1. input error: mobile phone number, ID number format, nickname repetition; 2. System processing: the content is overwritten and is not set. Check whether the user's nickname can be repeated;
-External cause: 3. System failure: you may have deleted a table by mistake; 4. Malicious destruction: stealing numbers;
Methods: 1. Operation authority management; 2. Content integrity audit;

Integrity constraint does not speak human language definition: for relationship pattern R, specific relationship R ∈ \in ∈ r satisfies the constraint condition f (R); Scope: attribute, tuple, table and multi table. The smaller the scope, the lower the cost:.
  definition- DDL definition language
  check- insert... Check rules for changing database operations; Consider cost.
  violation handling- Reject NO ACTION, CASCADE cascade, set to null, set to null
  responsible- RDBMS automatically builds indexes for full table scanning.

Take a look at each: (use "integrity constraint named clause" to facilitate modification!!!)
1. Entity integrity: define column level PRIMARY KEY, NOT NULL; Table level PRIMARY KEY(r0,r1); Violation rejection master code value is not unique and attribute value is empty.
2. Reference integrity: define the FOREIGN KEY of the reference table and the primary code of the cross reference of the referenced table PREFERENCES; In case of violation, all three can be used and rejected by default; You need to define whether the external code is null. For example, unexpectedly... For example, if a student has not assigned a major, the major number is null.
3. User defined integrity: UNIQUE, NOT NULL, CHECK; Violation of refusal;

From the above, let's preliminarily establish the table:
Consideration: 1. In order to improve efficiency, do you need to set sorting or index fields for each table? 2. Random generation? The system generates in order? 3. What is the difference between domain and named clause? 4. How to choose an address when registering?
1. Database building

CREATE DATABASE pyq DEFAULT CHARACTER SET=utf8mb4 COLLATE utf8mb4_unicode_ci;
USE pyq

2.1 user table

surfaceuserPrimary keyForeign keysort fieldIndex field
usersDBAuid/uiduid
Attribute nametypeDefault valueentityreferenceUser defined
uidvarchar(8)The system generates in orderPRIMARY KEYREFERENCES/
unamvarchar(20)wxid + system generation/come fromNOT NULL
ulocvachar(20)///set selection
usexchar(2)//Male, female, none
ubirdatetime//1900-01-01 00:00:00 to 2021-12-31 23:59:59
upwdvarchar12345678//NOT NULL, len>=8
CREATE TABLE users
{
	uid VARCHAR(8) PRIMARY KEY
	unam VARCHAR(20) NOT NULL
	uloc VARCHAR(20)
	usex CHAR(2)
	CONSTRAINT csex CHECK(usex in ('female','male','nothing')	
	ubir DATETIME
	CONSTRAINT cbir CHECK(ubir BETWEEN 1900-01-01 00:00:00 AND 2021-12-31 23:59:59)
	upwd	VARCHAR NOT NULL
	CONSTRAINT cpwd CHECK(len(upwd)>=8)
	UNIQUE INDEX users_idx(uid ASC)
};

2.2 friends list

surfaceuserPrimary keyForeign keysort fieldIndex field
friendsDBA/fid1,fid2fid1fid1
Attribute nametypeDefault valueentityreferenceUser defined
fid1varchar(8)//Foreign key, refer to users.uidNOT NULL
fid2varchar(8)//Foreign key, refer to users.uidNOT NULL
CREATE TABLE friends
{
	fid1 VARCHAR(8) NOT NULL
	fid2 VARCHAR(8) NOT NULL
	FOREIGN KEY(fid1) REFERENCES users(uid)
	FOREIGN KEY(fid2) REFERENCES users(uid)
	INDEX friends_idx(fid)
};

Isn't there a unique index that can be used here?
2.3 dynamic table

surfaceuserPrimary keyForeign keysort fieldIndex field
momentsDBAmiduidmidmid
Attribute nametypeDefault valueentityreferenceUser defined
midvarchar(16)The system generates in orderPrimary key//
muidvarchar(8)//Foreign key, refer to users.uidNot empty
mtimdatetime///Not null, 2010-01-01 00:00:00 to 2025-12-31 23:59:59
mcontext///Not empty
mpicvarchar////
CREATE TABLE moments
{
	mid VARCHAR(16) PRIMARY KEY
	muid VARCHAR(8) NOT NULL
	mtim DATETIME NOT NULL
	CONSTRAINT ctim CHECK(mtim BETWEEN 2010-01-01 00:00:00 AND 2025-12-31 23:59:59)
	mcon TEXT NOT NULL
	mpic VARCHAR
	FOREIGN KEY(muid) REFERENCES users(uid)
	UNIQUE INDEX moments_idx(mid)
};

2.4 timeline table

surfaceuserPrimary keyForeign keysort fieldIndex field
timeline_of_uidDBA/mid/mid
Attribute nametypeDefault valueentityreferenceUser defined
tmidvarchar(16)//Foreign key, refer to moments.midNOT NULL, UNIQUE
tstatinyint///0 or 1
CREATE TABLE timeline_of_uid
{
	tmid VARCHAR(16) NOT NULL UNIQUE
	tsta TINYINT NOT NULL
	CONSTRAINT csta CHECK(tsta in VALUES(0, 1))
	FOREIGN KEY(tmid) REFERENCES moments(mid)
	UNIQUE INDEX timeline_idx(tmid)
};

2.5 comment form

surfaceuserPrimary keyForeign keysort fieldIndex field
commentsDBAcidcmidcidcmid
Attribute nametypeDefault valueentityreferenceUser defined
cidvarchar(8)The system generates in orderPrimary key//
cmidvarchar(16)//Foreign key, refer to moments.midNOT NULL
cuidvarchar(8)//Foreign key, refer to users.uidNOT NULL
creuidvarchar(8)//Foreign key, refer to users.uid/
ctimdatetime///Not null, 2010-01-01 00:00:00 to 2025-12-31 23:59:59
ccontinytext///Not empty
CREATE TABLE timeline_of_uid
{
	cid VHARCHAR(8) PRIMARY KEY
	cmid VARCHAR(16) NOT NULL
	cuid VARCHAR(8)  NOT NULL
	creuid VARCHAR(8)  NOT NULL
	ctim DATETIME NOT NULL
	CONSTRAINT tim CHECK(ctim BETWEEN 2010-01-01 00:00:00 AND 2025-12-31 23:59:59)
	ccon TINYTEXT NOT NULL
	FOREIGN KEY(cmid) REFERENCES moments(mid)
	FOREIGN KEY(cuid) REFERENCES users(uid)
	FOREIGN KEY(creuid) REFERENCES users(uid)
	UNIQUE INDEX timeline_idx(cmid)
};

2.6 praise form

surfaceuserPrimary keyForeign keysort fieldIndex field
likesDBA/lmid/lmid
Attribute nametypeDefault valueentityreferenceUser defined
lmidvarchar(16)//Foreign key, refer to moments.midNOT NULL
luidvarchar(8)//Foreign key, refer to users.uidNOT NULL
ltimdatetime///Not null, 2010-01-01 00:00:00 to 2025-12-31 23:59:59
CREATE TABLE likes
{
	lmid VARCHAR(16) NOT NULL
	luid VARCHAR(8)  NOT NULL
	ltim DATETIME NOT NULL
	CONSTRAINT ctim CHECK(ltim BETWEEN 2010-01-01 00:00:00 AND 2025-12-31 23:59:59)
	FOREIGN KEY(lmid) REFERENCES moments(mid)
	FOREIGN KEY(luid) REFERENCES users(uid)
	UNIQUE INDEX likes(lmid)
};

Posted by ginginca on Sat, 30 Oct 2021 00:02:40 -0700