LINUX上数据库基本操作

1.数据库操作

#sql语句最后都加上英文的 ;
-显示当前数据库版本
select version();
-显示时间
select now();
-查看所有数据库
show databases;
-创建数据库
creat database python4 charset=utf-8;
-删除数据库
drop database 数据库名;
-使用数据库
use数据库名 
use python-04;
-查看当前使用的数据库
select database();

2.数据表的操作
2.1 数据表操作
-查看当前数据库中所有的表
show tables;
-desc查看表的结构
desc xxxxx;
-创建数据表
create table python04 (字段 类型 约束1,字段 类型 约束2);
-创建一个student表(id\name\age\high\gender\cls_id)
creat table students(
id int unsigned not null auto_increment primary key , #unsigned表示正数的意思 primary key 是主键的意思
name varchar(30),
age tinyint unsigned default 0,
high decimal(5,2),
gender enum(“男”,"女","中性",“保密”)default “保密”,
cls_id int unsigned
);
-删除表
-drop database 数据库名;
-drop table 数据表名;
-显示表的创建语句
-show create table student;

2.1 字段增删改查
-增加字段
alter table 表名字 add 字段
alter table student add birthday datatime;
-修改字段
	-不重命名
	alter table student modify 列名 类型和约束
	alter table student modify birthday date;
	-重新命名
	alter table student change 原名 新名 类型和约束
	alter table student change birthday birth date default "1990-01-01";
-删除字段
alter table student drop high;

2.2 数据的增删改查

	-查询基本使用
		-查询所有数据
		select * from student;
		-制定查询条件
		-查询id大于4的所有列
		select * from student where id>4;
		-查询name为小李飞飞的
		select * from student where name="小李飞飞";

		-查询指定列
		-select 列1,列2...from 表;
		-select name,gender from student;

		-可以使用as为列或表指定别名
		select id as 序号,gender as 性别 ,name as 姓名 from student;
		select a.name,s.age from students as a;
	 	--消除重复行 distinc 字段
	 	select distinct gender from students;
	-条件查询
		-比较运算符  > < >= <= = != <>(或者) 
		-select ... from 表名 where ....;
		  -查询大于18的信息 
		  select * from students where age>18;

		-逻辑运算符 and or not 
			--查询18到28之间的学生信息
			select * from students where age>18 and age<28;
			--查询18以上的女性
			select * from students where age>18 and gender=2;
			select * from students where age>18 and gender="女";
			--不在18以上的女性
			select * from students where not (age>18 and gender=2);
			--年龄不是<18并且是女性
			select * from students where not (age>18)and gender=2;
	-模糊查询 like
		--%替换一个
		select name from students where name like "小%";
		--_替换一个
			--查询姓名中有小的
			select name from students where name like "%小%";			
			--查询名字是两个字的
			select name from students where name like "__";
			--查询至少有两个字的名字
			select name from students where name like "__%";
		--rlike正则表达式
			--以小开头的名字
			select name from students where name rlike "^小.*";
			--以周开头以伦结尾
			select name from students where name rlike "^周.*伦$";
	
		-范围查询  
			--查询年龄在18-30之间的姓名和年龄
			--select name,age from students where age>18 and age<30 or age=12;
			
			--in表示在一个非连续的时间范围内
			--select name,age from students where age in (12,14,22);
			
			--not in 表示不在某个范围内
			
			--between ...and...表示在一个连续的范围内
			--查询年龄在18-30之间 既包括18也包括34
			--select name,age from students where age between 18 and 30;
			--not between ...and...表示不在某个连续的时间范围内
			--select name,age from students where age not between 18 and 30;
			--并不是select name,age from students where age not (between 18 and 30);

			--判断空
			--is null
			--查询身高为空的信息
			--select * from students where height is null;

			--判断非空
			--is not null
			--查询身高不为空的信息
			--select * from students where height is not null;

		--排序
			--order by 默认从小到大 省略了asc
			--desc 默认从大到小
			--查询年龄在18-30之间男性,年龄从小到大排序
			select * from students where age (between 18 and 30) and gender=1 order by age;
			--查询年龄在18-30之间男性,年龄从大到小排序
			select * from students where age (between 18 and 30) and gender=1 order by age asc;

			--order by 多个字段	身高一致的 按照id倒着排序
			select * from students where age (between 18 and 30) and gender=1 order by heigh desc,id desc;
		--聚合函数
			--总数count 查询男性有多少人 女性有多少人
			select count(*) from students where gender=1;				
			select count(*) as 男性人数 from students where gender=1;
			--查询最大年龄
			select max(age) from students;
			--查询女性的最高身高
			select max(height) from students where gender=2;
			--计算所有女人的年龄总和
			select sun(age) from students where gender=2;
			--平均值 avg 计算所有女人的年龄平均值
			select avg(age) from students where gender=2;

		--分页 
			-用limit直接限制查询出来的个数
			select * from students where gender=1 limit 2;
			-limit start ,count 从哪里开始(不包括)和最多显示几个
			--从第6个数开始,显示两个
			select * from students where limit 6,2;
			--limit 一定是放在最後面的
			select * from students order by age asc limit 6,2;
			--作业:查询所有的女性信息,按照身高从高到低排序,只显示两个
			select * from students where gender=2 order by height desc limit 0,3;


		--连接查询 多个表查询数据使用
			--inner jion ...on 内链接就是取两个表都有的数据,取交集
			-select * from 表A inner jion 表B;
			select * from students inner join classes;
			--查询有能够对应班级的学生和班级信息
			select * from students inner join classes on students.cls_id=classes.id;
			
			--按照要求显示姓名和班级
			select students.*,classes.name from students inner join classes on students.cls_id=classes.id;

			select s.name,c.name from students as s inner join classes as c on s.cls_id=c.id;
			--在以上的查询中,将班级姓名显示在第一列
			select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id;
			--在以上的查询中,将班级姓名显示在第一列,按照班级进行排序
			select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name;
			--在以上的排序中,再按照学号进行排序
			select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name,s.id;
			
		--外链接 分为左链接(哪个表在左边就以哪个表为基准找不到就显示null)和右链接
			select * from students as s left join classes as c on s.cls_id=c.id;
			--查询没有班级的学生 在查出来的结果集里面找就用 having 要是在一个表里就用 where
			select * from students as s left join classes as c on s.cls_id=c.id having c.id is null;
			select * from students as s left join classes as c on s.cls_id=c.id where c.id is null;

		--自关联
			--新建一个表
			create table areas(aid int primary key ,atitle varchar(20),pid int);
			--直接在一个大表里找山东的地级市
			select * from booktest_areas as province inner join booktest_areas as city on city.pid=province.aid having province.atitle="山东省";
		--子查询
			 --查询里边再查询
			 --
			select * from students where height = (select max(height) from students);
			--求平均值
			select round(avg(price),2) from goods;
	-逻辑删除字段
		-用一个字段来表示这条信息已经不能再使用了,没有真正的删除
		-给student表添加一个 is_delete字段 bit 类型
		alter table student add is_delete bit default 0;
		update student set is_delete=1 where id=6;
发布了8 篇原创文章 · 获赞 0 · 访问量 46

猜你喜欢

转载自blog.csdn.net/I__INIT/article/details/105113449