MySQL基础知识
一.数据库操作
1.数据库创建
命令行创建
mysql>create database 数据库名称
使用navicat
2.查看当前都有哪些数据库
show databases;
3.删除数据库
drop database 数据库名
二.表操作
1.创建表
create table 表名
创建一个空表格
create table students(
class varchar(50),
name varchar(50)
);
创建检查表
create table checks(
class varchar(50),
teacher varchar(50),
classroom int,
count int
);
创建成绩表,存储学生姓名 班级 数学成绩 语文成绩 学期
create table score(
name varchar(50),
class varchar(50),
math float,
chinese float
);
2.删除表
drop table 表名
三.数据操作
1.数据类型
整数类型
MySQL数据类型 | 含义(有符号) |
---|---|
tinyint(m) | 1个字节 范围(-128~127) |
smallint(m) | 2个字节 范围(-32768~32767) |
mediumint(m) | 3个字节 范围(-8388608~8388607) |
int(m) | 4个字节 范围(-2147483648~2147483647) |
bigint(m) | 8个字节 范围(±9.22*10的18次方) |
浮点型
MySQL数据类型 | 含义 |
---|---|
float | 单精度浮点型 |
double | 双精度浮点型 |
字符串型
MySQL数据类型 | 含义 |
---|---|
char(n) | 固定长度,最多255个字符 |
varchar(n) | 可变长度,最多65535个字符 |
tinytext | 最多255个字符 |
日期事件类型
MySQL数据类型 | 含义 |
---|---|
date | 日期 ‘2008-12-2’ |
time | 时间 ‘12:25:36’ |
datetime | 日期时间 ‘2008-12-2 22:06:44’ |
timestamp | 自动存储记录修改时间 |
2.增加数据
向表中插入一条数据
insert into 表名 values('mad','张慧宣');
向部分字段插入数据
insert into 表名 (name,class) values('张慧宣','mad');
向所有字段插入数据
insert into 表名 values('张慧宣','mad');
3.更新数据
语法: update 表名 set 字段=值 where
更新字段
update 表名 set math=80 where name='张慧宣';
更新多个字段
update 表名 set math=80,chinese=80 where name='张慧宣';
4.删除数据
delete from 表名 where name='张慧宣';
总结:
1.删除的是表中数据,不是表本身
2.删除一定加上where条件 ,不然会导致全部删除
5.查询数据
查询表中所有数据
select * from 表名;
查询score表中姓名为王的姓名 班级 数学成绩
select name,class,math from score where name='王';
查询所有数学及格的同学的姓名班级成绩
select name,class,math from score where math>=60;
查询除了第一学期之外的同学信息
select * from score where term!=1;
查询数学语文都及格的学生信息
select * from score where math>=60 and chinese>=60;
查询数学>80或者语文>80的学生信息
select * from score where math>=80 or chinese>=80;
where子句
where 查询条件目的是从某个表中或多个表中筛选出符合条件的数据
比较运算符:
>大于 >=大于等于
<小于 <=小于等于
=等于 !=不等于
逻辑运算符
and 与
or 或
not 非
其他
in not in 字段的值在枚举范围内
between...and not between...and 字段的值在数字范围内
is null | is not null 字段的值不为空