The basic concept of Mysql

A: Relationship

--1-- basic relationship.

One: a class has a class teacher

Many: there are more students in a class

Many-to-: more than one student in a class

Many to many: a plurality of teacher class, the teacher belonging to a plurality of classes (two many-implemented, i.e., an intermediate join a table, the associated two-to-many)

--2-- primary key

Very important constraint, the difference of the record other data identifier must be unique, can not be modified

Selection principle, do not use any business-related field primary key, such as identity cards, mail, etc.

Ⅰ: integer increment

Up to about 2.1 billion

Ⅱ: globally unique GUID

By a string GUID algorithm to MAC address, time stamp, a random number counted out "8f55d96b-8acc-4636-8cb8-76bf8abc2f57", up to 92.2 billion one hundred million

Ⅲ: primary key

Two or more primary key field is set, allowing n-1 column congruence

--3-- foreign key

Is achieved by constraining one, one to many, many to one relationship.

ex: a unique studentID Xiaoming, a class with n students, student added class_id listed in the table, the record corresponding to the student in a class record

Implementation: to achieve rather than the column name by defining foreign key constraints

ALTER TABLE students
ADD CONSTRAINT fk_class_id
FOREIGN KEY (class_id)
REFERENCES classes (id);

// foreign key constraint name can be arbitrarily fk_class_id, a FOREIGN  KEY (class_id) specifies
 // class_id as a foreign key, the REFERENCES classes (ID) assigned to the associated foreign key
 // ID column of table classes (i.e., classes table primary key).
Foreign key definitions
ALTER TABLE students
DROP FOREIGN KEY fk_class_id;

// delete the foreign key constraint and the foreign key is not deleted this column. Delete columns by DROP the COLUMN ... achieve
 // foreign key constraint and a foreign key to distinguish
Remove the foreign key

Defects: Due to foreign key constraints 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.

II: query data

--1-- basic query

SELECT

--2-- condition query

WHERE

--3-- projection inquiry

The results contain the specified column to column names named

--4-- sort query

Order: ORDER BY ASC --- reverse order: ORDER BY DESC

--5-- paging query

LIMIT <M> OFFSET <N>

--6-- aggregate query

Query by polymerizing (built-in) functions

SUM(),AVG(),MAX(),MIN()

Groups: GROUP BY, return is based on grouping condition plurality of records

--7-- multi-table query

FROM <表1>,<表2>

When the two tables have the same column name, can be used to modify the column name projection, the projection can likewise simplified table

--8-- join query

SELECT...FROM <表1> JOIN <表2> ON <条件...>

INNER JOIN data in Table 1, Table 2, even if it is not too

OUTER JOIN data in Table 2, Table 1 is not even came out

FULL OUTER JOIN Tables 1 and 2 have to come out

Three: modify data

--1--INSERT

INSERT INTO <表名> VALUES()

--2--UPDATE

UPDATE <table name> SET the WHERE field 1 = value 1

--3--DELETE

DELETE FROM <表名> WHERE

Guess you like

Origin www.cnblogs.com/KSea/p/12159226.html
Recommended