mysql语句总结
一:数据库的操作.show databases; // 查看所有的数据库
create database view; //创建数据库。
use view; //切换数据库
show create database view; //查看创建数据库的属性
select database(); ///使用函数查看当前在哪个数据库
drop databaseview; //删除数据库
二:表的操作。
show tables; //列出数据库中所有的表
helpcreate table //查看创建表的帮助
create tabletable1(ID int(12)); //创建表,但前提必须要有一个字段才可以创建表
show create table table1; //查看所创建表的属性
desc table1; //查看表的结构
alter table table1rename table2; //修改表名
drop table table1; #删除表
三:字段的操作
alter table table1 add name char(10); //向表中添加一个字段。
alter table table1add sex char(10)after> alter table table1add addrchar(10) first; //添加字段到第一个
alter table table1changeaddr address char(12); //修改字段的名称,change可以修改字段名称以及属性,
alter table table2 drop address; //删除字段
alter table table1 modifyaddress int(12); //修改字段的属性,modify只能修改字段的属性,不能修改字段的名称。
四:记录的操作
insert into table1set> insert into table2 values(2,'male','qiu',99); //向表中每个字段添加数据,一一对应,不能掉字段
insert into table2(ID,name,linux) values(2,'qiu',99); //向表中每个字段添加数据,可以自己定义,有的字段可以没有值
insert into table2(ID,name,linux) values(2,'qiu',99),(3,'wan',88); //向字段中添加多条记录
select*fromtable1; //查看表中的所有数据
select> select name,linuxfrom table2 order by linux ; //指定字段按从小到大的顺序查看Linux的成绩,默认是按添加的顺序排序。
select name,linuxfrom table2 order by linux desclimit 3;(只显示前三名)//逆序查看指定字段的数据,从大到小
select sex,count(sex)fromtable2 group by sex; //分组和统计,统计男女生有多少个人,以sex这个字段为一组进行统计
select sex,count(sex) fromtable2; //统计总共有几个记录
select sum(linux) fromtable2; //求指定字段的和
select avg(linux) fromtable2; //求指定字段的平均数
select name,chiness+math+Englishas total from table1; //求每个人的分数总和 as tiotal 是指定别名
select name,(chiness+math+English)/3as avg from table1; //求每个人的平均成绩 as avg 是指定别名
select name,linux from table2 where linux=(select max(linux) from table2); //子查询 select name,max(linux) from table2; 这种查询方法会出错,需要采用子查询
deletefromtable1 where name='mage'; //删除指定字段的数据
deletefromtable1; //删除整个表的数据
deletefromtable2 where>
deletefromtable2 where> deletefromtable1 where name is null; //删除匹配没有值的情况,其中is 可以换成
updatetable1 set name='mage'where name is null; //修改 字段为null的记录
updatetable1 set name='lisi'where name=''; //修改值为空数据的记录
updatetable1 set name='lisi'where name='mayun'; //将mayun的名字改为lisi
注:
+------+-------+
| > +------+-------+
|123 | mayun|
|123 | |
|123 | NULL|
+------+-------+
注:第三个ID中的123的NULL表示空值 ,第二个ID中的123 表示空数据
五:mysq支持模糊匹配和正则表达式查找
模糊匹配like
select from table2 where name like 'ha%'; //查找表中名字以ha开头的记录,通配符%表示所有,_表示单个字符
selectfrom t1 where name like '%o'; //匹配以o结尾的记录
正则表达式regexp
select from table2 where name regexp '^ha'; //采用正则表达式来查找名字以ha开头的记录
selectfrom table2where> selectfrom table2where>
delete from table2 where> select * from t1 where name regexp 'o$';
status; //查看数据库的状态
showprocesslist;//查看数据库中用户的连接状况,
show variables //用于显示mysql服务器的变量
show variables like '%port%';
数据库查找时: ;\g \G 的作用
;和\g都表示将查找的结果横向显示
\G 的作用是将查找的结果纵向显示
更改表之后刷新表的数据:flushprivileges;
在mysql中如果是真的为1,如果是假的就为0
页:
[1]