Beginner python road -day41

Operation of the database

The database can be seen as a folder, the table is a file, record, or data line is a line by line data content. 
1. First of all database operations: increase: create database database name;
# attention after the sql statement to be plus in English; the end of the examples create database db1; deleted: drop database database name; examples drop database db1; Review: No specific database level modify instruction, you can delete reconstruction query: show databases; use: use database name; use db1;
2 Data table operation: 
Note that the table data and numerical operations must rows under the premise of using a database, use the name of the database DB1 use

 2 .1 addition of new: 
Create table Table (Column name Type 1 1, 
column name 2 column type 2); 
Create table T1 ( 
ID int, char name ( 16 ) 
);

 2 .2 query: Show tables; 
desc table name; Display configuration 
See table creation process; show create table table name 
        query process if there can see the main sequence of the self-energizing self-energizing key: 
        Show the session Variables like ' auto_inc% ' ; 
    SET the session auto_increment_increment = 2 ; 
    Show Global   Variables like ' auto_inc% ' ; 
    SET Globalauto_increment_increment = 2 ; 
           you can see step the primary key of the self-energizing and provided

 2 .3 Review: 
modify the field: 
ALTER Table table name (t3) change the original column name (name) a new column names and their attributes 
(username VARCHAR (
32) Not null default ' ); new field: ALTER table table name (t3) add new columns (char pwd ( 32) Not null default ' ' ); delete field: ALTER table table name (t3) drop column name (pwd); 2 . 4 delete: drop table name; all data even with the deleted data table
3 . Data lines:
 3 .1 increased 
to increase 1: 
INSERT INTO table name (column 1, column 2) values (value 1, value 2); 
INSERT INTO T1 (ID, name) values ( 1, ' WW ' ); 
Improved . 1: 
Create table table name (column name a type 
) engine = Innodb charset = UTF8; 
engine: Innodb and MyISAM    5.5 or later default Innodb 
Create table T2 (ID int, name char ( 32 ) 
) engine = Innodb charset = UTF8; 

improvement 2: 
Create table table name (column name a Primary Key type AUTO_INCREMENT 
) Engine
= Innodb charset = UTF8; Create table T4 (AUTO_INCREMENT int Primary Key ID,
name char (
32) Not null default '' ) Engine = Innodb charset = UTF8; AUTO_INCREMENT: increment primary key: primary key index (effect: speed lookups) Not null: not null default: Default Final format: Create Table table name ( an attribute column [default is null], column 2 property [default is null], ..... n-th column attribute column [default is null] ) engine = storage engine charset = charset create T4 Table (AUTO_INCREMENT int Primary Key ID,
name char (
32) Not null default '' , char pwd (32) Not null default '' ) Engine = Innodb charset = UTF8; type column: a. Numeric tinyint: range: Signed: - 128-127 Unsigned: 0 to 255 unsigned smallint The: range: Signed: - 32768-32767 Unsigned: 0 to 65535 unsigned MEDIUMINT: range: Signed: - 8388608-8388607 unsigned: 0 Dao 16777215 unsigned int range bigint larger float (M, D) floating-point decimal (M, D) fixed-point precision than float more M: total number of decimal place decimal ( 5 ,) D: several decimal behind the decimal point ( . 5, 2 ) B string types:. char: fixed length char ( 32 values in this row) is 32 advantages: speed disadvantages: waste VARCHAR: variable length VARCHAR ( 32 ) advantages: no waste space saving disadvantage: slow text: text relatively large range, if the store a large number of characters, then, can use this field c time type. DATE datetime for increasing the data lines may also be: INSERT INTO T3 (ID, name) values ( . 1, ' Hello ' ), (2, ' Hello ah ah ' ) or the like
3 .2 query: 
select * from T3;: all of the columns in the table lists all of the 
select column names, column names, column names from T3: the value of a column found

 3 .3 Delete: 
the Delete from the table name ( t3); all of the data in the table removed, again adding 
additional time, continue to be a continuation of the previous ID TRUNCATE table name (t3); deletes all the data in the table out, again adding
additional time, ID will restart Delete
from table (t3) where the condition = name ' XXXXX ' ; . 3 .4 Review: Update T3 SET username = ' XXXX ' WHERE ID =. 3 ; Update T3 SET username = ' XXXX ' , pwd = 'xxxxx' where id=3;

Foreign key

Since if data is repeatedly establishing a plurality of tables or data is too long space, there is a foreign key 

to re-design a table, the corresponding relationship with the digital information stored in this table of 

specific code: 
Create Table Department ( 
ID int AUTO_INCREMENT Primary Key, 
depart_name VARCHAR ( 32)   Not null default '' 
) Engine = Innodb charset = UTF8; 
        
INSERT INTO Department (depart_name) values ( ' PR ' ), ( ' oFF oFF ' ), ( ' Guan ' ); 
        
Create UserInfo Table (     
ID int Primary Key AUTO_INCREMENT, 
name VARCHAR ( 32) Not null default '' , 
depart_id intNot null default. 1 ,
 # constraint foreign key name (fk_userinfo_depart) Foreign Key 
(column name (depart_id)) references the table name (Department) (column name associated (ID)),
constraint fk_userinfo_depart Foreign Key (depart_id)
References Department (ID) ) Engine
= Innodb charset = UTF8; Note that if the second table memory with a table of correspondence between you should first create a table or will be error INSERT INTO UserInfo (name, depart_id) values ( ' the root1 ' ,. 1 ) ; INSERT INTO UserInfo (name, depart_id) values ( ' root2 ' , 2 ); wrong Note: when creating multiple foreign key, the same name can not =====> many

 

Guess you like

Origin www.cnblogs.com/wangwei5979/p/11013205.html