#---------------------------
#----cmd命令行连接MySql---------
cd C:\Program Files\MySQL\MySQL Server 5.5\bin
# 启动mysql服务器
net start mysql
# 关闭mysql服务器
net stop mysql
# 进入mysql命令行
mysql -h localhost -u root -p
或mysql -u root -p
#---------------------------
#----MySql用户管理---------
#修改密码:首先在DOS 下进入mysql安装路径的bin目录下,然后键入以下命令:
mysqladmin -uroot -p123 password 456;
#增加用户
#格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by '密码'
/*
如,增加一个用户user1密码为password1,让其可以在本机上登录, 并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入mysql,然后键入以下命令:
grant select,insert,update,delete on *.* to user1@localhost Identified by "password1";
如果希望该用户能够在任何机器上登陆mysql,则将localhost改为"%"。
如果你不想user1有密码,可以再打一个命令将密码去掉。
grant select,insert,update,delete on mydb.* to user1@localhost identified by "";
*/
grant all privileges on wpj1105.* to sunxiao@localhost identified by '123'; #all privileges 所有权限
#----------------------------
#-----MySql数据库操作基础-----
# 创建数据库
create database namage default character set utf8 collate utf8_general_ci;
# 如果数据库存在删除
drop database if exists manage;
# 进入数据库
use manage;
# 删除数据库
drop manage;
# 查看表的结构
desc class;
# 查看表内数据
select * from class;
# 创建班级表并添加字段:
create table class(
id int(10) not null auto_increment,
name varchar(30) not null default " noname",
add_time datetime no t null,
primary key(id)
)
ENGINE = INNODB charset=utf8;
# 1、向表内添加2条数据:如果 add_time 字段为datetime
insert into class(name,add_time) values ("一年级","2018-08-31 15:33");
insert into class(name,add_time) values ("二年级","2018-08-31 15:33");
# 2、向表内添加2条数据:如果 add_time 字段为timestamp
insert into class(name) values ("一年级");
insert into class(name) values ("二年级");
# 创建学生表并添加字段:
CREATE table student(
id int(10) not null primary key auto_increment unique, # unique唯一性,不可重复
name varchar(30) not null default "noname " comment "名称",
age int(10) not null default 0 comment "年龄",
birthday datetime not null comment "生日",
class_id int(10) ,
foreign key(class_id) references class(id)
);
# 向表内添加4条数据:
insert into student(name,age,birthday,class_id) values ("卢宇蒙",23,"1996-07-11",1);
insert into student(name,age,birthday,class_id) values ("王志敏",23,"1996-08-12",1);
insert into student(name,age,birthday,class_id) values ("赵广正",23,"1996-09-13",2);
insert into student(name,age,birthday,class_id) values ("古川",23,"1996-10-14",2);
# 创建分数表并添加字段: decimal(5,2) 5是有效长度,2是小数点后2位
create table course(
id int (10) not null primary key auto_increment,
name varchar(30) not null ,
score DECIMAL(5,2) not null,
class_id int(10) not null,
stu_id int (10) not null,
foreign key(class_id) references class(id),
foreign key (stu_id) references student(id)
);
# 向表内添加5条数据:
insert into course(name,score,class_id,stu_id) values ("数学",90.6,1,1);
insert into course(name,score,class_id,stu_id) values ("语文","135.44",1,5);
insert into course(name,score,class_id,stu_id) values ("英语","100",2,3);
insert into course(name,score,class_id,stu_id) values ("政治","98",1,2);
insert into course(name,score,class_id,stu_id) values ("历史","89.92",2,4);
完成后如图所示:
# 查找三张表里所有的数据:
SELECT * FROM student;
SELECT * FROM class;
SELECT * FROM course;
# 查询student表中id=1的name名
select name from student where id=1;
# 查询student表中name=“王志敏”的数据
select * from student where name = "王志敏";
# 查询student表中年龄大于15的数据
select id,name from student where age>"15";
and且;
# 查询student表中年龄大于15并且小于30的数据
select * from student where age>"15" and age<"30";
or 或;
# 查询student表中年龄大于15或小于30的数据
select * from student where age>"15" and age<"30";
between 之间;
# 查询student表中年龄在15和30之间的数据
select * from student where age > "15" between age > "30";
in 包含
# 查询指定集合内的数据
select * from student where id in (1,3,5);
排序
id升序 : select * from student order by id asc;
id降序 : select * from student order by id desc;
id 最大值: select max(id) from student;
生日最小值:select min(birth) from student;
id平均值: select avg(id) as '求平均' from student;
统计数据: select count(*) from student
名字统计: select count(name) from student;(如果为空不统计)
id的和: select sum(id) from student
查询第 i 条以后的 j 条数据(不包括第i条):select * from student limit 2,5; #从第3条开始的5条数据(3-8)