day41, additions and deletions in the database search changed

                                      Database additions and deletions to change search   


I. Why use a database?
    
    Because using a file (Excel) data management before, but particularly large amount of data when using Excel management, then, is more of a trouble
    Therefore, the introduction of a new data management software: database software

II. Classification database?
    
    Relational Database
        
        1 . Constrained
         2. HDD-based storage (that is, the data stored on the hard disk, persistent === landing)
        
        Typical:
            MySQL oracle (SOEs) sqlserver (Microsoft) sqllite db2
        
    Non-relational data
    
        1. There is no constraint (Key ---> value)
         2 . Memory-based storage (put the data into memory)
        
        Typical:
            MemCache, redis(微博), mongodb
            
Three. MySQL architecture ::        
        
        Client:
            The client socket, connect the server to send commands (SQL statements)
        Server:
            socket server, the client receives the instruction, and returns the results to the client
        
        
Four. MySQL Installation

        Version: 5.5 5.7 or less
        
        1 executable file:
            .exe or .msi file
            Click Next.
            
        2 . Archive
        
            Decompression, into the bin directory
            
            mysqld: Start Services
            
            mysql: server connection
            
        3 . Configure environment variables
        

    ps:
        Work, MySQL does not need to develop their own line of equipment, operation and maintenance of equipment or DBA
        Exercise, need to manually install MySQL
        

        
Five Initialization:
        mysqld --initialise-secure
        
    
    Database (folder):
        Table (file)
           Data lines (one line content file)

Six operations on the database: ( ******************************** )
        
        1 . Database:
        
                increase:
                    SQL statement:  
                        create database database name;
                    example:
                        create database db1;
                    
                delete
                    drop database database name;
                    drop database db1;
                    
                modify
        
                    No special modification instruction update
                    
                    Deleted rebuild
                    
                Inquire
                    show databases;
                
                use:
                    use database name;
                    use db1;
        
        2 Data tables:
        
                New:
                    use db1;
                    
                    Version 0:
                        SQL statement:
                            create table 表名 (
                                Column name Column type 1
                            );
                        
                        example:
                            create table t1 (
                                id  int,
                                name char(32) 
                            );
                        
                        increase
                            instruction: 
                                insert into table (column 1, column 2) values ​​(value 1, value 2);
                            
                            example: 
                                insert into  t1 (id, name) values (1, 'zekai');
                                INTO T1 INSERT (ID, name) values ( 2, ' Hello ' );
                    
                    Improved 1:
                        create table 表名 (
                            Column name Column type 1
                        )engine=Innodb charset=utf8;
                    
                    ps: 
                        Engine: Innodb and MyIsam 
                        5.5 version of the above default Innodb
                        
                        create table t2 (
                            id  int,
                            name char(32) 
                        )engine=Innodb charset=utf8;
                        
                        INTO T2 INSERT (ID, name) values ( . 1, ' Hello ' );
                        insert into  t2 (id, name) values (1, 'xxx');
                    
                    Improved 2:
                    
                        create table 表名 (
                            Column name a type auto_increment primary key
                        )engine=Innodb charset=utf8;
                        
                        create table t4 (
                            id  int auto_increment primary key, 
                            name char(32)  not null  default ''
                        )engine=Innodb charset=utf8;
                        
                        auto_increment: increment
                        primary key: primary key index (effect: speed lookup)
                        not null: can not be empty
                        default: default value
                        
                        Note: After a finished back, not a comma ( ********* )
                        
                        One of:
                            INTO T3 INSERT (ID, name) values ( . 1, ' Hello ' );
                            insert into  t3 (id, name) values (2, 'xxx');
                        Two kinds:
                            insert into  t3 (name) values ('hello');
                            insert into  t3 (name) values ('xxx');
                        
                    
                    -------------------------------------------------------------
                    
                    The final format:
                        create table 表名 (
                            An attribute column [Default is null],
                            Column 2 Property [Default is null],
                            .....
                            Attribute column n-th column [Default is null]
                        ) Engine = storage engine charset = Character Set

                    The final example:
                        create table t4 (
                            id  int auto_increment primary key, 
                            name char(32)  not null  default '',
                            pwd  char(32)  not null  default ''
                        )engine=Innodb charset=utf8;
                    
                    View: 
                        instruction:
                            select the column name from the table name;
                        example:
                            select * from t1;
                
                    
                    Column Type:
                        
                        a. Numeric
                            create table t4 (
                                id  unsigned mediumint auto_increment primary key, 
                                name char(32)  not null  default '',
                                pwd  char(32)  not null  default ''
                            )engine=Innodb charset=utf8;
                            
                            tinyint : 
                                range:  
                                    Signed: - 128-127
                                    Unsigned: 0 to 255   unsigned
                            smallint
                                range:  
                                    Signed: - 32768-32767
                                    Unsigned: 0 to 65535   unsigned
                            
                            mediumint
                                range:  
                                    Signed: - 8388608-8388607
                                    Unsigned: 0 to 16777215   unsigned
                            int
                            bigint
                            
                            the difference: 
                                a. the range is not the same, come and choose according to their company's business
                                b. unsigned and signed meaning
                            
                            float (M, D) float
                            decimal (M, D) is more accurate than the fixed-point float
                                
                                For example: 3.1415151519868789789 
                                float: 3.141515000000000000 
                                decimal: 3.1415151519868789789
                                
                                126.35
                                
                                M: total number of decimal place decimal ( 5 ,)
                                D: Several behind the decimal point decimal ( . 5, 2 )
                                
                                scenes to be used:
                                    For example, deposit salary salary: 6000.23 decimal (, 2 )
                                    
                        b. Type String
                            
                            char: fixed length char ( 32 value) of this column is 32 Advantages: speed disadvantages: waste
                            VARCHAR: variable length VARCHAR ( 32 ) Advantages: no waste space saving drawbacks: slow
                            
                            According to the company's own business come and go choose:
                                
                                create table userinfo (
                                    id  unsigned mediumint auto_increment primary key, 
                                    name varchar(128)  not null  default '',
                                    pwd  char(32)  not null  default '',
                                    create_time  datetime not null default  '1970-01-01 00:00:00'
                                )engine=Innodb charset=utf8;
                                
                            In general, if there is no 100 % certainty, we are using varchar ()
                            
                            
                            text: text range is relatively large, if the store a lot of characters, you can use this field
                                            
                        c. Time Types
                                date 2019-6-12
                                
                                Recommended datetime
                
                
                delete
                    instruction:
                        drop table 表名;
                        
                        Even with all the data in the table data will be deleted
                        
                        ps: at work, online database, this command will not let you use
                        
                    Example:
                        drop table t1;        
                
                Inquire
                    show tables;
                    
                    desc table name;: View table structure
                    
                    show create table table name: create procedure to view the table
                        
                About increment primary keys: (not the point)
                    show session variables like 'auto_inc%';
                    set session auto_increment_increment = 2;
                    
                    show global  variables like 'auto_inc%';
                    set global auto_increment_increment = 2;
                    
                
                modify
                    create table t4 (
                        id  int auto_increment primary key, 
                        name char(32)  not null  default '',
                        pwd  char(32)  not null  default ''
                    )engine=Innodb charset=utf8;
                    
                    Modify the field:
                        alter table table name (t3) change the original column name (name) a new column name (username VARCHAR ( 32) Not null default '' );
                    
                    New field:
                        alter table 表名(t3)  add  新列(pwd char(32)  not null  default '');
                        
                    Delete field:
                        alter table table name (t3) drop column name (pwd);

        
        3 . Data lines:
                
                increase
                    INTO T3 INSERT (ID, name) values ( . 1, ' Hello ' );
                
                Inquire
                    
                    the SELECT * from T3;: all of the columns in the table List All
                    select the column name, column name, column name from T3: find out the value of a column
                
                delete
                    
                    the Delete from the table name (t3); all of the data in the table removed, when added again, continue to be a continuation of the ID
                    
                    truncate table name (t3); all of the data in the table removed, added again when, ID will re-start
                    
                    truncate speed
                    
                    ps: at work, online database, this command will not let you use
                    
                    delete from 表名(t3) where name = 'xxxxx';
                    
                    
                modify
                
                    update t3 set username='zekai';
                    
                    update t3 set username='xxxx'  where  id=3;
        
                    update t3 set username='xxxx', pwd='xxxxx'  where  id=3;
        
        

        
Seven foreign key: ( **************************** ************************************************************ )
        
        Disadvantages:
            1 Data Repeat
             2 . If the department is too long, too much space
            
        
        Solution:
            
            Re-design a table, this table stores information related sectors
        
        
        Sector Table:
        
            create table department (
                id  int auto_increment primary key, 
                depart_name varchar(32)  not null  default ''
            )engine=Innodb charset=utf8;
            
            INTO Department INSERT (depart_name) values ( ' PR ' ), ( ' OFF OFF ' ), ( ' Guan ' );
            
            create table userinfo (    
                id  int auto_increment primary key, 
                name varchar(32) not null default '',
                depart_id int not 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;
            
            
            insert into userinfo (name, depart_id) values ('root1', 1);
            INTO UserInfo INSERT (name, depart_id) values ( ' root2 ' , 2 ); Wrong
            
            note:
                Creating multiple foreign key when the name can not be the same
            
            =====> many
        

Guess you like

Origin www.cnblogs.com/WBaiC1/p/11013234.html