sql_ based learning _ _ relational model primary keys, foreign keys, indexes

Relational databases are based on the relational model. The relational model is essentially a two-dimensional table a number of storage data, you can see them as a lot of Excel table.

Each row of the table is called a record (Record), record data on a logical sense.

Each column of the table is called a field (Column), with each row of a table have the same number of fields.

Field defines the data types (integer, floating point, string, date, etc.), and is allowed to NULL. Note NULLindicates that the field data does not exist. If the field is an integer NULLnot represent its value 0. Similarly, a string field NULLdoes not indicate that it is the empty string ''. --NULL and is not 0 and the 'comparison.

The need to establish a "one to many" between tables and tables of relational databases, the relationship between "many" and "one to one", so to be able to organize and store the data in accordance with the logic of the application.

In a relational database, the relationship is established by the primary and foreign keys to maintain.

1, the primary key: can be uniquely distinguished by different recording a field, this field is referred to as the primary key;

In a relational database, a data table for each row is called a record. A record is made of a plurality of fields. Each record contains a number of defined fields. A table with all the records have the same field definitions.

Table constraint: two arbitrary recording can not be repeated;

Primary requirements: record once inserted into the table, it is best not modify the primary key, the master key is used as the only location history, and change the primary key, will cause a range of effects;

Select the basic principles of the primary key: do not use any service-related fields as the primary key;

As the primary key is the best business completely unrelated field, we generally set this field to be named id. Common as idtype field are:

  1. The self-energizing type Integer: a database is automatically assigned to each integer increment when inserting the data record, so we do not worry about the primary key is repeated, they do not themselves generate a predetermined primary key;

  2. Type globally unique GUID: uses a globally unique string as a primary key, similarly 8f55d96b-8acc-4636-8cb8-76bf8abc2f57. GUID algorithm through the network card MAC address, time stamp and a random number strings to ensure that any computer at any time generated is different, most programming languages have built-in GUID algorithm, you can budget yourself out of the main key.

Primary key : i.e., two or more fields are the primary key, the master key is called the primary key; for the primary key, a duplicate allowed, as long as not all primary key columns can be repeated:

A primary key is a unique identifier recorded in the table. Select the primary key is very important: not the primary key with a business sense, but should use BIGINT increment or GUID type. Primary key should not be allowed NULL.

A plurality of columns may be used as the primary key, the primary key is not used.

Foreign Key: it can be associated with other data tables together, which is called a foreign key column;

Foreign key is not achieved by the column name, but by defining the foreign key constraint:

ALTER TABLE students
ADD CONSTRAINT fk_class_id
FOREIGN KEY (class_id) ——名称可以任意 REFERENCES classes (id); —— 外键关联到哪个表的键
通过定义外键约束,关系数据库可以保证无法插入无效的数据。即如果classes表不存在id=99的记录,students表就无法插入class_id=99的记录。

As the foreign key constraint will reduce the performance of the database, the majority of Internet applications for the pursuit of speed, does not set the foreign key constraint, but only by the application itself to ensure the correctness of the logic. In this case, class_idjust a regular column, but it played a key role outside of it.

To delete a foreign key constraint, also through the ALTER TABLEimplementation:

ALTER TABLE students
DROP FOREIGN KEY fk_class_id; 

Note: Delete the foreign key constraint and the foreign key is not deleted this column. Delete columns through DROP COLUMN ...implementation.

Many to many: many relationship is actually implemented by two-many relationship, that is, through an intermediate table, associated with two-many relationship, the formation of many relationship; through the intermediate table, we define a relationship "many to many."

One: recording a table corresponds to a unique one record to another table.

There are some applications, a large table will split into two tables one object is to separate and often do not always read the read field, in order to obtain higher performance. For example, to split a large user table for the user basic information table user_infoand user detailed information table user_profiles, most of the time, only need to query the user_infotable, you do not need to query the user_profilestable, so that you can improve query speed.

Relational database can achieve many, and many-to-one relationship by a foreign key. Foreign key may be constrained by the database, constraints may not be provided, only the logic of the application to rely guaranteed.

 

Guess you like

Origin www.cnblogs.com/CrazyJioJio/p/12591679.html