服务端操作:
$ mysql -uroot -p
#查看mysql数据库中user数据库表的三列内容: Host,User,Password.
MariaDB [(none)]> select Host,User,Password from mysql.user;
#172.25.254.197这台主机以root用户身份远程登录, 密码为westos, 访问数据库的所有内容(.)
MariaDB [(none)]> grant all on *. to root@'172.25.254.197' identified by 'westos';
#任意一台主机以root用户身份远程登录, 密码为westos, 访问数据库的所有内容(.*)
#sql语句中的%等价于.
MariaDB [(none)]> grant all on .* to root@'%' identified by 'westos';
#任意一台主机以root用户身份远程登录, 密码为westos, 访问mysql数据库的所有表(mysql.)
MariaDB [(none)]> grant all on mysql. to root@'%' identified by 'westos';
MariaDB [(none)]> select Host,User,Password from mysql.user;
#删除用户授权(远程登录)
MariaDB [(none)]> drop user root@'%';
客户端测试:
#指定主机名为172.25.254.18, 用户名为root远程登录.
$ mysql -h 172.25.254.18 -uroot -pwestos 创建数据库Blog并指定编码格式为utf8(存储的数据为中文, 需要设置);
MariaDB [(none)]> create database Blog default charset='utf8'; 显示所有数据库名称;
MariaDB [Blog]> show tables; 创建数据库表userinfo, 两列数据。
varchar可变长字符串, not null数据非空,unique数据唯一。
MariaDB [Blog]> create table userinfo(
-> username varchar(20) not null unique,
-> password varchar(20) not null)
-> ; 查看表结构
MariaDB [Blog]> desc userinfo; 添加数据到数据表中;
MariaDB [Blog]> insert into userinfo values('user2', 'passwd');
MariaDB [Blog]> insert into userinfo values('张三', 'passwd'); 数据查询;
MariaDB [Blog]> select * from userinfo; 修改表结构: 添加一列信息、修改一列信息、删除一列信息.
MariaDB [Blog]> alter table userinfo add gender varchar(3);
MariaDB [Blog]> alter table userinfo change gender sex varchar(3);
MariaDB [Blog]> alter table userinfo drop sex; 数据表重命名;
MariaDB [Blog]> rename table userinfo to users; 查看表的常见语句;
MariaDB [Blog]> show create table users; 删除数据表;
MariaDB [Blog]> drop table users; 删除数据库;
MariaDB [Blog]> drop database Blog;
MariaDB [(none)]> create database Blog default charset='utf8';
MariaDB [(none)]> use Blog;
MariaDB [Blog]> create table users(
-> id int primary key auto_increment,
-> username varchar(20) unique not null,
-> password varchar(20) not null default '000000');
MariaDB [Blog]> desc users;
MariaDB [Blog]> insert into users values(1, 'user1', 'password');
MariaDB [Blog]> insert into users(username) values('user2');
MariaDB [Blog]> insert into users(username) values('user3'),('user4'), ('user5');
MariaDB [Blog]> update users set password='666666' where username='user4';
MariaDB [Blog]> select from users where username='user4';
MariaDB [Blog]> select from users;
MariaDB [Blog]> delete from users where username='user4';
MariaDB [Blog]> select * from users;
create table student(
sno varchar(12) primary key,
sname varchar (10) comment '学生姓名',
sex varchar (2) comment '性别',
age int,
address varchar(50),
classno varchar (5)
); *******************************关于查询条件**********************************
1、 查询students表中的所有记录的sname、ssex和class列。
MariaDB [Blog]> select sname,ssex,class from students;
2、 查询教师所有的单位即不重复的Depart列。
MariaDB [Blog]> select distinct depart from teachers;
3、 查询students表的所有记录。
MariaDB [Blog]> select from students;
4、 查询scores表中成绩在60到80之间的所有记录。
MariaDB [Blog]> select from scores where degree between 60 and 80;
5、 查询scores表中成绩为85,86或88的记录。
MariaDB [Blog]> select * from scores where degree=85 or degree=86 or degree=88;
MariaDB [Blog]> select from scores where degree in (85,86,88);
6、 查询students表中“95031”班或性别为“女”的同学记录。(作业)
select from students where xxxxxx or xxxxx; *************************************关于排序******************************************* 7、 以class降序查询students表的所有记录。
MariaDB [Blog]> select from students order by class desc;
MariaDB [Blog]> select from students order by class;(默认升序)
8、 以cno升序、degree降序查询scores表的所有记录。
以cno升序、degree降序: 当cno相同时, 按照degree降序排列。
cno degree
1 3
1 2
2 3
MariaDB [Blog]> select * from scores order by cno,degree desc; ******************************************关于聚合函数*********************************** 9、 查询“95031”班的学生人数。
MariaDB [Blog]> select from students where class='95031';
MariaDB [Blog]> select count() from students where class='95031';
MariaDB [Blog]> select count(*) as studentCount from students where class='95031'; (最终版)
10、查询‘3-105’号课程的平均分。
MariaDB [Blog]> select avg(degree) as avgScore from scores where cno='3-105'; *****************************************关于group by 和having*************************** 11、查询scores表中至少有5名学生选修的并以3开头的课程的平均分数。
12、查询最低分大于70,最高分小于90的Sno列。
13、查询scores表中的最高分的学生学号和课程号。
14、查询所有学生的Sname、Cno和Degree列。
15、查询所有学生的Sno、Cname和Degree列。
16、查询所有学生的Sname、Cname和Degree列。
17、查询“95033”班所选课程的平均分。
USE Blog;
CREATE TABLE IF NOT EXISTS students
(sno VARCHAR(3) NOT NULL,
sname VARCHAR(4) NOT NULL,
ssex VARCHAR(2) NOT NULL,
sbirthday DATETIME,
class VARCHAR(5));
CREATE TABLE IF NOT EXISTS courses
(cno VARCHAR(5) NOT NULL,
cname VARCHAR(10) NOT NULL,
tno VARCHAR(10) NOT NULL);
CREATE TABLE IF NOT EXISTS scores
(sno VARCHAR(3) NOT NULL,
cno VARCHAR(5) NOT NULL,
degree NUMERIC(10, 1) NOT NULL);
CREATE TABLE IF NOT EXISTS teachers
(tno VARCHAR(3) NOT NULL,
tname VARCHAR(4) NOT NULL, tsex VARCHAR(2) NOT NULL,
tbirthday DATETIME NOT NULL, prof VARCHAR(6),
depart VARCHAR(10) NOT NULL);
INSERT INTO students (sno,sname,ssex,sbirthday,class) VALUES (108 ,'曾华' ,'男' ,'1977-09-01',95033);
INSERT INTO students (sno,sname,ssex,sbirthday,class) VALUES (105 ,'匡明' ,'男' ,'1975-10-02',95031);
INSERT INTO students (sno,sname,ssex,sbirthday,class) VALUES (107 ,'王丽' ,'女' ,'1976-01-23',95033);
INSERT INTO students (sno,sname,ssex,sbirthday,class) VALUES (101 ,'李军' ,'男' ,'1976-02-20',95033);
INSERT INTO students (sno,sname,ssex,sbirthday,class) VALUES (109 ,'王芳' ,'女' ,'1975-02-10',95031);
INSERT INTO students (sno,sname,ssex,sbirthday,class) VALUES (103 ,'陆君' ,'男' ,'1974-06-03',95031);
INSERT INTO courses(cno,cname,tno)VALUES ('3-105' ,'计算机导论',825);
INSERT INTO courses(cno,cname,tno)VALUES ('3-245' ,'操作系统' ,804);
INSERT INTO courses(cno,cname,tno)VALUES ('6-166' ,'数据电路' ,856);
INSERT INTO courses(cno,cname,tno)VALUES ('9-888' ,'高等数学' ,100);
INSERT INTO scores(sno,cno,degree)VALUES (103,'3-245',86);
INSERT INTO scores(sno,cno,degree)VALUES (105,'3-245',75);
INSERT INTO scores(sno,cno,degree)VALUES (109,'3-245',68);
INSERT INTO scores(sno,cno,degree)VALUES (103,'3-105',92);
INSERT INTO scores(sno,cno,degree)VALUES (105,'3-105',88);
INSERT INTO scores(sno,cno,degree)VALUES (109,'3-105',76);
INSERT INTO scores(sno,cno,degree)VALUES (101,'3-105',64);
INSERT INTO scores(sno,cno,degree)VALUES (107,'3-105',91);
INSERT INTO scores(sno,cno,degree)VALUES (108,'3-105',78);
INSERT INTO scores(sno,cno,degree)VALUES (101,'6-166',85);
INSERT INTO scores(sno,cno,degree)VALUES (107,'6-106',79);
INSERT INTO scores(sno,cno,degree)VALUES (108,'6-166',81);
INSERT INTO teachers(tno,tname,tsex,tbirthday,prof,depart) VALUES (804,'李诚','男','1958-12-02','副教授','计算机系');
INSERT INTO teachers(tno,tname,tsex,tbirthday,prof,depart) VALUES (856,'张旭','男','1969-03-12','讲师','电子工程系');
INSERT INTO teachers(tno,tname,tsex,tbirthday,prof,depart) VALUES (825,'王萍','女','1972-05-05','助教','计算机系');
INSERT INTO teachers(tno,tname,tsex,tbirthday,prof,depart) VALUES (831,'刘冰','女','1977-08-14','助教','电子工程系');