在linux虚拟机中,我们用service mysqld start命令来启动mysql服务。
启动mysql服务好之后,我们用mysql -u root -p命令来进入数据库。 然后输入你已经设置过的密码,如果还没有设置密码,就之间按回车键就可以进入数据库中了。
数据库中输入的命令要以 ";号 " 或 "\g " 结束。
数据库模式定义语言(DDL):
数据定义语言用于改变数据库结构,包括创建、更改和删除数据库对象。
"[]"方括号表示规则中的可选元素。方括号中的规则部分可以明确指定也可以省略。
对mysql数据库中的库进行操作:
查看mysql数据库中有多少个库的命令:
show databases;
创建一个数据库:库名(dbname)
create database [if not exists] dbname;
方括号中的内容表示如果存在将不在创建数据库;
删除一个数据库:
drop database [if exists] dbname;
对表进行操作:
1.创建表:表名(tbname)
create table tbname(字段 字段类型 [字段约束],字段 字段类型 [字段约束],...);
例如: create table stu(
id varchar(20) primary key comment "学号",
name varchar(20) not null comment "姓名",
age int default null comment '年龄',
sex enum("man","woman") comment "性别"
)ENGINE = InnoDB default CHARSET = utf8;
comment:注释,即注释字段是什么含义。它只会在查看表的内容时出现,在查看表的字段信息时不会出现。
2.查看当前数据库有多少个表:
show tables;
3.删除一个表:
drop table tbname;
例如:drop table biao;
4.查看表字段信息:
desc tbname
5.查看表的内容:
6.修改表的结构:alter tbname
1.修改字段的类型 modify
alter table tbname modify 字段 字段的类型;
例如:alter table stu modify name varchar(10);
2.修改字段的名称 change [字段类型] [字段约束];
alter table tbname change 旧字段名 新字段名 [字段类型] [字段约束];
例如:alter table stu change name mname varchar(100) default null;
3.添加一个字段 add
alter table tbname add 字段 字段的类型;
例如:alter table stu add score float;
4.删除一个字段 drop
alter table tbname drop 字段名;
例如:alter table stu drop score;
5.修改表名 rename
alter table 旧表名 rename 新表名;
例如:alter table stu rename student;
数据操纵语句(DML):
数据操纵语言用于检索、插入和修改数据。
1.添加数据 insert load source replace
insert into tbname(字段,字段,...) values(值,值..);
tbname中的字段的值和个数要与values中的值和个数一一对应。
tbname后面没有(),表示tbname(所有的字段)
例如:insert into stu(id,name,age,sex)
values("001","zhangsan",19,1);
insert into stu
values("003","wangwu",20,0);
2.删除数据 delete(删除后还能在日志中找回) truncate(删除后不能在日志中找回,建议不使用)
delete from tbanme [where];
例如:删除id="001"的数据
delete from stu where id = "001";
删除stu表中所有的数据
delete from stu;
3.修改数据 updata
update tbname set 字段 = 字段新的值 [where];
例如:修改stu表所有数据的sex
update stu set sex = "woman";
修改id="001"这条数据的age;
update stu set age = 15
where id = "001";
4.查询数据 select 也是SQL最重要的作用
1.普通查询
select 字段,字段,... from tbname [where];
例如:查询整个stu表的数据
select * from stu;
查看stu表中age>20,id和name的数据信息。
select id,name from stu where age > 20;
2.去重查询(重复的信息不显示) distinct
select distinct 字段 [,字段...]from tbname;
例如:查询表stu字段为age的信息
select distinct id,age from stu;
3.排序 order by asc升序(默认为升序) desc降序
select 字段[,字段,...] from tbname
order by 字段 升序或降序;
例如:查询根据age字段降序排序,显示所有字段信息。
select * from stu order by age desc;
4.分组 group by
例如: 把相同的id分为一组,然后计算score这个字段加起来的值
select id,SUM(score) from result group by id;
5.多表查询:
1.等值查询
例如:select name,score
from stu,result
where stu.id = result.id and age >= 20 and score < 60;
2.连接查询:
2.1外连接查询
1.左外连接查询 左表所查询到的数据都显示出来
select name,score
from (select id,name from stu where age >= 20) a
left join
(select id,score from result where score < 60) b
on a.id = b.id
where score is not null;
2.右外连接查询 右表所查询到的数据都显示出来
select name,score
from (select id,name from stu where age >= 20) a
right join
(select id,score from result where score < 60) b
on a.id = b.id
where name is not null;
3.全外连接查询 左右表查到的数据都显示
select name,score
from (select id,name from stu where age >= 20) a
full join
(select id,score from result where score < 60) b
on a.id = b.id;