mySQL learning introductory tutorial - 5. Commonly used advanced operations

Five, mysql commonly used advanced operations

 

1. MySQL table replication
create table t2 like t1; #Copy the table structure, t2 can learn all the table structures of t1
insert into t2 select * from t1; #Copy table data, but this will still have defects, because the columns are not considered , because the table structures of t1 and t2 are exactly the same, so this operation will not go wrong!
Suggestion:
insert into t3(name) select name from t1; #Specify the copied column

2. MySQL index
1. Create an index directly
create index index_name on table_name(column_list); #Create a common index
create unique index index_name on table_name(colume_list); #Create a unique index, please ensure that the column has no duplicate values ​​before creating a unique index, Otherwise, the creation is unsuccessful!

2. Directly delete the index
drop index index_name on table_name;

3. Modify - create an index
alter table table_name add index [index_name](colum_list); #Create a common index
alter table table_name add unique [index_name](column_list); #Create a unique index
alter table table_name add primary key [index_name](column_list) ; #Create a primary key index, if you do not add index_name, use column_list as the default index name

4. Modify-delete index
alter table table_name drop index index_name; #delete common/unique index
alter table table_name drop primary key; #delete primary key index

[Recommended use 3
, 4] 1. View the index: show index from t1 \G
2. alter table table_name modify id int not null; (I haven't found that modify the index with modify, it seems to be modified with add?)

3. MySQL view
view: Through a condition, a part of the data is extracted from a table to form an intermediate table, which is the view
Note: The view changes with the change of the main table
1. Create a view
create view view_name as select *from table_naem where id > 4 and id <= 10;

3. Check which views have been created
show tables; #The view is an intermediate table
3. Check the data in the view
select * from view_name; #Same as viewing table data

4. Delete the view
drop view view_name;

Fourth, MySQL built-in function supplementary
view function function and simple example: function_name
e.g. lcase;

1. String function
1) lcase("string")/ucase("string") #Convert to lowercase/uppercase, the same as lower(str)/upper(str)
2) length("string") #return character The length of the string
3) repeat("string",n) #repeat the character from n times
4) space(n) #generate n spaces

2. Mathematical functions
1) bin(decimal_number) # decimal to binary
2) ceiling(n) # has the same function as ceil, rounded down
3) sqrt(n) # square root
4) max(col)/min(col) #Take the maximum/minimum value, use
5) rand() #Generate random numbers
select * from table_name order by rand(); #Use the rand function as the sorting benchmark

3. Date function
1) datediff(expr1, expr2) #Return the number of days between expr1 and expr2, if expr1> expr2, return a positive value

 

 ****************************Comparative Advanced Section******************** ****************

Five, MySQL prepared statement
1. Set the preprocessing stmt, and pass a data as the judgment condition of where
prepare stmt from "select * from table_name where id > ?";

2. Set a variable
set @i = 1;

3. Execute the prepared statement
execute stmt using @i;

4. Delete the preprocessing instruction
drop prepare stmt;

6. MySQL transaction processing
[Note] The MyISAM storage engine does not support transactions, and the InnoDB storage engine should be used

set autocommit = 0; #Turn off autocommit  
delete from t1 where id > 4;  
savepoint p1; #Set the restore point  
delete from t1;  
rollback to p1; #rollback to p1 restore point  
rollback; #roll back to the original restore point  
commit ; ​​#Submit data to the server  
set autocommit = 1; #Turn on autocommit, turn off transaction processing  

Seven, MySQL storage
1. Create a storage p1()

mysql>\d // #Modify the delimiter to //  
mysql>create procedure p1()  
        ->begin  
        ->set @i = 0;  
        ->while @i < 100 do  
        ->insert into t2(name) values(concat("user",@i));  
        ->set @i = @i + 1;  
       ->end while;  
       ->end;//  
mysql>\d ;  

 

2. Execute p1()
call p1();
3. Check the status information of the
procedure show procedure status \G

4. View the specific information of
procedure p1 show create procedure p1 \G

Eight, MySQL trigger
1, create a trigger
# Create a trigger named t1, when inserting data into the t1 table, it will trigger an action: insert data into the t2 table

mysql>\d //  
mysql>create trigger t1 before inserton t1 for each row  
        ->begin  
        ->insert into t2(name) values(new.name);  
        ->end//  
mysql>\d ;  

 

#Create trigger t2, if table t1 deletes data, trigger the trigger, and the data in table t2 is also deleted accordingly

mysql>\d //  
mysql>create trigger t2 before delete on t1 for each row  
       ->begin  
      ->delete from t2 where id =old.id;  
      ->end//  
mysql>\d ;  

 

#Create trigger t3, if you modify table t1, the records in t2 will be modified accordingly

mysql>\d //  
mysql>create trigger t3 before update on t1 for each row  
        ->begin  
        ->update t2 set id =new.id where id = old.id;  
        ->end//  
mysql>\d ;  

 

2. Delete trigger
drop trigger trigger_name;

[Attachment] Delete all data in the table: truncatetable_name; #It is faster, and it can also clear the auto_increment list

9. Rearrange the auto_increment value
How to restore the auto-incremented ID in MySQL?
1. When emptying the table, do not use delete from table_name;
instead: truncate [table] table_name;

Or
2. Use the alter command to modify the table directly after clearing the content:
  alter table table_name auto_increment = 1;

 

http://blog.csdn.net/zjf280441589/article/details/20217647

http://blog.csdn.net/zjf280441589/article/details/20230427

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327080145&siteId=291194637