mysql data type integrity constraints 054

mysql data type integrity constraints 054

Create users and authorize user permissions:

# 1. Create user: 
# Specify the fgf user with ip 192.168.15.109 to log in
 create user ' fgf ' @ ' 192.168.15.109 ' identified by ' 12 ' ; # Specify the fgf user with
 
ip 192.168.15.1 to log in create user ' fgf ' @ ' 192.168.15.% ' identified by ' 123 ' ; 
# Specify any ip fgf user login create user ' fgf ' @      
  ' % ' identified by  ' 123 ' ; 
# 2 . Delete user
 drop  user  ' username ' @ ' IP address ' ; 
# 3 . Modify user 
rename user  ' username ' @ ' IP address '  to  ' new username ' @ ' IP address ' ; 
# 4 .
Modify password set password for  ' username ' @ ' IP address' = password( ' new password ' ); 

Authorization management for current user 
View permissions 
show grants for  ' username ' @ ' IP address ' 
# Authorize fgf users to only query, insert and update db1.t1 files 
grant  select , insert , update  on db1 to  ' fgf ' @ ' % ' ; 
# Indicates all privileges, except for the grant command, which is only available to root, fgf users have arbitrary operation permissions on t1 files under db1;
 grant  all  privileges  on db1 .t1 to  ' mjj' @ ' % ' ; 
#fgf users perform any operations on files in the db1 database 
grant  all  privileges  on db1. *  to  ' fgf ' @ ' % ' ; 
# fgf users perform any operations on all database files 
grant  all privileges on  * . *  to  ' fgf ' @ ' % ' ;

copy table

# Copy table structure and copy records
 create  table t2 select  *  from db1.t1; 

# Only copy table structure, not copy records 
create  table t2 select  *  from db1.t1 where  1 > 3 ;
 create  table t2 like db1.t1;

Data type : Integer is signed by default 

  Data type unsigned (unsigned) and signed zerofill with 0

  The role of constraints: to ensure the integrity and consistency of data

    tinyint[-128~127] small integers can be obtained on both sides

    int integer

    bigint extremely large integer

# Added unsigned means starting from 0 and no longer starting from negative numbers
 create  table t1(id int ( 4 ) unsigned,name char ( 20 ));

floating point 

  float single precision becomes less and less accurate as the number of decimal places increases

    float[(M,D)] [UNSIGNED] [ZEROFILL]

    Parameter explanation: single-precision floating-point number (non-accurate decimal value), M is the full length, D is the number after the decimal point, the maximum value of M is 255, and the maximum value of D is 30

  Double double precision will become more and more inaccurate as the number of decimals increases, but it is more accurate than float

    DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL] Letters have the same meaning as float.

  decimal exact decimal

    decimal[(m[,d])] [unsigned] [zerofill]

      #Parameter explanation: accurate decimal value, M is the total number of integer parts (negative signs are not counted), D is the number after the decimal point. The maximum value of M is 65, and the maximum value of D is 30.

        #Accuracy:  **** Accuracy is always accurate as the number of decimals increases ****

             This type is required for exact numerical calculations

             The reason why ecaimal can store precise values ​​is that it is internally stored as strings.

Date type: DATE TIME DATETIME TIMESTAMP YEAR

    The role is to store user registration time, article release time, employee entry time, birth time, expiration time, etc.

    YEAR Year (1901~2155)

    DATE 

    TIME hours minutes seconds 

    now() The content function that comes with the sql language: Get the current event time (according to the data type)

    create table t10(born_year year,intClass datetime);

character type 

    char fixed length, simple and rude, waste of space but fast access speed 

    Varchar variable length accurately saves space but slow access speed

length(): View the number of bytes 
char_length(): View the number of characters

  Enums and Collections

 create  table consumer( 
     id int , 
     name varchar ( 50 ), 
     sex enum( ' male ' , ' female ' , ' other ' ) default  ' male ' ,
      level enum( ' vip1 ' , ' vip2 ' , ' vip3 ' , ' vip4 ' ),#In the specified range, choose more than one 
     fav set ( 'play ' , ' music ' , ' read ' , ' study ' ) # Within the specified range, multiple selections 
    );

    Note: use tinyint(1) in sql to represent boolean type

Integrity constraints

  not null and default

    If you set not null alone, you cannot insert a null value

    If not null is set and default is specified, a null value can be inserted and default will be executed

  unique key

    Single row unique

create table t4(
    id int not null,
    name char(20) unique
);

create table t4(
    id int not null,
    name char(20),
    unique(name)
);
insert into t4(id,name) values(1,'alex');
insert into t4(id,name) values(1,'wusir');

    Multi-column unique As long as one column is the same, it cannot be inserted (similar to and)

create table t5(
    id int,
    name char(20),
    unique(id),
    unique(name)
);

    union unique cannot insert when all are the same (similar to or)

create table t6(
    id int,
    name char(20),
    unique(id,name) 
);

    Application Scenario Course Selection System A student can choose multiple courses A course can be chosen by multiple students

primary key

  Chemical reaction: not null + unique   

  A single-column primary key cannot be empty and is unique

# primary  key index (for a large amount of data) query speed is faster
 create  table t7( 
    id int  primary  key , 
    name varchar ( 10 ) unique 
); 

create  table t8( 
    id int  not  null  unique , 
    name varchar ( 10 ) unique 
);

  combined primary key  

create table t9(    
    id int,    
    name varchar(10),
    primary key(id,name)
);

auto_increment constraint: the constrained field is an auto-increment constrained field must be constrained by key at the same time

create table student(
    id int primary key auto_increment,
    name varchar(20) not null,
    sex enum('male','female') default 'male', 
    ip varchar(20) unique
);

insert into student(name,sex,ip) values 
('alex','female','127.0.0.5'),
('wusir','male','173.45.32.1');

  Clear the table to distinguish the difference between delete and truncate:

    delete from t1 : If there is self-incrementing id information, the data still starts with the last one before deletion

    truncate table t1: It is suitable for deleting a large amount of data, and the deletion speed is faster than the previous one, and it starts directly from zero.

foreign key

  Foreign key is to associate two tables 

# Create the main table
 create  table dep( 
    # The main table id is set to automatically increase 
    the id int  primary  key auto_increment, 
    # Set the department name as a single column unique 
    name char ( 10 ) unique , 
    # Department description cannot be empty 
    dep_desc varchar ( 50 ) not  null 
); 

# create slave table 
create  table emp( 
    eid int  primary  key auto_increment, 
    name char ( 10 ) not  null , 
    ageint not null,
    dep_id int,
    constraint fk_dep foreign key(dep_id) references dep(id)
    on delete cascade
    on update cascade,
);



create table emp(
    eid int primary key auto_increment,
    name char(10) not null,
    age int not null,
    dep_id int,
    constraint fk_dep foreign  key (dep_id) references dep(id) 
     on  delete  cascade  
    on  update  cascade , 
); 

insert  into dep(name,dep_desc) values 
    ​​( ' board of directors ' , ' management company department ' ), 
    ( ' public relations department ' , ' PR Management Department ' ), 
    ( ' IT Department ' , ' IT Management Department ' ), 
    ( ' Finance Department ', ' Financial Management Department ' ); 
    
insert  into emp(name,age,dep_id) values 
    ​​( ' alex ' , 18 , 1 ), 
    ( ' wusir ' , 30 , 2 ), 
    ( ' Boss Wu ' , 20 , 3 ), 
    ( ' Boss Ma ' , 18 , 4 ), 
    ( ' Boss Qiu ' , 20 , 2 ), 
    (' goddess ' , 16 , 3 );

 

posted @ 2018-11-22 00:47 You are not as important as you think Read ( ... ) Comment ( ... ) Edit Favorites

Guess you like

Origin blog.csdn.net/bruce_van/article/details/89443043