小白学习MySQL的第一天——入门篇

203 阅读4分钟

MySQL简介:

MySQL是一个关系型数据库管理系统,在 WEB 应用方面,MySQL是最好的 RDMS (Relational Database Management System,关系数据库管理系统) 应用软件,它是由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品,MySQL 是最流行的关系型数据库管理系统中的一个。 MySQL官网下载链接:MySQL

SQL: 结构化查询语言, 是所有关系型数据库都要遵循的规范

SQL分类:

SQL分类可以分成四类:分别为DDL,DML,DQL,DCL。

DDL: 数据定义语言:简称DDL(Data Definition Language) 作用: 用来定义数据库对象:数据库,表,列/字段等。 关键字: create,drop,alter等

DML: 数据操作语言:简称DML(Data Manipulation Language) 作用:用来对数据库中表的记录进行更新。 关键字: insert,delete,update等

DQL: 数据查询语言:简称DQL(Data Query Language) 作用:用来查询数据库中表的记录。 关键字: select,from,where等

DCL: 数据控制语言:简称DCL(Data Control Language) 用来定义数据库的访问权限和安全级别,及创建用户。

MySQL数据库使用

接下来便是开始学习使用一些基础的SQL语句。

对数据库增删改查操作

# 创建一个数据库: create database [if not exists] 数据库名;
#示例
create database student;
# 删除数据库: drop database 数据库名;
# 示例:
drop database student;
# 使用/切换数据库:  use 数据库名;
use student;
# 查看所有的数据库名:
show databases;
# 查看当前使用的数据库: 
select database();
# 查看指定库的建库语句:  show create database 数据库名;
show create database student;

对数据库中表增删改查操作

# 创建表: create table [if not exists] 表名(字段1名 字段1类型 [字段1约束] , 字段2名 字段2类型 [字段2约束] ...);
create table stu(id int,name varchar(10),age int);
# 插入数据记录: insert into 表名 (字段名...) values (具体值...) , (具体值...);
insert into stu(id,name,age) values(1,'张三',18);
# 删除表: drop table [if exists] 表名;
drop table stu;
修改表名: rename table 旧表名 to 新表名;  
rename table test1 to stu;
# 查看所有表:
show tables;
# 查看表结构: desc 表名;
desc stu;
# 查看指定表的建表语句:  show create table 表名;
 show create table stu;

修改表中字段(增删改)

# 添加字段:  alter table 表名 add 字段名 字段类型 [字段约束];
alter table student add weight float;
# 删除字段:  alter table 表名 drop 字段名;
alter table student drop weight;
# 修改字段名和字段类型:  alter table 表名 change 旧字段名 新字段名 字段类型 [字段约束];
alter table student change id sid int;
# modify只修改字段类型: alter table 表名 modify 字段名 字段类型 [字段约束];
alter table student modify weight double;

表中记录操作(删改)

# 删除数据记录: delete from 表名 [where 条件]; 注意: 如果没有加条件就是删除所有数据
delete from stu;
修改数据记录: update 表名 set 字段名=值 [where 条件];
			注意: 如果没有加条件就是修改对应字段的所有数据
update stu set id=100;

基础查询

# 创建了一个student
create table student(
    id int primary key ,   
    name varchar(10),
    chinese double,
    english double,
    math double
);
# 查询表中所有学生的信息;
select * from student;
# 查询表中所有学生的姓名和对应的英语成绩;
select name,english from student;
# 去除语文成绩值的重复数据
select distinct chinese from student;
# 统计每个学生的总分
select name,(chinese+english+math) from student;
# 在所有学生总分数上加10分特长分
select name,(chinese+english+math)+10 from student;
# 查询总分大于200分的所有同学
select name from student where (chinese+english+math)>200;

# 查询英语分数在 80-90之间的同学
select name from student where english between 80 and 90;
select name from student where english>=80 and english<=90;
# 查询英语分数不在 80-90之间的同学
select name from student where english not between 80 and 90;
select name from student where english<80 or english>90;
# 查询英语成绩大于90分的同学
select name from student where english > 90;
# 查询所有姓李的学生英语成绩
select name,english from student where name like '李%';
# 查询英语80或者总分200的同学
select * from student where english = 80 or (chinese+english+math)=200;
# 查询数学分数为89,91的同学;
select * from student where math in (80,91);

以上就是一些常见的SQL语句的使用,当然本人也是“在学者”,上述例子也是本人在某培训过程中的一些小练习,如有不足之处,欢迎指正,如有新知识补充,欢迎大家在评论区评论大家一起学习。

大家一同进步!!