python database infrastructure

1. Use the data type :( principles: enough on the line, the range of possible small) 

integer: int, bit
decimal: decimal
string: VARCHAR (variable length), char (fixed length character string)
Date Time: date, time , datetime
enumerated type (enum)
special information
2 constraint (limit)
main:
primary key primary key: physical storage order
non-empty allowed to fill in the spaces not null
unique unique: can not be duplicated
default default: default data item
foreign key foreign key: storing the primary key of another table fields
other:
unsigned unsigned
auto increment increment
4. common terminology:
database: the database is a collection of related tables.
Data: Table are matrix data. In a database table it looks like a simple spreadsheet.
Column: one (data element) contain the same data, such as a zip code.
Rows: one (= tuple or record) is a group of related data, such as a user subscription data.
Redundancy: store twice as much data redundancy reduces the performance, but to improve the security of the data.
Primary key: Primary key is unique. A data table can contain only one primary key. You can use the primary key to query the data.
Foreign Key: foreign key for associating the two tables.
Composite key: a composite key (key combination) the index key as a plurality of columns, typically used in the composite index.
Index: using the index can quickly access specific information in a database table. Index value in the database is one or more columns in the table is sorted a configuration. Catalog of books similar.
Referential integrity: the integrity of the relationship is not allowed in reference to the requirements referenced entity does not exist. Integrity is the entity relationship model integrity constraints that must be met, the purpose is to ensure data consistency.
5.Mysql database engine:
Innodb support transactions and row-level locking (for locking the entire data element when the data element modified)
MyISAM does not support more than

6. Common command-line commands and statements
Notes: "-"
in the parameter name later adding "~" to ensure the integrity of parameters without being mistaken for a variety of situations in which two parameters

into the client: mysql -u username -p password

display all databases: show databases;

show all the time: select now ();

display version: select version ();

create a database:
the default parameters create: create database database name
specifying data encoding formats: create database database name of the charset = encoding format name

to create View database (parameters): show create database name of the database has been created;

Delete the database: delete the database: drop database database name;

see the current database select database used ();

enter the specified database: use the database name;

create data tables create table table name (field type constraints [, field type constraint]);

View data table fields of the structure: desc name;

display all the names of the current database: show tables;

insert data into the specified table: insert into table values (field value sequence (the order defined by the table))
shows the structure of the table: show create table table name;
select all data specified table: select * from table name;
on time monitoring function: set profiling = 1;
shows the statement of historical queries and execution time: show Profiles;
7. modify data sheet:
Add fields: alter table data table add listed types;
modified field (non-renamed): alter table data table modify listed types and constraints;
modified field (rename): alter table data table change the original column name of the new column name types constraint;
remove field: alter table name of the table column names drop;
delete tables: drop table name;
8. modify data in the table According to:
Insert Data: insert into table values (field value sequence) primary key was incremented, the value can fill 0, null or default
partly inserted: insert into table (non-empty fields and other fields) values (field value sequence)
multiple insert line: sequences of the plurality of values bracketed by "," separated

delete data:
physical delete: delete from table [where condition]; Drop primary without conditions after deleting all data better not to use
tombstone: adding a flag field by field value update changed data
is generally used tombstone
modify data: update table set field name = field value where conditions; (where statements without modifying the table all the data)
query data:
the SELECT * from table name [where condition]; * means all fields (columns)
SELECT name field [as alias], field name [as alias] from table [where condition]; field name may be reversed

9. database query:
condition query:
Comparison operators> ,> =, <=,! =, = (<>)
logical operators and or
fuzzy inquiry: field name like conditions field values like
% Replacement of one or more name like "small%" ( "small" at the beginning of the word name field) "Small%" name field name like ending value "small" character
_ replace a name like "__" (name field value there must be two words) name like "__%" name field contains at least two characters
regular expression matching the query: rlike field names like regular expression string

range query:
in: field name in (set of values)
not in
the BETWEEN and: field name BETWEEN value 1 and value 2
not between and: field name not between the value 1 and value 2 (remember after can not not of between and bracketed to avoid an error)
iS whether null / is not null determination field value is null

10 the sorting: in the last query field name plus the default order by ascending descending desc, when sorted field values are equal, according to the primary sort key,
multiple sort conditions: 1 order by name field, 2 field names, field 3 were sorted according to the field value 2, so in the case of pushing the same field values of 1, if the last is equal to a reference value field, according to the primary sort key ascending

11. aggregation functions:
counting count: select count (*) [as alias] from table where conditions;
The maximum value max: select max (field names) from table [where Condition];
Total sum: select sum (field names) from table [where Condition];
minimum value min: select mini (field names) from table [where conditions];
average avg: select avg (field names) from table [where condition]; equivalent to select sum (field name) / count (*) from table [where conditions];
a retention after several decimal places round value (, median): select round (avg (field names), 2) from table [where condition];


12. grouping: must be used in conjunction with the polymerization, or no meaning.
group by field name: select field name [, aggregate functions ()] from the condition table where the field name first query group by sorting structure, each of the select statement and then execute each
GROUP_CONCAT (field names, [other characters, field name] ) with the group by using the configuration of several fields in the query results to group_cat () like brackets, and then displayed on a single line of the same group. Default separated by commas
having Condition: filter out packets with the condition is not satisfied with the use group by

13. Results quantity:
limit: Usually the final discharge
quantity limit
limit the initial position (zero), the number
14 join query:
The connector (intersected) inner join: select * from Table 1 inner join Table 2 on Table 1. Table 2 Field field =
external connection:
left connecting left join: to the left of the reference table join query
right connecting the Join right
15 .having and where the difference: the former determine whether conditions are met before the statement of the query results, the latter judgment in the original table.
16. The self-association: a field value of a table with another table uses field values
17. subquery: recommended to use less
nested query query condition other

design specifications 18. Table:
Paradigm:
a first field in each paradigm 1NF no longer split
second paradigm 2NF must have a primary key, the master key can have multiple fields, other than the primary key must rely on the primary key instead of the entire part of the primary key
of the third paradigm 3NF

19.python linked databases:
the overall steps of:
connecting to the database
to get a cursor objects
execute the statement is executed
to close the cursor object
close the connection

from pymsql impirt * # import module associated
connection name = connect (parameter list) connection object instance instantiation
Parameters:
Host: target mysql host to connect to ip, this machine is "localhost"
Port: target mysql host port to connect to the default 3306
Database: name of the database
user: username connection
password: password connection
charset: Communication adopted encoding, utf8 recommended
cursor name = connection instance name .cursor () # instantiate a cursor cursor object
result = number cursor name .execute (sql statement) # perform a sql statement and return the number of query result
cursor name .fetchone () # obtain a result of execution of the statement is executed on the strip
pieces results cursor name .fetchmany (number) # implemented statement
cursor name .fetchall


20. to use the connection CRUD example .commit (), undo the last execution examples of using the connection .rollback ()


21.mysql advanced:
view: the results in table 1 consisting of a query directly, or from several of the table. Simple to understand: a virtual table, isolated databases and program sql statement. View can not be updated, is virtual does not create an immediate execution, it will temporarily created when the view is used, so you can view the data in the update data with its associated table is updated
statement: create view query view name as
Transaction: a sequence of operations, these operations are either executed or not executed, is an indivisible unit of work.
mysql command-line default for each statement were opened affairs, pymsql not turned on by default in a transaction, you need to manually commit and rollback.
Four characteristics: ACID
Atomicity (Atomicity) the transaction is treated as the basic unit no longer divided, all operating sequences of the transaction are executed successfully and either committed or rolled back all failed
consistency (Consistency) is transferred from one consistent state consistent state to another, the transaction is executed successfully transferred into a state database, the unsuccessful maintain the original state.
Isolation (isolation) firm to make changes before final submission, is not visible to other transactions.
Persistent (Durablity) submit the transaction, if successful, will be made to modify their saved permanently to the database.
Use framework:
Method 1:
Start Transaction;
perform a sequence of operations;
commit;
Second way:
the begin:
operation sequence
commit;

index: a special kind of file, which contains all records of the data table reference pointer. Popular understood as a catalog, improve database query speed
create index index name on table name (field (length)) do not specify the length of the non-string
after joining the index, query the index will help significantly speed up the search
Objective: To improve query performance, analog dictionary
Principle: Through the establishment of the B-tree record the address of the organization and sorting in a query can be quickly located by some traversal algorithm and memorize
the primary and foreign keys are automatically indexed
view index: show index from table name;
delete index: drop index index name on table name;
disadvantages: It will increase the burden on the table insert and update
master from:




Guess you like

Origin www.cnblogs.com/burningcarbon/p/11220186.html