MySQL数据库

MySQL

mysql数据库介绍和安装

MySQL数据库

特点:开放源码的,轻量型的数据库管理系统。使用简单快捷

navicat:mysql的图形化操作软件

mysql的卸载:

1.在控制面板中选择卸载程序,然后选择mysql卸载

2.在盘符中将mysql的安装文件夹删除 例如:C:\Program Files\mysql

3.在programData隐藏文件夹下删除mysql文件夹

4.清理垃圾

mysql的测试安装

1.打开navicat点击connection然后输入连接名

2.输入连接地址

3.输入用户名: root

4.输入安装时设置的密码

5.点击测试连接

6.显示connection successful 则表示安装成功

mysql创建连接

注意:连接不是数据库,在连接下创建数据库

mysql会自带4个数据库:

mysql、information_schema、performance_schema、test

mysql的使用

mysql管理数据库

1.mysql创建数据库特别简单、方便。

使用命令:
       a.任意双击打开一个数据库,然后选择Query-->New Query-->打开sql窗口
	   b.书写创建库的命令
		create database 库名 default CHARACTER set utf8;
		示例:
		create database student default CHARACTER set utf8;
        mysql的注释为 ##注释内容
	   c.切换数据库到新建库:
		 use 库名;	
	   注意:
			选择要执行的sql语句,右键选择 run selected
使用图形化界面:
	   在连接名上右键-->new database-->弹出新建库的窗口-->输入库名-->选择编码格式为utf8-->点击ok即可

2.删除数据库

使用命令: 
	a.任意双击打开一个数据库,然后选择Query-->New Query-->打开sql窗口
	b.drop database 库名;
使用图形化界面:
	选择要删除的库,右键-->delete database	

mysql操作表

1.创建表语句

create table 表名(字段名 类型 约束,字段名 类型 约束,...) 
注意:mysql是没有序列的,但是我们可以在创建表的时候直接指定主键是自增的
示例: 
    create table student(
		sid int(10) not null auto_increment primary key,
		sname varchar(100) not null,
		sage int(3),
		ssex char(4),
		sbith date
	)
	**注意:主键自增在创建表的字段后使用 auto_increment**

2.mysql常见的字段类型

数值类型:
		int(长度) 表示整数类型的数据 长度可以指定10
		float/double 表示浮点数
字符类型:
		varchar(长度) 动态分配存储长度
		char(长度) 分配固定长度
日期类型:
		data 格式为yyyy-mm-dd
		datatime 格式为yyyy-mm-dd HH:MM:SS 占用8个字节
		datastamp 特点:会自动进行拾取的转换 占用4个字节
		time 时间
		year 年份
其他类型:
		text 非二进制大对象(字符数据)
		blob 二进制大对象(非字符数据)

3.约束:https://blog.csdn.net/a909301740/article/details/62887992

主键约束:
		在创建表时候使用 primary key 即可(*)
		在创建表的语句最后面使用 constraint 约束名 primary key(主键字段名)
		在创建表后使用 alter table 表名 add constraint 约束名 primary key(字段名)
非空约束:
		在创建表的时候直接在字段后使用 not null 即可
		在创建表后使用 alter table 表名 modify 字段名 类型  not null
		注意:mysql的非空约束中空字符可以存储进去
检查约束:
		问题:在mysql中没有检查约束,但是使用check关键字又不会报错
		解决:
			使用代码逻辑进行无效数据的过滤(*)
			使用mysql的存储过程(自行百度)
唯一约束:
		在字段名后直接使用 unique 关键字(*)
		在创建表的语句的最后面使用 constraint
		在创建表的语句之后使用 alter table 表名 add constraint 约束名 unique key(字段名)			
外键约束:
		在字段名后直接使用 references 父表名(父表主键名)
		在创建表的语句后面使用 constraint 外键约束名 foreign key(字段名) references 父表名(父表字段名)
	    在创建表的语句之后使用 alter table 表名 add constraint 外键约束名 foreign key(字段名) references 父表名(父表字段名) on delete cascade on update cascade
示例:
	create table student(
		sid int(10) not null auto_increment primary key,
		sname varchar(100) not null,
		sage int(3) not null,
		ssex char(4) not null,
		sqq varchar(10) not null unique,
		sbith date,
		cid int(10) references clazz(cid)
		##constraint fk_cid foreign key(cid) references clazz(cid)
	)

4.表的修改删除

添加字段:
		alter table 表名 add 字段名 字段类型; 
删除字段:
		alter table 表名 drop 字段名;
修改字段类型:
		alter table 表名 modify 字段名 新的字段类型;
修改字段名:
		alter table 表名 change 字段名 新的字段名 类型;
修改表名:
		alter table 表名 rename as 新的表名;
删除表名:
		drop table 表名		

5.其他操作

显示表信息:
		show tables
显示创建表语句:
		show create table 表名
显示库:
		show databases

mysql操作表的数据

1.查询

    单表查询:
			别名:直接空格在字段后使用即可
			去除重复:使用distinct关键字即可
			连接符:使用concat(字段名,字段名)
			排序:order by 字段名
			where子句:一样
			函数:
				max min avg sum count
			分组:group by 字段名
					注意:分组可以和字段一起使用
			having:
					分组后筛选
	多表查询:	
			sql92:和oracle一样
				 --特点:易于书写,难于阅读
			     --缺点:92的SQL语句结构不清晰
			     --用法:
			        --select 内容(别名,连接符,去除重复,oracle函数,逻辑运算)
			        --from 表名1,表名2,表名3...
			        --where 条件(连接条件,普通筛选条件,where子句关键字)
			        --group by 分组字段
			        --having 多行函数筛选
			        --order by 排序字段

			sql99:和oracle一样
			      --特点:难于书写,易于阅读
			      --使用:
			        --select 内容 from 表名1
			        --inner join 表名2
			        --on 连接条件
			        --inner join 表名3
			        --on 连接条件
			        --where 普通筛选条件
			        --group by 分组
			        --having 多行函数筛选
			        --order by 排序
	子查询:
			单行子查询:
				  --使用时机:筛选条件不明确需要执行一次查询,并且查询结果为一个字段并且只有一个值
				  --注意:where子句中允许出现查询语句,该查询语句为子查询
				  --使用:select 内容 from 表名 where 字段名 比较运算符 子查询语句
				    --查询所有比雇员"CLARK"工资高的员工信息
				      select sal from emp where ename='CLARK';
				      select * from emp where sal>2450;
				      select * from emp where sal>(select sal from emp where ename='CLARK');
				    --查询工资高于平均工资的员工的名字和工资
				      select lower(ename),sal from emp where sal>(select avg(sal) from emp);
				    --查询和soctt同属一个部门且工资比他低的员工资料
				      select * from emp where sal<(select sal from emp where ename='SCOTT') and deptno=(select deptno from emp where ename='SCOTT');
				    --查询工资最高的员工资料
				      select * from emp where sal=(select max(sal) from emp);
				    --查询职务和scott相同,雇佣时间早的员工信息  
				      select * from emp where job=(select job from emp where ename='SCOTT') and hiredate<(select hiredate from emp where ename='SCOTT');
				    --查询工资比scott高或者是雇佣时间早的员工编号和名字  
				      select empno,ename from emp where job=(select job from emp where ename='SCOTT') and hiredate<(select hiredate from emp where ename='SCOTT');
      	
							
			多行子查询:
				    --使用:当子查询的结果只有一个字段但是字段有n个值,考虑使用多行子查询,其实就是使用关键字
				    --关键字:any 任意
				      --select 内容 from 表名 where 字段名 比较运算符 any 子查询语句
				    --关键字2: all所有
				      --select 内容 from 表名 where 字段名 比较运算符 all 子查询语句
				    --关键字3:in  相当于 = any
				      --select 内容 from 表名 where 字段名 in 子查询语句
				    --查询工资高于任意一个CLERK的所有员工信息
				      select * from emp where sal>(select min(sal) from emp where job='CLERK');
				      select * from emp where sal> any (select sal from emp where job='CLERK');
				    --查询工资高于所有SALESMAX的员工信息        
				      select * from emp where sal>(select max(sal) from emp where job)
				      select * from emp where sal> all (select sal from emp where job)
				    --查询部门20中同部门10的雇员工作一样的雇员信息
				      select * from emp where job in (select job from emp where deptno=10) and deptno=20;

2.增加

insert into 表名 values(值1,值2,值3...); 全字段插入
insert into 表名(主键字段名,字段,字段...) values(值1,值2,值3...); 部分字段插入   

3.删除

delete from 表名 where 条件 

4.修改

update 表名 set 字段名=值,字段名=值... where 条件 

mysql的分页

使用关键字limit
	select * from 表名 limit m*n-n,n;
	m表示页面数
	n表示每页显示的数量

mysql的数据库备份

命令方式:
	导出:
		mysqldump -u -root -p 数据库名 >dbname/sql
	导入:
		mysql>source d:\dbname.sql
工具方式:
	导出:
		直接在库上右键选择导出数据库即可
	导入:
		直接在库上右键选择导入数据库即可
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值