Getting Started with MySql (2) CRUD

############### CRUD operation in MySql#################### 
show create database tedu; 
create database db2 charset utf8; 
show create database db2; 
#Create person table 
use db2; 
create table person( 
    name varchar(50), 
    age int 
) charset utf8; 


/* Insert data (full table insert) */ 
    -- The order of insertion should be consistent 
    -- The type of insertion should be consistent 
    -- integers can be passed in as string types, but must be strings composed of pure numbers 
    -- if you determine the inserted value, you can use null 
select * from person; 
insert into person 
values ​​('Zhang San', 34); 

insert into person 
values ​​('Liu Bei',null); 

/* specify fingertip to insert records*/ 
insert into person(name) values ​​('Tom'); 
insert into person(name, age) values ​​('Luo Youquan' ,30); -- It is recommended to list the fields in the table in order


/* Full table insert multi-table records*/ 
insert into person values('Dafei',21), 
                         ('Erfei',18), 
                         ('Three Fei',16); 
insert into person(name) values ​​(' Villa'),('jack'),('lossi'); 


#person Insert record-- 
    if the insertion string contains single quotes, you can use two single quotes to convert it into a single quote-- 
    single quotes can prevent SQL injection 
create table user( 
    username varchar(50), 
    password varchar(50) 
)charset utf8; 

insert into user(username, password) values ​​('Zhang Zhang','234134'),('Li Xiaofeng','adfc') ,('Liu Zilong','fadfsdg'),('Wang Dafei','5464re'); 
select * from user where username='Li Xiaofeng' and password='adfc'; 

#but input username:xxxx  密码: 1' OR '1'='1
select * from user where username='xxxx' and password='1' OR '1'='1';
 
* Modify data*/
select * from person; 
update person set age=45 where name='Liu Bei'; 
update person set name='Guan Yu',age=42 where name='Tom'; 

/* delete data*/ 
delete from person where name=' lossi'; 
delete from person where age<20; 


/* data type*/ 
-- value, date, string 
-- value type tinyint smallint int[commonly used] bigint float doubl[commonly used] decimal(m,n)[commonly used] 
drop table tb1; 
create table tb1( 
    a tinyint, 
    b smallint, 
    c int, 
    d bigint(4), -- specify the display width, you can not specify 
    e float, 
    f double, 
    f1 double(6,4), -- also Can be displayed like decimal, but not strict, generally do not write 
    g decimal(6,4) -- strictly control the number of digits 
) 

insert into tb1(f1) values ​​(22.33344); 
insert into tb1(g) values ​​(22.33344);
insert into tb1(f1) values ​​(222.33344); 
insert into tb1(g) values ​​(222.33344); 
select * from tb1; 

/* String type 
            fixed-length character string, up to 255, if the insertion length is insufficient, it will be automatically filled, if it exceeds Assignment failed, 
           variable length string varchar; length must be specified; 
            -- char is more efficient than varchar; 
            text; 65535 bytes; 
*/ 

/* date type 
    timestamp timestamp, will automatically record the inserted time 
  
    YY-MM- DD 
    HH:mm:SS 
  */ 

create table tb3( 
    a time, 
    b date, 
    c datetime, 
    d timestamp 
); 
select * from tb3;  
insert into tb3(a) values ​​(' 21:51:00');
update tb3 set b='2023-06-11' where a='21:51: 00';


 

Guess you like

Origin blog.csdn.net/u010655348/article/details/131151451