17-mySql数据库入门

大家好,我是被白菜拱的猪。

Structure Query Language
结构化		查询		语言

MySQL 软件解压使用
mysql5解压到D盘的根目录
【说明】如果计算机没有D盘,	(1)需要修改Mysql.bat(有两处需要修改)
					(2)需要修改my.ini(有两处需要修改)
	
【结果】:D:/mysql5/bin
(1)双击Mysql.bat文件,会弹出一个cmd(命令提示符界面)
(2)windows键 + R,输入cmd,点击回车
	出现了一个命令提示符界面
	【1】进入任意盘符的指令: 盘符+英文冒号(:)
		例如:去D盘
			D: 回车
	【2】进入任意子目录(子文件夹)
		例如:进入mysql5
			cd mysql5
			[说明]:cd == change directory(切换目录)
		再进入bin目录
			cd bin
			[说明]:bin == binary(二进制)
	【3】可以使用dir查看当前文件夹下都包含什么文件(和文件夹)
		dir 回车
	【4】进入mysql环境
		mysql -uroot -pmysql
		[说明]	-u 表示	user(用户)
			 	-p 表示 	password(密码)
	
	【5】创建我们自己的数据库(物理存在的文件)
		create database 库名称;
		[说明]在计算机上物理的创建一个文件夹(包含一个 db.opt)
		opt--词根:选择
		option	选项
		Adopt	选中;收养
		
	【6】进入我们创建的数据库(使用我们创建的数据库)
		use 库名称;
		
	【7】在库中创建表(表格)
		create table student (code int, name char(90));
		[说明]:在计算机的库文件夹中,物理的创建了一个student.frm
	【7.1】如果需要给表增加一个列
		alter table student add sex char(8);
						            增加	 性别	类型(长度)
	【7.2】需要删除一个表
		drop table student;	
	【7.3】使用desc指令,查看表的结构
		desc student;					    
		
	【8】向表中存储数据
		insert into student (code,name) values (1001,'Alex');
		insert into student values (1002, 'Bomb');
		
	【9】查找表中的数据
		select code,name from student
		select * from student 表示查全部的列(字段 == column)
		
	【10】修改(如果已经插入的记录需要修改信息)
		update student set sex = '男' where code = 1002
		update student set sex = '男' where name = 'Bomb'
		update student set sex = '男' where code = 1002 and name = 'Bomb'
	
	【11】删除一个记录
		delete from student where sex = 'male';		
		【**】如果删除时,依赖的是一个NULL
			delete from student where code is NULL;
			
	【12】我们可以对记录进行约束
		(1)unique可以对当前的列设置唯一值(这个列的数据不能重复)
		create table student (code int unique, name char(30), sex char(8));
		
		(2)not null 非空约束
		create table student (code int not null, name char(30), sex char(8));
		
		(3)既 not null 又 unique
		create table student (code int not null unique, name char(30), sex char(8));
		create table student (code int unique not null, name char(30), sex char(8));
		
		(4)如果一个表中有两个列是(非空 && 唯一)的
		create table student (code int unique not null, name char(30) not null unique, sex char(8));
	
		(5)主键:主要的索引(如果我们的查询条件是主键列,查询速度是非常快的)
		create table student (code int primary key, name char(30) not null unique, sex char(8));
			[说明]:如果没有认为的指定主键,则第一个not null 并且 unique的列,就是主键所在的列
			
		(6)指定一个默认值(default '值')
		create table student (code int primary key, name char(30) not null, sex char(8) default 'female');
	
	【13】带有条件的查询
		(1)如果只需要查询到male,需要在查询的时候指定条件
		select * from student where sex = 'male';
		
		(2)多个条件组合(and与 / or或)
		select * from student where sex = 'male' and name = 'mary';
		
		(3)按照年龄排序(默认是升序)
		select * from student order by age;
		select * from student order by age asc;
		select * from student order by age desc; 降序
	
		(4)在条件过滤的基础上去排序
		 select * from student where sex = 'male' order by age desc;
		 
		 (5)可以进行分组
		 select sex,count(name) from student group by sex;
		 select sex,max(age) from student group by sex;
		 select sex,min(age) from student group by sex;
		 select sex,avg(age) from student group by sex;
		 select sex,sum(age) from student group by sex;
		 [说明]:count、max、min、avg、sum都叫做聚合函数(在整体里面求个体)
		 【结论】如果使用了分组(group by)。则只能查询被分组的列,及其他列的聚合函数
	
		(6)使用不等号: > >= <= < != <> 
		select * from student where age > 20;
		
		(7)如果结果过多,可以只要一部分
		select * from student limit start, count
									从那开始	取出几个

-------------------------------------------------------
前条件:where		在整个表格(的数据)中过滤
后条件:having		表示分组之后的结果再过滤

列 like '%A%'

嵌套查询
select count(*) from (select distinct kind from goods where trademark = 'Apple') ss123

(1)子查询:select distinct kind from goods where trademark = 'Apple'


(2)嵌套 
select sid from student where sname = '张三'; 查到学号是1
select * from achievements where course = '语文' and sid = 1
select * from achievements where course = '语文' and sid = (select sid from student where sname = '张三');

(3)连接
select * from student s, achievements a where s.sid = a.sid
select * from student s, achievements a where s.sid = a.sid and s.sname = '张三' and course = '语文';

发布了24 篇原创文章 · 获赞 4 · 访问量 2038

猜你喜欢

转载自blog.csdn.net/weixin_44226263/article/details/98352405