Advanced database operations and review

New data

Basic syntax
insert into table [(field list)] values (value list);
data inserted, assuming a value corresponding to the primary key already exists, the insertion will fail.

Primary key violation
when there is a conflict of the primary key may be selectively processed; updating a replacement

Primary key violation: update
Insert into table [(field list: A primary key)] values (value list) on duplicate key update new field = value;

insert into test_class values(1,1);
insert into test_class values(1,2);

Primary key violation: Update
insert into test_class values (1,2) on duplicate key update roomId = 2;
Here Insert Picture Descriptionprimary key violation: Alternatively
replace into test_class values (2,4);

Here Insert Picture DescriptionData Added: worm copies

The worm copies: to acquire data from existing data, then the data and carry out new operations; increased exponentially data.

Advanced table creation: Create a new table (copy the table structure) from the existing table

. Create table like table database table names;

Here Insert Picture DescriptionJust copy the structure, the data is not copied.
The worm copies: first find the data, and then add the isolated data again.
insert into table [(field list)] field list SELECT / * from the data table;

For example: insert into copy select * from cc ;
Here Insert Picture Descriptionmeaning the worm copy
1, copy data from an existing table to the new table
2. You can quickly make the data table expansion to a certain magnitude; stress test tables and efficiency.

Update data
update table set field = value [where Condition]

New high-level syntax
update table set field = value [where Condition] [limit the number of updates]
For example: update copy set name = 3 where name = 1 limit 3;
Here Insert Picture Description
deleting data
with similarly updated: limit by limiting the number may
delete from table [where condition] [limit conditions]
e.g. delete from copy where name = 3 limit 2;

Here Insert Picture Description
Ideas: delete data that does not change the structure of the table, the table can only be deleted after reconstruction table.
truncate table
Empty Table: Reset from growth

Query data
basic grammar
select field list / * from table [where conditions]

The complete syntax
select [select option] field list [field alias] / * from the data source [where conditional clause] [group by clause] [having clause] [order by clause] [limit clause];

select Options
select the option: select treatment check out the results of
All: By default, retains all the results
Distinct de-emphasis, check out the results, will be repeated to remove (all fields are the same)

Here Insert Picture Descriptionselect (distinct field list), other fields

Fields Alias
field alias: When querying data came out, and sometimes the name does not necessarily meet the demand (when multi-table queries, there will be a field of the same name), you need to rename the field names: Alias

Syntax
field names [as] alias;

the SELECT
stuId as student number,
stuname as name,
Sex sex from test_student;
Here Insert Picture Descriptionwhether as can be.

Data Source
Data source: source data, source relational database table data are, in essence, as long as the data is similar to the two-dimensional table, and ultimately can be used as a data source.
Is divided into multiple data sources: single table data source, multi-table data source query
single data source table: select * from table
multi-table data source: select * from Table 1, Table 2;
Here Insert Picture DescriptionRemove a record from a table, in addition to all the records in a table match, and all the number of records and the number of reserved fields :(), the result is called: Cartesian product (cross-connect): Cartesian product of no use to avoid.

Subquery: source of the data is a query (query result is two-dimensional table)
the SELECT * from (the SELECT statement) as the table name;

* from SELECT (SELECT * from test_student) AS S;
Here Insert Picture DescriptionWhere clause
Where clause: used to determine data, screening data.

where clause return: 0 or 0 representative of false, 1 representative of true.
Analyzing conditions: comparison operators>, <,> =, < =, =, <!>, =, like, between and, in, not in
logical operators: && (and), || ( or), (not)!

where principle: where a is the only time to get data directly from the disk began to judge the conditions, remove a record from the disk, start where judgment; if the result of the judgment established saved in memory, if it fails to give up directly.

the random number rand 0-1, floor rounding down
update test_student set age = floor (rand () * 20 + 20), height = floor (rand () * 20 + 170);

Here Insert Picture DescriptionConditions Query 1: 1, 3, asked to identify the student id students
select * from test_student where stuId = 1 || stuId = 3;
or select * from test_student where stuId in ( 1,3);

Here Insert Picture DescriptionQuery Condition 2: students find height of between 180-190
SELECT * WHERE height from test_student> = 180 [height and <= 190;
SELECT * WHERE height from test_student BETWEEN 190 and 180 [;

Here Insert Picture Descriptionbetween itself closed interval; values must be left between a value less than or equal to the right.
For example: select * from test_student where height between 190 and 180;
Here Insert Picture Descriptionboth conditions are met:
SELECT * WHERE from test_student. 1;

Here Insert Picture Description
Group by clause
Group by: grouping means, are grouped according to a field (the same discharge set, different sets of assigned)

The basic syntax: group by field names
such as: gender grouping
select * from test_student group by sex;
Here Insert Picture Descriptionmeaningful groups: to statistical data (by group statistics: statistical data grouped fields)
SQL provides a range of statistical functions
count () : the number of records statistical packet; the number of records for each group of
max (): maximum value of each statistical
min (): minimum statistical
AVG (): statistical mean
Sun (): statistics and
packet statistics: height height , average age and the total age of
select sex, count (*), max (height), min (height), avg (age) sum (age) from test_student group by sex;

Here Insert Picture Descriptioncount () function: count the number of records, you can use two parameters, * represents statistical records, field representatives statistics corresponding field (null does not count)

Here Insert Picture DescriptionAutomatically ordering packets: packet fields according to the default order.
group by field [asc | desc]; - the result is then the entire grouping result after the merge sort.

Here Insert Picture DescriptionMulti-field groups: the first group according to a field, then grouped the results are grouped by other field again.

alter table test_student add c_id int;
update test_student set c_id=ceil(rand3);
insert into test_student values(4,‘王五’,‘女’,24,170,2);
select c_id,sex,count(
) from test_student group by c_id,sex;

First class, the men
Here Insert Picture Descriptionhave a function: can be connected to a string (the set of all reserved one field) of the results of a field in the packet;
GROUP_CONCAT (Field);

Here Insert Picture DescriptionStatistics back: with rollup: any after a group will have a team, the last packet is required to report to their superiors statistics; field according to the current packet, which is back Statistics: Statistics back when grouping will field blank.

select c_id,count(*) from test_student group by c_id with rollup;

Here Insert Picture Description
select c_id,sex,count(*) from test_student group by c_id,sex,with rollup;

Here Insert Picture DescriptionMulti-field back: this will be considered first layer packet back; the number of groups to look at the second group of the first group, the number of groups is how much, how much is back, and then add back to the first layer.

Having clause
Having clause: where clause as: conditional judgment.
where the data disk is for determining, after entering into the memory, are grouped operation; grouping result needs to having to deal with.
Having to do things where almost all do, but where not all things having to do do.

1, and the statistical results or is just having to use statistical functions

Determine all class sizes than or equal to the number of students 2 of
the SELECT c_id, COUNT ( ) from test_student Group by c_id hacing COUNT ( )> = 2;
the SELECT c_id, COUNT ( ) from test_student the WHERE COUNT ( )> = 2 Group by c_id; - error
Here Insert Picture Description2, having to use a field alias; where not; where is to take data from the disk, but the name can only be a field name, alias is entered in the field will produce memory later.

select c_id,count() as total from test_student group by c_id having total>=2;
select c_id ,count(
) as total from test_student where total >= 2 group by c_id ;

Here Insert Picture Descriptionselect stuName as name, stuId student number from test_student having names like 'Zhang%';
select stuName as name, stuId student number from test_student where name like 'Zhang%'; - error
Here Insert Picture DescriptionOrder by clause

Order by clause: sorting, in ascending or descending order according to a field, set dependent proofreading.
Use basic syntax:
the Order by field name [asc | desc]; - asc ascending (default), desc descending

select * from test_student group by c_id;
select * from test_student order by c_id;

Here Insert Picture DescriptionSorting can sort multiple fields: first sorted according to a field, and then sorted the contents, and then re-sorted according to certain data.

Multi-field sorting, first class, gender after
the SELECT * from tese_student the Order by c_id, Sex desc;
Here Insert Picture DescriptionLimit clause
Limit clause: the result is a limitation of the statement: limit the number of
limit used in two ways
1, Option One: only to limit the length (data size): limit the amount of data;
query student: two front
SELECT * from limit test_student 2;
Here Insert Picture Description2, scheme II: starting position limits, limit the number: limit the starting position, length;
query students: the first two more

select * from test_student limit 0, 2; - the number of records numbered starting from 0

select * from test_student limit 1,2; - number of records are numbered from 1
Here Insert Picture Description
limit Scheme 2 is mainly used to implement paging data: to save time for the user to submit a response efficiency of the server, reduce the waste of resources.
For the user: You can click on the paging buttons: 1,2,3,4
For servers terms: to get the user to select the page number unused data: limit offset, length;

length: the amount of data per page: substantially unchanged;

The amount of offset = (p -1) * per page: Offset

review

Column properties: a primary key, since the growth unique key
relationships: one to one, one to many and many to many
paradigms: Layer Model
1NF: field must meet the atomic
2NF: partially dependent on (no composite primary key) is not present
3NF: absent transitive dependencies (a separate entity to build the table)
denormalization: Game efficiency and disk space

Advanced data manipulation
new operation: primary key violation (update and replace), worm copies
update: limiting the number updates: limit
the deletion: limit the number of deleted: limit, clear the table (truncat)
query: select options, field aliases, data source (single table, multiple tables and subqueries [alias]), WHERE clause (condition determination: disk), group by clause (packet statistics, statistical functions, packet ordering, multi-field packet, statistics back), HAVING clause ( Analyzing the results, statistical results for the packet), Order clause (sorting, sorting multiple fields), limit (limit number of records, tab)

Guess you like

Origin blog.csdn.net/weixin_44097082/article/details/94866696