安装就8说了
配置环境变量
需要配置MySQL目录下的bin目录到系统环境变量中,将路径复制到path环境变量中
ps:路径不能有中文
win+r打开命令控制台或者开始-运行-cmd
开启数据库:net start mysql57
关闭数据库:net stop mysql57
ps:如果拒绝访问,就管理员运行
使用命令连接数据库
mysql -h -u -p
- -h为主机地址,本地服务器地址为127.0.0.1或者localhost,如果连接自己电脑的mysql可以不用写-h
- -u用户名,没有改的话就是root
- -p密码
- 退出命令"quit"或"exit"
常用命令
show databases; #显示所有的数据库
create database java1905; #创建一个数据库 取名为 java1905
use java1905; #使用这个数据库
show tables; #查看当前数据库中的表
create table stu( #创建一个表 取名为 stu
-> stuid int(3) not null primary key auto_increment,#创建一列 为stuid 长度3位 不能为空 主键 自动增长
-> stuname varchar(10) not null); #学生姓名 varchar类型的 长度为10 实际长度以插入的数据为准 不能为空
这些命令可以在Navicat等数据库管理软件中使用,接下来的所有命令均为在Navicat中的查询下使用的。
结构化查询语句sql
//创建数据库
create datebase [if not exists] +数据库名字
//删除数据库
dorp database [if exists] +数据库名字(删库跑路一气呵成)
//查看数据库
show database
//使用数据库
use +数据库名字
数据库数据类型
mysql语句
- mySql语句中不严格区分大小写,
- 每个语句结尾以;分隔
- 注释用#
#新建一个名为test的数据库;
create database test;
#删除数据库
dorp database 数据库名;
#查看数据库
show database;
#使用数据库
use database;
#查看表
desc +表名;
#新建数据表
create table stu(
#创建一个学生编号 主键 自动增长
stuid int not null primary key auto_increment,
#学生的姓名 不能为空 注释为 学生姓名
stuname varchar(10) not null comment '学生姓名',
#学生年龄 默认值 18 注释 学生年龄
stuage int(3) default 18 comment '学生年龄'
);
插入新字段:使用insert语句
#表示插入一条数据 insert into+表名(这里写列名) values (写每一列对应的值)
#注意,列和值,必须一一对应
insert into stu(stuid,stuname,stuage) values (1,'张三',23);
#插入值的时候 主键可以不写值 有默认值的 或者 可以为null的也可以不写
insert into stu(stuname) values ('李四');
通常可以给表名、字段名两边加反引号,避免被系统识别为语句,注释comment内用也要加单引号
查询:使用select语句
#查询 select ,其中 * 表示查询所有的列; from stu 表示从stu表中查询;
select * from stu;
#查询 * 表示所有的列 需要查询哪一列 单独写 多个列之间使用逗号隔开,
select stuname,stuid from newstu;
#根据学生编号查询学生信息 || or && and
select stuname from newstu where stuid=1 or stuid=2;
#查询名字为张三 并且编号是1的人
select * from newstu where stuname='张三' and stuid=1;
#查询编号在 1-5之间的
select * from newstu where stuid between 1 and 5;
#一次插入多条语句 ,每个之间用英文逗号分隔
insert into newstu(stuname,birthday)
values
('lisi','2018-10-10 10:00:00'),
('王五','2018-10-10 10:00:00'),
('他二舅','2018-10-10 10:00:00'),
('他大舅','2018-10-10 10:00:00'),
('板凳','2018-10-10 10:00:00');
#删除 delete stuid等于1的项
delete from newstu where stuid=1;
alter语句,可以用来增加、修改、删除数据表
#修改表名stu为 newstu
alter table stu rename as newstu;
#给newstu添加一个日期类型的
alter table newstu add bir datetime comment '生日';
#修改生日列 类型为 varchar;modify只能更改列的数据类型,不能更改列名
alter table newstu modify bir varchar(10) ;
#修改列名
alter table newstu change bir birthday datetime;
#删除学生年龄 列
alter table newstu drop stuage;