Java学习第27天--mysql数据库基本SQL操作

MySQL第一天:

1.卸载MySQL

1.记录两个路径:
安装目录下my.ini 74行左右 两个路径:basedir 和 datadir
2.先停MySQL服务
1.手动停:
找到管理界面-->服务
2.命令停:
net stop mysql
net start mysql
3.控制面板卸载
找到MySQL --> 卸载
4.把第一步记录的两个路径中的内容删除
5.其它卸载方式:
使用一些软件管理工具:360软件管家

2.MySQL安装
1.安装文件放在一个没有中文,没有空格的路径
2.双击安装
安装路径选择:没有中文,没有空格的路径
3.安装分为两个过程:安装和配置
最后一步的四个对号都出现,表明安装成功
如果安装过程没有出现四个对号.重新安装即可.
重新安装的时候,会提示删除或者是修复,选择删除


进入MySQL控制台的两种方式:
1.MySQL自带的控制台:
直接输入密码
2.命令提示符:
mysql -uroot -proot

数据库基本操作:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[DEFAULT] CHARACTER SET charset_name
  | [DEFAULT] COLLATE collation_name

//最简单的建库语句  
create database day27;
create database IF NOT EXISTS day27;

//查看数据库
show databases;

//查看建库语句
show create database day27;
可以看到数据库的默认字符集

//创建库的时候,指定字符集和校对规则
create database day27_2 CHARACTER SET utf8 COLLATE utf8_sinhala_ci;

//查看系统中的字符集
show character set;
//查看系统中的校对规则
show collation;

//查看某个字符集的校对规则
show collation like 'utf8%';
create database day27_3 character set utf8;

//删除数据库
drop database day27_3;

//修改数据库的字符集和校对规则
alter database day27 character set gbk;
alter database day27 collate gbk_bin;

//查看当前正在使用的数据库
select database();

//进入到一个数据库里
use day27;

//建表格式
create table tbl_name(
col_name type [约束],
col_name type [约束],
col_name type [约束]
);

create table stu(
name varchar(50),
age int,
num varchar(20)
);


mysql中的数据类型:
数值:
tinyint: 1
smallint: 2
MEDIUMINT 3
int: 4 (最常用)
bigint: 8

字符串:
char : 指定长度没有填满,以空格填充(定长)
varchar : 指定长度没有填满,不以空格填充(变长)
时间/日期:
date:只有日期,没有时间
time:只有时间,没有日期
datetime:既有日期,也有时间,需要传值
timestamp:既有日期,也有时间,可以把当前系统时间自动录入

//显示一个数据库中的所有表
show tables;

//显示创建表的语句
show create table stu;

//以列表的形式显示表的语句
desc stu

//查看某一个表的所有信息
select * from stu2;

//创建表,加上各种约束
create table employee2(
id int primary key auto_increment,
name varchar(10) not null,
phone_num varchar(15) unique
);

create table temp(
name varchar(10),
age int
);
//删除表
drop table temp;
//添加列
alter table employee2
add hobby varchar(20);
//删除列
alter table employee2
drop hobby;
//修改列数据长度
alter table employee2
modify name varchar(20);
//修改列数据类型
alter table employee2
modify name int;
//修改列名
alter table employee2
change name score int;
//修改表名:第一种
rename table employee2 to emp;

//修改表名:第二种
alter table emp
rename to employee2;

//往表中插入数据
insert into 表名(列名1,列名2...) values(值1,值2...);

insert into employee2(id,score,phone_num) values(1,100,'13512345678');
//说明:在MySQL中,字符串,日期,都用单引!!!

//当想往所有列插入数据时,有简便写法
insert into employee2 values(2,99,'13012345678');


//一般情况下,id列系统自动维护,不用传值
insert into employee2 values(null,98,'13612345678');

insert into employee2 values(null,97,'13712345678');

//查询数据
select 列名1,列名2,... from 表名;

select id,score,phone_num from employee2;
//当想要显示一个表中的所有列时,简便写法:
select * from employee2;

//显示指定列
select phone_num,id from employee2;

//删除记录
delete from employee2 where id = 2;

//清空表
delete from employee2;

//truncate截断表
truncate employee2;

//delete,truncate清空表的区别
目的一样:把表中的数据删除.保留表结构.

delete是一条一条的删除数据
truncate是一次性把表删除,相当于drop table,接着再创建一个一样的表.


//更新数据
update employee2 set score = 100 where id = 1;

update employee2 set score = 95;


//把所有记录在原基础上增加5分
update employee2 set score = score + 5;

//增加name列
alter table employee2
add name varchar(10);


insert into employee2 values(null,98,'12812345678','tom');

insert into employee2 values(null,97,'13912345678','李四');

//插入中文数据
insert into employee2 values(null,100,'13812345678','张三');

1.临时设置cmd窗口的编码:
mysql --default-character-set=gbk -uroot -proot
窗口关闭,失效
2.永久设置:
安装目录下的my.ini配置文件


//更新一个范围内的数据
update employee2 set name = '张三' where id >= 3;


//对多列值进行更新
update employee2 set score = 100 , name = '李四' where id = 6;

//
update employee2 set score = 50 where id < 3;

update employee2 set score = 90 , name = '李四' where id = 3;

update employee2 set score = score - 10 ;

//删除主键约束
alter table employee2 drop primary key;
//添加主键约束
alter table employee2 add primary key(id);

//删除唯一值
alter table employee2 drop unique;

//where 子句是对行(记录)进行过滤
select * from employee2 where id = 3;
select id from employee2 where id = 3;
select id from employee2 where id > 3;

//查询结果在进行计算
select score + 10 from employee2 where id = 1;


//给列起别名
select score + 10 as sum from employee2;
//as 关键字可以省略
select score + 10 sum from employee2;


//将成绩为(80,88,90)的信息查询出来
select * from employee2 where score in (800,880,99);

insert into employee2 values (null,99,'12012345678','张');
insert into employee2 values (null,100,'12112345678','二张');
insert into employee2 values (null,100,'12212345678','二张三');
insert into employee2 values (null,100,'12312345678','二张三四');
insert into employee2 values (null,100,'12412345678','张三四');


%统配符:
没有,一个,或多个字符都能匹配

_:只能匹配一个字符

select * from employee2 where name like '张%';
select * from employee2 where name like '%张';

select * from employee2 where name like '张_';
select * from employee2 where name like '%张%';

//查询名字为null的记录
select * from employee2 where name is null;

//查询名字带张,并且分数不是100分
select * from employee2 where name like '%张%' and score != 100;


select * from employee2 where name like '%李%' or score = 100;

//查询成绩在80-90之间的记录:between and
select * from employee2 where score >= 80 and score <= 90;
select * from employee2 where score between 80 and 90;

//查询98,99分的记录
select * from employee2 where score = 88 or score = 99;

//查询两个字名字的信息
select * from employee2 where name like '__';

select name ,math from stu;

//按某列倒叙排列
select name ,math from stu order by math desc;
//按某列正序排序
select name ,math from stu order by math asc;
//正序排序asc可以省略
select name ,math from stu order by math;

//使用多列进行排序
select name,math,ch from stu order by math desc, ch desc;

练习:(使用测试数据的表stu)
1.查询各个学生总成绩,并按总分从高到底排序
select name,math + english + ch as sum from stu order by sum desc;

2.查询学生成绩,先按math升序,math相同,按english降序排序
select * from stu order by math ,english desc;

3.将所有姓张的同学的english成绩降序排列
select * from stu where name like '张%' order by english desc;

//可以使用select中没有出现的列进行排序
select name ,math from stu order by ch;





猜你喜欢

转载自blog.csdn.net/czz1141979570/article/details/80230724