一:数据库的操作.
show databases; // 查看所有的数据库
create database view; //创建数据库。
use view; //切换数据库
show create database view; //查看创建数据库的属性
select database(); ///使用函数查看当前在哪个数据库
drop database view; //删除数据库
二:表的操作。
show tables; //列出数据库中所有的表
help create table //查看创建表的帮助
create table table1(ID int(12)); //创建表,但前提必须要有一个字段才可以创建表
show create table table1; //查看所创建表的属性
desc table1; //查看表的结构
alter table table1 rename table2; //修改表名
drop table table1; #删除表
三:字段的操作
alter table table1 add name char(10); //向表中添加一个字段。
alter table table1 add sex char(10) after ID; //在指定字段的后面添加字段,默认在最后面添加
alter table table1 add addr char(10) first; //添加字段到第一个
alter table table1 change addr address char(12); //修改字段的名称,change可以修改字段名称以及属性,
alter table table2 drop address; //删除字段
alter table table1 modify address int(12); //修改字段的属性,modify只能修改字段的属性,不能修改字段的名称。
四:记录的操作
insert into table1 set ID=123,name='mayun'; //向表中添加数据(记录),不同的字段用逗号分割
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 * from table1; //查看表中的所有数据
select ID,name from table1; //查看指定的字段的记录,不同字段用逗号分割。
select name,linux from table2 order by linux ; //指定字段按从小到大的顺序查看Linux的成绩,默认是按添加的顺序排序。
select name,linux from table2 order by linux desc limit 3;(只显示前三名)//逆序查看指定字段的数据,从大到小
select sex,count(sex) from table2 group by sex; //分组和统计,统计男女生有多少个人,以sex这个字段为一组进行统计
select sex,count(sex) from table2; //统计总共有几个记录
select sum(linux) from table2; //求指定字段的和
select avg(linux) from table2; //求指定字段的平均数
select name,chiness+math+English as total from table1; //求每个人的分数总和 as tiotal 是指定别名
select name,(chiness+math+English)/3 as avg from table1; //求每个人的平均成绩 as avg 是指定别名
select name,linux from table2 where linux=(select max(linux) from table2); //子查询 select name,max(linux) from table2; 这种查询方法会出错,需要采用子查询
delete from table1 where name='mage'; //删除指定字段的数据
delete from table1; //删除整个表的数据
delete from table2 where id=1 && name='zhao'; //删除id为1并且name为zhao的数据,其中&&相当于and
delete from table2 where id=1 || id=2; //删除id为1或者id为2的数据 其中|| 相当于or
delete from table1 where name is null; //删除匹配没有值的情况,其中is 可以换成 <=>
update table1 set name='mage' where name is null; //修改 字段为null的记录
update table1 set name='lisi' where name=''; //修改值为空数据的记录
update table1 set name='lisi' where name='mayun'; //将mayun的名字改为lisi
注:
+------+-------+
| ID | name |
+------+-------+
| 123 | mayun|
| 123 | |
| 123 | NULL |
+------+-------+
注:第三个ID中的123的NULL 表示空值 ,第二个ID中的123 表示空数据
五:mysq支持模糊匹配和正则表达式查找
模糊匹配 like
select from table2 where name like 'ha%'; //查找表中名字以ha开头的记录, 通配符%表示所有,_表示单个字符
select from t1 where name like '%o'; //匹配以o结尾的记录
正则表达式 regexp
select from table2 where name regexp '^ha'; //采用正则表达式来查找名字以ha开头的记录
select from table2 where id regexp '1|2'; //查找id为1或者id为2的记录
select from table2 where id! =1; //查找id不等于1的数据
delete from table2 where id regexp '1|2'; //删除id为1或者id为2的记录
select * from t1 where name regexp 'o$';
status; //查看数据库的状态
show processlist; //查看数据库中用户的连接状况,
show variables //用于显示mysql服务器的变量
show variables like '%port%';
数据库查找时: ; \g \G 的作用
;和\g都表示将查找的结果横向显示
\G 的作用是将查找的结果纵向显示
更改表之后刷新表的数据:flush privileges;
在mysql中如果是真的为1,如果是假的就为0