mysql----linux系统

MYSQL命令:
一、进入mysql(linux安装mysql后默认是没有密码的):

mysql -uroot;

二、mysql命令----“增“:create(关键字)
1、新建一个库(database):aaa
create database aaa;
2、新建一个表(table):students

create table students (
id int unsigned not null auto_increment primary key,
 name varchar(30) not null,
 age tinyint unsigned not null,
sex enum('男','女','保密')default '保密'
);

三、mysql命令----“改”:
1、insert into:向表中添加数据;
insert into +表名 values (加入的数据);

	全列插入:
	①、向students表中加入一组数据,,注意表的约束;
	insert into b values (0,'陈平安',16,'男');
	部分插入:
	insert into + 表名(字段名)values (对应字段加入数据);
	② 向students表中加入name,age:
	insert into students(name,age) values('陆抬',18);

2、alter修改字段

	①、向表中添加字段及约束:
	alter table +表名 add + 字段;
	alter table students add high decimal(5,2);
	②、修改字段的约束:
	alter table +表名 modify +字段及其约束;
	alter table students modify sex enum('男','女','变态','保密')default '保密';
	③、修改字段名及约束:
	alter table +表名 change +原名 +新名 +约束;
	alter table students change sex gender enum ('男','女','变态','保密')default '保密;
	④、删除字段:
	alter table + 表名 drop +字段名;
	alter table students drop high;

3、修改字段的值:update

	update +表名 set 字段=*** where 条件;
	修改表中id为3的name为 齐静春;
	update students set name='齐静春' where id=3;

四、mysql命令----删除:

1、物理删除(真的删除没有数据存在,不建议使用):
	delete from +表名 + where +条件:(满足条件的删除)
	delete from students where id=3;
2、逻辑删除(给数据做一个标记,说明数据作废,不调用了):
①添加一个标记的字段默认0为删除:
alter table students add is_delete bit default 1;
②将删除的数据的is_delete字段对应的值改为0;
update stdents set is_delete=0 where id=3;

五、mysql命令----查看:select

1、查询字段信息:
	select +字段名 from +表名;
	select name from students;
2、给字段定义别名(容易观察):as
	select +字段名 as +别名 from +表名;
	select name as '姓名',age as '年龄' from students;
3、消除重复行(distinct):
	select distinct +字段名 from 表名;
	select distinct gender from students;

4、条件查询:where

	select +想要显示的字段 from + 表名  where +条件;
	①、比较:>,<,=
	年龄大于18的信息:
	select name,age from students where age>18;
	年龄小于18岁的信息:
	select * from students where age<18;  #  *代表所有;
	年龄是18,40,50的信息:
	select * from students where age=18 or age=40 or age=50;
	或
	select * from students where age in (18,40,50);
	②区间 and,or,between:
	年龄大于18并且身高大于180:
	select * from students where age>18 and high>180;
	年龄大于18或者身高大于180:
	select * from students where age>18 or high>180;
	年龄在18~50之间:
	select * from students where age between 18 and 30;
	年龄不在18~50之间:
	select * from students where age not between 18 and 30;

5、模糊查询(like)与正则查询(rlike):

	①、模糊查询:like
	-----%:代表一个或多个字符(包括为空);
	-----_:下划线代表一个字符(多个代表多个);
	查询以 “陈”为姓的信息:
	select * from students where name='陈%';
	 查询名字中有 ‘平’ 的信息:
	 select * from students where name='%平%';
	 查询两个字的名字:
	 select * from students where name='__'		
	②、正则查询:rlike
	---- ^:代表以什么开头;
	---- .*:代表所有:
	----$:代表以什么结束;
	select * from students where name='^陈.*';
	select * from students where name='陈.*安$';

6、空判断:is null

查询身高为空的信息:
select * from students where high is null;
查询身高不为空的信息:
select * from students where high is not null;

7、排序:order by(默认由小到大)

select +显示信息 from +表名 order by +排序的字段;
①由小到大排序(升序):asc
select * from students order by age asc;
②由大到小排序(降序):desc
select * from students order by age desc;

8、聚合函数:

①、总数:count(*)
查询性别为 男 的人数:
select count(*) from students where sex='男';
②、最大值 max:
查询年龄最大的:
select max(age) from students;
③、最小值min:
查询表中年龄最小的:
select min(age) from students;
④、求和:sum
表中年龄的和:
select sum(age) from students;
⑤、平均值:avg
表中年龄的平均值:
select avg(age) from students;
或
select sum(age)/count(*) from students;
⑥保留小数  round
年龄的平均值保留两位小数:
select round(avg(age),2) from students;

9、分组:group by 条件命令:having

①按性别分组,显示性别:
select gender from students group by gender;
②计算男性人数:
select gender,count(*) from students group by gender having gender='男';
③、查询组中的名字:
select group_concat(name) from students group by gender having gender='男';

10、分页显示:limit

显示5行:
select * from students limit 5;
从第四行开始,共显示5行:
select * from students limit(4,5);

mysql 修改密码:

1、mysql 里面(密码:root;用户:root):
update user set passwoed=password('root') where user='root';
2、mysql外面:
mysqladmin -uroot -p123 password 12345;
(将root的原密码123改为12345;)
3、忘记密码:
修改配置文件 vi /etc/my.cnf.d/server.cnf 
在[mysqld]里面添加--skip-grant-tables
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值