数据库相关命令操作

一、安装MYSQL

1、初始化
mysqld -initialize --console
2、安装MySQL服务
mysql -install
3、开启服务
net start mysql
4、关闭服务
net stop mysql
5、删除服务
sc delete mysql
或者注册表直接删除
在这里插入图片描述

二、登录

1、登录
mysql -uroot -p
2、修改初始密码
alter user ‘root’@‘localhost’ IDENTIFIED BY ‘12345678’;

三、数据库的基本操作

1、查看数据库
show databases;
在这里插入图片描述
2、创建数据库
create database test;

安全方式创建:
create database if not exists dunkai;
3、使用数据库
use test;

4、删除数据库
drop database dunkai;

安全方式删除数据库:
drop database if exists dunkai;

注意:删除数据库的时候一定要谨慎,数据是无价的,回车确认了,就删除了,就不能恢复。

四、数据库的存储引擎

show engines \g
在这里插入图片描述

五、数据表的操作

1、查看表
show tables;

2、创建表
create table student(
id int,
name varchar(10),
sex varchar(2),
age int,
score int
);

3、删除表
drop table subject;
安全删除方式:drop table if exists subject;
4、展示表结构
desc student;
在这里插入图片描述
5、修改表列
A.修改列的类型
alter table student modify sex char(2);
B. 修改列的类型以及列名
alter table student change sex sex1 varchar(2);
C. 修改表名
alter table student rename to newstudent;
alter table newstudent rename as student;

6、插入数据
A. 插入单条数据
insert into student
(id, name, sex,age, score)
value
(3, “Kevin”, “男”, 19, 98);

B. 插入多条数据
insert into student
(id, name, sex,age, score)
values
(4, “业精于勤”, “女”, 20, 100),
(5,“我爱你”, “男”, 27, 85),
(6,“起名字真难”,“男”, 28, 90);

7、删除数据
删除一条数据
delete from student where id=1;

删除多条数据
delete from student where age between 20 and 27;

8、使用主键约束
主键:主码,
主键约束的数据一定是唯一,不允许为空。
主键能够表示表中唯一的一条数据。

A. 单字段主键
create table student(
id int primary key,
name varchar(10),
sex varchar(2),
age int,
score int
);

第二种方式:

create table student(
id int,
name varchar(10),
sex varchar(2),
age int,
score int,
primary key(id)
);

B. 多字段联合主键
create table student(
id int,
name varchar(10),
sex varchar(2),
age int,
score int,
primary key(id, name, age)
);

六、MySQL数据类型

MySQL数据类型:数值型、字符串型、日期/时间

1、整数类型
类型名 解释 字节 有符号 无符号
tinyint 很小的整数 1个字节 -128->127 0->255
smallint 小整数 2个字节 -32768->32767 0->65535
mediumint 中等整数 3个字节 -223->223-1 0->2^24-1
int 普通整型 4个字节 -231->231-1 0->2^32-1
bigint 大整数 8个字节 -263->263-1 0->2^64-1

2、浮点型和定点型
MySQL使用浮点型和定点型来表式小数。
单精度浮点型(float), 4个字节
双精度浮点型(double) 8个字节
定点型(decimal)decimal(M,N) M+2个字节

M:位数(精度),N:小数 (标度)

3、字符串型
MySQL支持两种字符串:文本字符串,二进制字符串

常用:char , varchar, text, enum, set类型

类型 解释 字节
char(M) 固定长度长度文本字符串 M个字节(1<=M<=255)
varchar(M) 变长文本字符串 变长,实际长度+1, (255)
tinytext 非常小的文本字符串 变长,实际长度+1,(255)
mediumtext 中等大小文本 变长,实际长度+1,(2^16)
longtext 大的文本 变长,实际长度+1,(2^24)
enum 枚举类型 1个字节或两个字节
set 集合 最多64个成员

varchar占用的空间等于字符串长度+1

空字符串

什么时候用char, 什么时候用varchar
要求速度非常快:char的速度快于varchar
以空间换性能
以性能换空间

折中选择。

4、日期时间类型
类型 日期 范围 字节
YEAR YYYY 1901->2155 1个字节
TIME HH:MM:SS -838:59:59->838:59:59 3个字节
DATA YYYY-MM-DD 1000-01-01->9999-12-31 3个字节
DATETIME YYYY-MM-DD HH:MM:SS 1000-01-01 0:0:0->9999-12-31 23:59:59 8个字节
TIMESTAMP YYYY-MM-DD HH:MM:SS 1970-01-01 0:0:1->2038-1-19 03:14:07 4个字节

七、查询数据

1、基本查询语句
A. 查询所有字段
select * from student;

B. 查询指定字段
select name,score from student;
C、根据条件查询
select id,name,score from student where score>=90;
= 等于
不等于!= , <>
大于 >
小于 <
大于等于 >=
小于等于 <=
两者之间 between … and …
select id,name,score from student where score between 90 and 95;
带and

select id, name,score from student where score=100 and age <= 30;
2、带in关键字查询
select * from student where score in(80,90,95,100);
查询出在列举在括号中的
3. 带like的字符串匹配查询

A. 百分号%, 可以匹配任意长度,
select * from student where name like “%止%”;
B. 下划线匹配 _ 一次只能匹配一个字符
select * from student where name like “_ _止境”;
4. 查询空值
select * from student where score is null;
select * from student where score is not null;

5、and多个条件查询
select * from student where sex=“男” and score>90;

6.带or的多个条件查询
select * from student where sex=“男” or score>90;
7. 消除重复值
select distinct score from student;
9、对结果排序

select * from student order by score;

默认升序排序asc;

降序排序加上desc;
select * from student order by score desc;

  1. 限定查询结果数量
    Limit

select * from student order by score limit 4;

从第几条开始,显示几条
select * from student order by score limit 2,3;

八、更新数据

update student set score=99,age=44 where name = “学无止境”;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值