MySQL语句速查

MySQL

C:\phpstudy_pro\Extensions\MySQL5.7.26\bin

登录:

mysql -u root -p

指定一个数据库:

use information_schema;

创建一个数据库:

create database test;

创建一个表:

create table test(

-> id int(10) not null primary key auto_increment,

-> name char(20) not null,

-> age int(8) not null,

-> sex char(20) not null

-> );

5.查看表:

show tables;

6.插入数据:

insert into test(name,age,sex) values('a','18','Male');

7.查询数据:

select * from test;

8.更改数据:

update test set name='abcde' where id=2;

9.删除数据:

delete from test where name='abcdef';

delete from test where id=5;

10.查询指定数据:

select * from test where sex='Male';

select name from test where sex='Male';

or:只要一个为真就是真

select name from test where sex='Male' or name=1;

select name from test where sex='Male' and name='a';

11.order by:

select * from test order by 4;

select * from test order by age desc; #降序

select * from test order by age asc; #降序




12.联合查询:

select name,age from test union select name from test2;

13.修改表名 : 
alter table test rename test1; 

 

14.添加表列: 
alter table test add  column name varchar(10); 


15.删除表列 :
alter table test drop  column name;  

 

16.修改表列类型 :
alter table test modify address char(10) 

||alter table test change address address  char(40)  


17.修改表列名:
alter table test change  column address address1 varchar(30)

18.**慎用**清空整个表的数据(但是保存列名,我的应用场景是清空我的测试数据,写入正式的数据)

truncate table 表名


















 

Tips:鼠标经过代码时会出现工具栏,工具栏上方有一键复制代码的功能哦~天云网络培训,专注于网络空间安全相关培训,并提供相关课程的在线培训 报名微信 tyedu1
天云网络培训 » MySQL语句速查

提供专业、易懂、平民化教育。让每个热爱网络的人有个归宿

立即查看 了解详情