# By the Create Database Review; # check Show Databases; Show create database review; # View to create a database, the default is CHARACTER SET latin1 encoding # change # is no specific change operation, delete the reconstruction alter database review charset utf8; # the database changed the character encoding utf8 encoding # delete drop database review;
use review; # 增(创建) create table test ( id int auto_increment primary key, name varchar(32) not null default '' )engine=Innodb charset=utf8; create table test_join ( id int auto_increment primary key, course varchar(32) not null default '', name_join int not null default 0, constraint fk_name_id foreign key (name_join) references test(id) )engine=Innodb charset=utf8; # 删 drop table test; alter table test drop name; # 删除列/字段名 # 改 alter table test rename new_test; # 更改表名 alter table test change/modify name title varchar(32) not null default ''; # 修改字段名 alter table test add title varchar (32 ) not null default ''; # add a new column # investigation show tables;
# Increase INSERT INTO Test (name) values ( 'wangyong'), ( 'Liguo'), ( 'jiyuzhi'); # delete Delete from Test WHERE id =. 3; Delete from Test; After # deleted, added data id directly increase truncate test; after # deleted, added data will clear id # change update test set name = 'title' ; # the value of the name attribute to all title Update Test SET name = 'title' WHERE id = 2; the id # = name of the attribute value to 2d title # check SELECT * from Test; SELECT name from Test;
WHERE # SELECT * WHERE ID from Test> ID. 1 and <=. 3; # BETWEEN closed interval and SELECT * WHERE ID BETWEEN from Test. 1 and. 3; # in / in Not SELECT * WHERE ID in from Test (l, 2,3 ); # wildcard SELECT * WHERE from Test name like '%% Wang'; SELECT * from Test WHERE name like '% wang_'; # limit table name SELECT * from limit index offset, the number of data extraction; SELECT * from test where id limit 2,3; # 2 from No. (number 3) start inquiry 3 # group by packet # select age, aggregate functions (count (num) / sum ( num) / max (num) / min (num) / avg (num )) from table group by the column name; SELECT name, COUNT (name) from Test group by name; SELECT name, COUNT (name) from Test group by name HAVING name = 'wangyong'; # secondary screening # order by ordering select * from test order by name desc ; # desc: descending select * from test order by name asc ; # asc: Ascending # even table (left the Join, right the Join, Inner the Join) # left the Join table on the left displays all right no used is not displayed select test.name, test_join.course from test left join test_join on test.id = test_join.name_join;
2.1 SQL injection
The reason: I believe that all the data entered by the user
Solution:
-
Their escape to manually input by the user judgment data
-
Do not splice SQL statement using the execute method PyMySQL, preventing SQL injection
pymysql Import Gender = the INPUT ( 'Gender >>>:') sname = the INPUT ( "sname >>>:") # linked server conn = pymysql.connect (host = 'localhost ', user = 'root', password = ' ', Database =' practiceday43 ', charset =' utf8 ') the Cursor = conn.cursor (the Cursor = pymysql.cursors.DictCursor) # write SQL statements, SQL statements do not go splicing sql = "select * from student where gender =% s sname =% S and " # pass value, the tuple in the form of a reference to the SQL pass the cursor.execute (SQL, (Gender, sname)) RES = cursor.fetchone () Print (RES) IF RES: Print ( 'obtained value success') the else: Print ( 'failure to obtain value') the Cursor.close() conn.close()
2.2 Transaction
Four characteristics:
Atomic: a set of operations either all succeed, or all fail Consistency: before and after the operation of the operation, the total amount is consistent Isolation: The operating procedure for the other things, the transaction is no effect of persistence: when we commit / rollback, the impact has been in force, compensating transactions to solve
use
Open: start transaction operation of a set of SQL statements to complete (commit / rollback)
Create Table User ( ID int AUTO_INCREMENT Primary Key, name VARCHAR (32) Not null default '', Money int Not null default 1000 ) Engine = InnoDB charset = UTF8; INSERT INTO User (name, Money) values ( 'wangyong', 1000 ), ( 'Liguo', 1000); # normal operation Start Transaction; Update User SET Money = 1100 is WHERE name = 'wangyong'; UPDATA User SET Money = 900 WHERE name = 'Liguo'; # unusual ROLLBACK; # final result The data has not changed MySQL> the SELECT * from the User; + ---- + ---------- + ------- + | the above mentioned id | name | Money | + ---- + + ------- + ---------- | 1 | wangyong | 1000 | | 2 | Liguo | 1000 | + ---- + ---------- + ------- +
3. Index
3.1 Role: speed up queries
3.2 Classification and creation:
Primary key index: primary key
# 第一种: create table test( id int auto_increment primary key, name varchar(32) not null default '' )engine=innodb charset=utf8; # 第二种: create table test( id int not null default 0, name varchar(32) not null default '' )engine=innodb charset=utf8; alter table test change id id int auto_increment primary key;
# First Create Table Test ( ID int Primary Key AUTO_INCREMENT, name VARCHAR (32) Not null default '', UNIQUE the ix_name (name) ) = InnoDB Engine charset = UTF8; # create unique index Index Name second table on (name); Create Table Test ( ID int AUTO_INCREMENT Primary Key, name VARCHAR (32) Not null default '' ) Engine = InnoDB charset = UTF8; create unique index the ix_name ON Test (name); # Create joint unique index create unique index index name on table name (name, Age); Create table Test ( ID int Primary Key AUTO_INCREMENT, Age int Not null default 0, name VARCHAR (32) Not null default '' )engine=innodb charset=utf8; create unique index ix_name_age on test (age, name);
# 第一种 create table test( id int auto_increment primary key, name varchar(32) not null default '', index ix_name (name) )engine=innodb charset=utf8; # 第二种 create table test( id int auto_increment primary key, name varchar(32) not null default '' )engine=innodb charset=utf8; create index ix_name on test (name); # 联合索引 create table test( id int auto_increment primary key, age int not null default 0, name varchar(32) not null default '' )engine=innodb charset=utf8; create index ix_name_age on test (age, name);
explain selext * from test; +----+-------------+-------+-------+---------------+---------+---------+------+------+------------- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra +----+-------------+-------+-------+---------------+---------+---------+------+------+------------- | 1 | SIMPLE | test | index | NULL | ix_name | 98 | NULL | 1 | Using index +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------
- not recommended like search - most left-prefix composite index if the index is a combination of: (name, Email) the WHERE name and Email - using an index where name - using an index where email - do not use indexes
# drop 索引名称 on 表名 drop index ix_name_age on test; mysql> explain select * from test; +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | 1 | SIMPLE | test | ALL | NULL | NULL | NULL | NULL | 1 | NULL | +----+-------------+-------+------+---------------+------+---------+------+------+-------+
Innodb (support high concurrent) 1. (default version includes 5.5) 2. Support Services 3. does not support full-text indexing 4. index and data are in the same file, the structure of reality .ibd table .frm file MyIsam (support high concurrency is not particularly good) 1 (default version 5.5 or below 5.3) 2. does not support transactions 3. support full-text indexing 4..frm: table structure .MYD: table data .MYI: table index full-text indexing :( Chinese well) sphinx