Python Day41

initialization:

  mysqld --initialise-secure

  Database (folder) list (file) data lines (one line content file)

Database operations:

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 instructions updata

    Deleted rebuild

  Inquire

    show databases;

  use

    use database name;

    Examples: use db1;

Data Table 2:

  New:

    use db1;

  Version 0:

    SQL statement:

      create table table name (Column name 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,“jack”)

        insert into t2(id,name) values(2,“owen“)

  Improved 1:

    create table table name (Column name Type 1) engine = Innodb charset = utf8;

  ps:

    Engine: Innodb and MyIsam

    5.5 version of the above are the default Innodb

    create table t1(id int,name char(32))engine=Innodb charest = utf8;

    

    insert into t2 (id, name) values (1, '你好');
    insert into t2 (id, name) values (1, 'xxx');

Improvement 2:

Create Table table name (
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: not null
default: default value

Note: after you back a finished, not a comma (********* )

one kind:
INSERT INTO T3 (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');


-------------------------------- -----------------------------

final format:
create table table (
column 1 Property [Default is null],
column 2 Property [Default is null],
.....
row n-th column attribute [Default is null]
) = Engine Storage Engine charset = character set

The final example:
Create Table T4 (
ID int Primary Key AUTO_INCREMENT,
name char (32) Not null default '',
pwd char (32) Not null default ''
) = Engine Innodb charset = UTF8;

View:

Commands:
SELECT column names from table;
example:
SELECT * from T1;


column types:

. A numeric
Create Table T4 (
ID unsigned MEDIUMINT AUTO_INCREMENT Primary Key,
name char (32) Not null default '',
pwd char (32) default null not ''
) = Engine Innodb charset = UTF8;

tinyint:
range:
signed: -128 to 127
unsigned: 0 to unsigned 255
smallint the
range:
signed: -32768 to 32767
unsigned: 0 to unsigned 65535

MEDIUMINT
range :
signed: -8388608 to 8388607
unsigned: 0 to unsigned 16777215
int
BIGINT

differences:
. a different ranges, according to their company's business choice come
b unsigned and signed meaning.

a float (M, D) float
decimal (M, D) than the fixed-point type float more precise

example: 3.1415151519868789789
float: 3.141515000000000000
decimal: 3.1415151519868789789

126.35

M: total number of decimal digit decimal (. 5,)
D: Several behind the decimal point decimal (5, 2)

be used:
for example said deposit salary pay: 6000.23 decimal (, 2)

B string type.

char: this column values of a given length char (32) is 32 advantages: speed disadvantages: waste
VARCHAR: variable length VARCHAR (32) Pro: waste, space-saving drawbacks: slow

according to their company's business come selection:

Create Table UserInfo (
ID Primary Key unsigned MEDIUMINT AUTO_INCREMENT,
name VARCHAR (128) Not null default '',
pwd char (32) Not null default '',
the create_time datetime default null not '1970-01-01 00:00:00'
) = Engine Innodb charset = UTF8;

in general, if there is no 100% certainty, are used VARCHAR ()


text: text relatively large range, if the large number of characters stored, you can use this field

c time type.
DATE 2019-6-12

recommended datetime


delete
instruction:
drop Table name;

even with all data in the data table are deleted

ps: at work, online database, this command will not let you use the

example:
drop the table T1;

inquiry
Show the tables;

desc table name;: View table structure

show create table table name: create procedure to view a table

on the primary key increment: (not focused)
Show the session Variables like 'auto_inc%';
SET the session auto_increment_increment = 2;

Show Global Variables like 'auto_inc%';
SET Global auto_increment_increment = 2;


modified
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;

editable 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 table name ( t3) add new columns (pwd char (32) not null default '');

delete field:
ALTER table table name (t3) drop column name (pwd);


3. Data lines:

increasing
insert into t3 (id, name) values (1, ' Hello');

query

select * from t3;: all of the columns in the table are listed all the
select column names, column names, column names from t3: the value of a column. isolated

delete

delete from table name (t3); deletes all the data in the table out, once again, when added, will continue on a continuing ID

TRUNCATE table name (t3); the table All data deleted, added again when, ID will re-start

truncate speed

ps: work, online database, this command will not let you use the

delete from table name (t3) where name = 'xxxxx ' ;


modify

Update SET username = T3 'Zekai';

Update SET username = T3 'XXXX' WHERE ID =. 3;

Update SET username = T3 '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 sector information


department table:

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 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 ( 'the root1',. 1);
INSERT INTO UserInfo (name, depart_id) values ( 'root2' , 2); wrong

Note:
Creating multiple foreign key, the name can not be the same

 

Guess you like

Origin www.cnblogs.com/xinfan1/p/11011850.html