Java learning day 27--mysql database basic SQL operations

MySQL Day 1:

1. Uninstall MySQL

1. Record two paths:
There are two paths around line 74 of my.ini in the installation directory: basedir and datadir
2. Stop the MySQL service first
1. Manual stop:
Find the management interface --> Services
2. Command to stop:
net stop mysql
net start mysql
3. Control Panel Uninstall
Find MySQL --> uninstall
4. Delete the contents of the two paths recorded in the first step
5. Other uninstall methods:
Use some software management tools: 360 Software Manager

2. MySQL installation
1. The installation file is placed in a path with no Chinese and no spaces
2. Double click to install
Installation path selection: no Chinese, no space path
3. The installation is divided into two processes: installation and configuration
The four checkmarks in the last step all appear, indicating that the installation was successful
If there are no four check marks during the installation process, you can reinstall it.
When reinstalling, you will be prompted to delete or repair, select delete


There are two ways to enter the MySQL console:
1. The console that comes with MySQL:
Enter the password directly
2. Command Prompt:
mysql -uroot -proot

Basic database operations:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[DEFAULT] CHARACTER SET charset_name
  | [DEFAULT] COLLATE collation_name

//The simplest build statement  
create database day27;
create database IF NOT EXISTS day27;

// view database
show databases;

//View the build statement
show create database day27;
You can see the default character set of the database

//创建库的时候,指定字符集和校对规则
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;





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325942346&siteId=291194637