一、南天软件
已知表t_test有如下记录
id | date | grade | score |
---|---|---|---|
1 | 2005-05-09 | 优 | 98.1 |
2 | 2005-05-09 | 优 | 98.2 |
3 | 2005-05-10 | 良 | 86.3 |
4 | 2005-05-10 | 良 | 75.1 |
5 | 2005-05-10 | 及格 | 68.2 |
1,写出构建t_test的语句
MariaDB [db1]> CREATE TABLE t_test(
-> id INT(4) NOT NULL PRIMARY KEY,
-> DATE VARCHAR(20) NOT NULL,
-> grade VARCHAR(5) NOT NULL,
-> score DOUBLE(3,1) NOT NULL);
2,写出插入t_test表内容的语句
MariaDB [db1]> insert into t_test values(1,'2005-05-09','优',98.1);
MariaDB [db1]> insert into t_test values(1,'2005-05-09','优',98.1);
MariaDB [db1]> insert into t_test values(2,'2005-05-09','优',98.2);
MariaDB [db1]> insert into t_test values(3,'2005-05-10','良',86.3);
MariaDB [db1]> insert into t_test values(4,'2005-05-10','良',75.1);
MariaDB [db1]> insert into t_test values(5,'2005-05-10','及格',68.2);
3,用一条语句查询优良各有多少人,平均分是多少
MariaDB [db1]> select grade,count(1),avg(score) from t_test group by grade;
+--------+----------+------------+
| grade | count(1) | avg(score) |
+--------+----------+------------+
| 优 | 2 | 98.15000 |
| 及格 | 1 | 68.20000 |
| 良 | 2 | 80.70000 |
+--------+----------+------------+
4,把及格日期改为2005-05-11
MariaDB [db1]> update t_test set date='2005-05-11' where grade='及格';
MariaDB [db1]> select * from t_test;
+----+------------+--------+-------+
| id | DATE | grade | score |
+----+------------+--------+-------+
| 1 | 2005-05-09 | 优 | 98.1 |
| 2 | 2005-05-09 | 优 | 98.2 |
| 3 | 2005-05-10 | 良 | 86.3 |
| 4 | 2005-05-10 | 良 | 75.1 |
| 5 | 2005-05-11 | 及格 | 68.2 |
+----+------------+--------+-------+
二、青牛软件
1,修改MySQL用户root密码的指令是
mysqladmin -u root password test
2,第一次启动MySQL初始化时,脚本mysql_install_db创建什么数据库
mysql和test
3,简述sql语句中Truncate和delete的区别
原文链接
4,NULL是指无任何值
5,当事务执行出现死锁时,系统执行ROLLBACK
操作
6,MySQL数据库的默认端口是3306
三、汉邦高科
1,MySQL备份命令
- 备份整个数据库
[root@centos ~]# mysqldump -uroot -p -A -B > all.db.sql
- 备份某一个数据库
[root@centos ~]# mysqldump -uroot -p -B student > student.sql
- 备份某一张表
[root@centos ~]# mysqldump -uroot -p student score > student_score.sql
- 恢复
方法1: mysql -uroot -p < 备份文件
方法2:进入数据库 source 备份文件
2,MySQL如何对smart用户授权访问,密码为123456
grant all on * to smart identified by "123456";
3,简述MySQL忘记密码该如何处理
--skip-grant-tables 跳过权限表,以下是10.4重置方式。
1)restart MariaDB with --skip-grant-tables
重启时跳过权限表
前提停止数据库:systemctl stop mariadb
mysqld --skip-grant-tables --user=mysql
2)login into the unprotected server
登录数据库:use mysql
3)run FLUSH PRIVILEGES
刷新权限表:FLUSH PRIVILEGES
4)run SET PASSWORD FOR root@localhost to change the root password
重置密码:SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');
5.5 重置root密码:
1) 停止数据库
[root@kongd ~]# systemctl stop mariadb
2) 启动时加上--skip-grant-tables 跳过权限表
[root@kongd ~]# mysqld_safe --skip-grant-tables --user=mysql
3) 登录数据库,修改密码
[root@kongd ~]# mysql
MariaDB [(none)]> update mysql.user
-> set password=password('123')
-> where User="root" and Host="localhost";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
四、MySQL语句示例
学生表:Student (Sno, Sname, Ssex , Sage, Sdept)
学号,姓名,性别,年龄,所在系 Sno为主键
课程表:Course (Cno, Cname,)
课程号,课程名 Cno为主键
学生选课表:SC (Sno, Cno, Score)
学号,课程号,成绩 Sno,Cno为主键
1.用SQL语句创建学生表student,定义主键,姓名不能重名,性别只能输入男或女,所在系的默认值是 “计算机”。
create table student1(sno int(10) primary key,sname varchar(20) unique,ssex varchar (10),ssage int(10),sdept varchar(20) default '计算机',check(ssex in ('男','女')));
create table course(cno int(10) primary key,cname varchar(16)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
create table sc(sno int(10),cno int(10), score int(10),primary key (sno,cno),foreign key(sno) references student1(sno),foreign key(cno) references course(cno));
2.修改student 表中年龄(age)字段属性,数据类型由int 改变为smallint。
alter table student1 alter column sage smallint;
3.为SC表建立按学号(sno)和课程号(cno)组合的升序的主键索引,索引名为SC_INDEX 。
create unique index sc_index on sc(sno asc,cno asc);
4.向student表添加一条纪录:200201,张三,男,21,计算机。
insert into student1 values(200201,'张三','男',21,'计算机');
5.选修了2号课程且成绩低于70的的学生每人成绩增加5分。
update sc set score=score+5 where cno=2 and score<=70;
6.删除选修了课程名称为“单片机”的学生的选课成绩记录。
delete from sc where cno=(select cno from course where cname='单片机');
7.创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。
create view stu_info as select student1.sname,student1.ssex,course.cno,sc.score from student1,sc,course where student1.sno=sc.sno and sc.cno=course.cno;
8.查询不及格学生的姓名。
select sname,score from sc,student1 where sc.sno=student1.sno and score<60;
9.查询选修四门以上课程的学生学号。
select sno from (select sno,count(*) num from sc group by sno)t where t.num>=4;
10.查询2号课程的最高分的学生的姓名。
select sname from student1 where sno=(select sno from (select sno,max(score) from sc where cno=2) t);