問題1
create table t_test(
id int(4) not null primary key auto_increment,
time varchar(20) not null,
degree varchar(10) not null,
score double(3,1));
問題2
insert into t_test 1,'2005-05-09','優',98.1);
問題3
select degree,count(*),avg(score) from t_test group by degree;
+--------+----------+------------+
| degree | count(*) | avg(score) |
+--------+----------+------------+
| 優 | 1 | 98.00000 |
| 及格 | 1 | 89.50000 |
| 良 | 1 | 89.50000 |
+--------+----------+------------+
問題4
update t_test set time='2005-05-11' where degree='及格';
B
C
A
A
delete和truncate刪除數據的區別
truncate table test執行更快,清空物理文件,清空表中的所有內容
delete from test是邏輯刪除,按行刪除,而且可以通過where語句選擇要刪除的行
select * from A where time benteen '2006-01-01' and '2006-01-31';
create 創建表
alter 修改表
drop 刪除表
3306
A
C
C
BD
D
使用mysqldump備份
備份所有庫:[root@kongd ~]# mysqldump -uroot -p -A -B > all.db.sql
備份student庫:mysqldump -uroot -p -B student > student.sql
備份表:mysqldump -uroot -p student score > student_score.sql
恢復:
方法1: mysql -uroot -p < 備份文件
方法2:進入數據庫 source 備份文件
授權:grant all on *.* to smart identified by "123456";
重置密碼
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)
驗證:
停止:
[root@kongd ~]# yum install psmisc -y
[root@kongd ~]# killall -9 mysqld
mysqld: no process found
啓動:
[root@kongd ~]# systemctl start mariadb
[root@kongd ~]# mysql -uroot -p123456 -e 'show databases;'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
1、表:table1 (FId,Fclass,Fscore),用最高效最簡單的SQL列出各班成績最高的列表,顯示班級、成績兩個字段。
select Fclass,max(Fscore) from table1 group by Fclass;
select * from db1.abc order by db1.abc.uid desc limit 0,20;
使用mysqldump備份
備份test庫:mysqldump -uroot -p -B test > test.sql
恢復test庫:mysql -uroot -p < test.sql
select insert_time,count(*) from submit_message_send_histroy_201503 group by insert_time;
show open tables where In_use > 0;
update monitor_info set monitor_command="info01.log" where monitor_command="info.log";
根據題目條件,寫出相應的命令:
學生表:Student (Sno, Sname, Ssex , Sage, Sdept)
學號,姓名,性別,年齡,所在系 Sno爲主鍵
課程表:Course (Cno, Cname,)
課程號,課程名 Cno爲主鍵
學生選課表:SC (Sno, Cno, Score)
學號,課程號,成績 Sno,Cno爲主鍵
爲保證答案一致性,表中添加數據如下:
Student表插入數據
INSERT INTO Student
VALUES (1002, ‘李四’, ‘男’, 20, ‘英語’);
INSERT INTO Student
VALUES (1003, ‘王五’, ‘男’, 22, ‘計算機’);
INSERT INTO Student
VALUES (1004, ‘趙六’, ‘女’, 20, ‘英語’);
INSERT INTO Student
VALUES (1005, ‘錢七’, ‘男’, 21, ‘計算機’);
INSERT INTO Student
VALUES (1006, ‘孫八’, ‘男’, 20, ‘計算機’);
INSERT INTO Student
VALUES (1007, ‘胡九’, ‘女’, 19, ‘計算機’);
INSERT INTO Student
VALUES (1008, ‘武十’, ‘男’, 20, ‘計信管’);
Course表插入數據
INSERT INTO Course
VALUES (1, ‘英語’);
INSERT INTO Course
VALUES (2, ‘高數’);
INSERT INTO Course
VALUES (3, ‘計算機’);
INSERT INTO Course
VALUES (4, ‘單片機’);
INSERT INTO Course
VALUES (5, ‘java’);
INSERT INTO Course
VALUES (6, ‘MySQL’);
INSERT INTO Course
VALUES (7, ‘Linux’);
INSERT INTO Course
VALUES (8, ‘C++’);
SC表插入數據
INSERT INTO SC
VALUES (1001, 2, 45);
INSERT INTO SC
VALUES (1001, 3, 85);
INSERT INTO SC
VALUES (1001, 4, 74);
INSERT INTO SC
VALUES (1002, 1, 90);
INSERT INTO SC
VALUES (1003, 1, 54);
INSERT INTO SC
VALUES (1003, 2, 62);
INSERT INTO SC
VALUES (1003, 3, 81);
INSERT INTO SC
VALUES (1003, 4, 80);
INSERT INTO SC
VALUES (1003, 5, 72);
INSERT INTO SC
VALUES (1003, 6, 88);
INSERT INTO SC
VALUES (1003, 7, 77);
INSERT INTO SC
VALUES (1003, 8, 67);
INSERT INTO SC
VALUES (1004, 1, 92);
INSERT INTO SC
VALUES (1005, 1, 53);
INSERT INTO SC
VALUES (1005, 4, 63);
INSERT INTO SC
VALUES (1005, 7, 77);
INSERT INTO SC
VALUES (1006, 1, 70);
INSERT INTO SC
VALUES (1006, 2, 82);
INSERT INTO SC
VALUES (1006, 4, 78);
INSERT INTO SC
VALUES (1006, 6, 68);
INSERT INTO SC
VALUES (1006, 8, 55);
INSERT INTO SC
VALUES (1007, 2, 76);
INSERT INTO SC
VALUES (1007, 4, 90);
INSERT INTO SC
VALUES (1007, 6, 89);
INSERT INTO SC
VALUES (1008, 1, 81);
INSERT INTO SC
VALUES (1008, 2, 48);
1.用SQL語句創建學生表student,定義主鍵,姓名不能重名,性別只能輸入男或女,所在系的默認值是 “計算機”。
創建student表
create table student(sno int(10) primary key,sname varchar(20) unique,ssex varchar (10),ssage int(10),sdept varchar(20) default '計算機',check(ssex in ('男','女')));
創建course表
create table course(cno int(10) primary key,cname varchar(16)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
創建sc表
create table sc(sno int(10),cno int(10), score int(10),primary key (sno,cno),foreign key(sno) references student(sno),foreign key(cno) references course(cno));
2.修改student 表中年齡(age)字段屬性,數據類型由int 改變爲smallint。
alter table student 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 `Student` 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 student.sname,student.ssex,course.cno,sc.score from student,sc,course where student.sno=sc.sno and sc.cno=course.cno;
8.查詢不及格學生的姓名。
select sname,score from sc,student where sc.sno=student.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 student where sno=(select sno from (select sno,max(score) from sc where cno=2)t);