数据库基本操作---待更新

这篇博客详细介绍了数据库的基本操作,包括启动/停止服务、连接与断开数据库,查看版本和时间,以及远程连接。接着讲解了数据库的创建、删除、切换和查看。在表操作部分,涉及查看所有表、创建和删除表。数据操作部分涵盖了增加、删除、修改、查询、分组、排序和分页。最后,文章讨论了数据库的关联查询,如INNER JOIN、LEFT JOIN和RIGHT JOIN。
摘要由CSDN通过智能技术生成

一、基本命令

1.启动/停止服务
net start/stop 服务器名称
2.连接数据库
mysql -uroot -p 密码
3.断开连接
quit/exit;
4.查看版本,显示当前时间
select version();
select now();
5.远程连接
mysql -h ip地址 -u 用户名 -P

二、数据库操作

1.创建
create database 数据库名 charset=utf8;
2.删除
drop database 数据库名;
3.切换数据库
use 数据库名;
4.查看当前选择的数据库
 select database();

表操作

1.查看当前数据库中所有的表
show tables;
2.创建表
create table 表名(列及类型);

示例:

create table students(
	id int auto_increment primary key,
	name varchar(20) not null,
	age int not null,
	gender bit default 1,
	address varchar(20),
	isDelete bit default 0);

3.删除表
drop table 表名;
desc 表名
show create table 表名;
rename table 原表名 to 新表名
alter table 表名 add/change/drop 列名 类型;
示例
alter table newCar add isDelete bit default 0;

四、数据操作

1.增
  • 全列插入
在insert into student values(0,"tom",19,1,"北京",0),(),....多条数据插入
  • q缺省插入
在insert into student(name, age, address) values("lilei", 19, "上海")
2.删除
delete from 表名 where 条件;

逻辑删除

update student set isDelete=1 where id=1;
3.改

update 表名 set 列1=值1,列2=值2... where 条件:
无条件则全改

4.查
  • 基本语法
 select 列1, 列2.../* from 表名;
  • 消除重复行
 select distinct gender from students;

- 条件查询

 select 列名/ *  from 表名 where 条件;

比较运算符
= > < <= >= !=/<>
逻辑运算符
and or not
模糊查询like
%表任意多个字符
_表示一个任意字符
select * from student where name like "孟%"/"孟_";
范围查询

 select * from student where id in (8,20,10);
 查询6到8的学生
 select * from student where id between 6 and 8;

空判断
...where address is null/not null;

聚合
count(列)
max(列)
min() sum() avg()
select min(id) from student wehre gender=0;

5.分组

按照字段分组,表示字段相同的数据会被放到一个集合中,分组后只能查询出相同的数据列,对于有差异的数据列无法显示在结果集中,可对分组后的数据进行统计,做聚合等

select 列1, 列2,聚合... from 表名 group by 列1,列2...
示例:查询男女总数
select gender, count(*) from student group by gender;

分组后数据筛选
select 列1, 列2,聚合… from 表名 group by 列1,列2…having 列1,…聚合…
示例

 select gender, count(*) from student group by gender having gender=0;

where 与having的区别:

 where 是对from后指定的表进行筛选,属于对原始数据的筛选
 having是对group by的结果进行筛选
6.排序
 select * from student order by 列1 asc/desc, 列2 asc/desc;

按照列1进行排序,列1中值相同的按照列2排序,asc表升序
示例:

 select * from student where idDelete=0 order by age asc, id desc;
7.分页
select * from 表名 limit start,count;
select * from student where gender=1 limit 0,3;

六、关联

建表

1.create table class(id int auto_increment primary key,
				name varchar(20) not null,
				stuNum int not null);
2.create table students(id int auto_increment primary key,
					name varchar(20) not null,
					gender bit default 1,
					classid int not null,
					foreign key(classid) references class(id));
插入数据
insert into class values(0, "py1", 55), (0, "py2", 50), (0, "py3", 30);
insert into students values(0, "tom", 1, 1), (0, "lilei", 1, 10), (0, "jieke", 1, 2);

关联查询

select students.name, class.name from class inner join students on class.id=students.classid;

inner join AB共同部分
left join 左部分
right join 右部分

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值