MySQL数据导入导出及查询

来源:互联网 发布:做网络主播有哪些条件 编辑:程序博客网 时间:2024/05/01 04:11

  

专业

班级

姓名

姓别

坐号

计算机网络

1班

张三

8

软件工程

2班

李四

12

计算机维护

1班

王五

9

计算机网络

2班

LILY

15

软件工程

1班

小强

20

计算机维护

1班

CoCo

18

Table: Score

姓名

英语

数学

语文

张三

65

75

98

李四

87

45

86

王五

98

85

65

LILY

75

86

87

小强

85

60

58

CoCo

96

87

70

Table: Record

姓名

记录

小强

迟到 

小强

事假

李四

旷课

李四

旷课

李四

迟到

CoCo

病假

LILY

事假

1. 用一条语句搜索出张三所在的班级及其坐号。

2. 用一条语句搜索出计算机维护1班所有人的考勤记录。

3. 用一条语句搜索有一科分数不及格而且有旷课记录的人的名单。

4. 导出数据库保存为“csdn-0913.sql”

 答案:

 

 

 

第一个表、

Enter password: ****

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 6

Server version: 5.6.13-enterprise-commercial-advanced MySQL Enterprise Server -

Advanced Edition (Commercial)

 

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| csdn               |

| mysql              |

| performance_schema |

| test               |

+--------------------+

5 rows in set (0.00 sec)

 

mysql> use test;

Database changed

mysql> drop table if exists student;

Query OK, 0 rows affected (0.31 sec)

 

mysql> drop table if exists class;

Query OK, 0 rows affected (0.11 sec)

 

mysql> create table class(专业 varchar(30),班级 varchar(20),姓名 varchar(20),

别 varchar(10),座号 int);

Query OK, 0 rows affected (0.70 sec)

 

mysql> desc class;

+--------+-------------+------+-----+---------+-------+

| Field  | Type        | Null | Key | Default | Extra |

+--------+-------------+------+-----+---------+-------+

专业   | varchar(30) | YES  |     | NULL    |       |

班级   | varchar(20) | YES  |     | NULL    |       |

姓名   | varchar(20) | YES  |     | NULL    |       |

性别   | varchar(10) | YES  |     | NULL    |       |

座号   | int(11)     | YES  |     | NULL    |       |

+--------+-------------+------+-----+---------+-------+

5 rows in set (0.01 sec)

 

mysql> insert into class values('计算机网络','1','张三','',8);

Query OK, 1 row affected (0.09 sec)

 

mysql> insert into class values('软件工程','2','李四','',12);

Query OK, 1 row affected (0.09 sec)

 

mysql> insert into class values('计算机维护','1','王五','',9);

Query OK, 1 row affected (0.08 sec)

 

mysql> insert into class values('计算机网络','2','LILY','',15);

Query OK, 1 row affected (0.08 sec)

 

mysql> insert into class values('软件工程','1','小强','',20);

Query OK, 1 row affected (0.08 sec)

 

mysql> insert into class values('计算机维护','1','CoCo','',18);

Query OK, 1 row affected (0.09 sec)

 

mysql> select * from class;

+-----------------+--------+--------+--------+--------+

专业            班级   姓名   性别   座号   |

+-----------------+--------+--------+--------+--------+

计算机网络      | 1班    张三   男     |      8 |

软件工程        | 2班    李四   男     |     12 |

计算机维护      | 1班    王五   男     |      9 |

计算机网络      | 2班    | LILY   | 女     |     15 |

软件工程        | 1班    小强   男     |     20 |

计算机维护      | 1班    | CoCo   | 女     |     18 |

+-----------------+--------+--------+--------+--------+

6 rows in set (0.00 sec)

 

第二个表、

 

 

 

mysql> create table score(姓名 varchar(20),英语 int,数学 int,语文 int);

Query OK, 0 rows affected (0.49 sec)

 

mysql> desc score;

+--------+-------------+------+-----+---------+-------+

| Field  | Type        | Null | Key | Default | Extra |

+--------+-------------+------+-----+---------+-------+

姓名   | varchar(20) | YES  |     | NULL    |       |

英语   | int(11)     | YES  |     | NULL    |       |

数学   | int(11)     | YES  |     | NULL    |       |

语文   | int(11)     | YES  |     | NULL    |       |

+--------+-------------+------+-----+---------+-------+

4 rows in set (0.01 sec)

 

mysql> insert into score values('张三',65,75,98);

Query OK, 1 row affected (0.09 sec)

 

mysql> insert into score values('李四',87,45,86);

Query OK, 1 row affected (0.04 sec)

 

mysql> insert into score values('王五'98,85,65);

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that

corresponds to your MySQL server version for the right syntax to use near '98,85

,65)' at line 1

mysql> insert into score values('王五',98,85,65);

Query OK, 1 row affected (0.08 sec)

 

mysql> insert into score values('LILY',75,86,87);

Query OK, 1 row affected (0.08 sec)

 

mysql> insert into score values('小强',85,60,58);

Query OK, 1 row affected (0.08 sec)

 

mysql> insert into score values('CoCo',96,87,70);

Query OK, 1 row affected (0.09 sec)

 

mysql> select * from score;

+--------+--------+--------+--------+

姓名   英语   数学   语文   |

+--------+--------+--------+--------+

张三   |     65 |     75 |     98 |

李四   |     87 |     45 |     86 |

王五   |     98 |     85 |     65 |

| LILY   |     75 |     86 |     87 |

小强   |     85 |     60 |     58 |

| CoCo   |     96 |     87 |     70 |

+--------+--------+--------+--------+

6 rows in set (0.00 sec)

 

 

第三个表、

 

 

mysql> create table record(姓名 varchar(20),记录 varchar(20));

Query OK, 0 rows affected (0.67 sec)

 

mysql> desc record;

+--------+-------------+------+-----+---------+-------+

| Field  | Type        | Null | Key | Default | Extra |

+--------+-------------+------+-----+---------+-------+

姓名   | varchar(20) | YES  |     | NULL    |       |

记录   | varchar(20) | YES  |     | NULL    |       |

+--------+-------------+------+-----+---------+-------+

2 rows in set (0.01 sec)

 

mysql> insert into record values('小强','迟到');

Query OK, 1 row affected (0.06 sec)

 

mysql> insert into record values('小强','事假');

Query OK, 1 row affected (0.09 sec)

 

mysql> insert into record values('李四','旷课');

Query OK, 1 row affected (0.11 sec)

 

mysql> insert into record values('李四','旷课');

Query OK, 1 row affected (0.08 sec)

 

mysql> insert into record values('李四','迟到');

Query OK, 1 row affected (0.10 sec)

 

mysql> insert into record values('CoCo','病假');

Query OK, 1 row affected (0.08 sec)

 

mysql> insert into record values('LILY','事假');

Query OK, 1 row affected (0.08 sec)

 

 

 

mysql> select * from record;

+--------+--------+

姓名   记录   |

+--------+--------+

小强   迟到   |

小强   事假   |

李四   旷课   |

李四   旷课   |

李四   迟到   |

| CoCo   | 病假   |

| LILY   | 事假   |

+--------+--------+

7 rows in set (0.00 sec)

 

 

 

1、用一条语句搜索出张三所在的班级及其坐号。

 

mysql> select 班级,座号 from class where 姓名 like '张三';

+--------+--------+

班级   座号   |

+--------+--------+

| 1班    |      8 |

+--------+--------+

1 row in set (0.08 sec)

2、用一条语句搜索出计算机维护1班所有人的考勤记录。

 

mysql> select class.专业,class.班级,class.姓名,record.记录 from class,record whe

re class.姓名=record.姓名 and class.专业 ='计算机维护' and class.班级 ='1';

+-----------------+--------+--------+--------+

专业            班级   姓名   记录   |

+-----------------+--------+--------+--------+

计算机维护      | 1班    | CoCo   | 病假   |

+-----------------+--------+--------+--------+

1 row in set (0.07 sec)

 

3、用一条语句搜索有一科分数不及格而且有旷课的人的名单。

 

 

mysql> select distinct score.姓名,score.英语,score.数学,score.语文,record.记录 f

rom score,record where score.姓名=record.姓名 and( score.英语<=60 or score.数学<

=60 or score.语文<=60) and record.记录='旷课';

+--------+--------+--------+--------+--------+

姓名   英语   数学   语文   记录   |

+--------+--------+--------+--------+--------+

李四   |     87 |     45 |     86 | 旷课   |

+--------+--------+--------+--------+--------+

1 row in set (0.00 sec)

 

4、导出数据库保存为“csdn-0913.sql

 

Microsoft Windows [版本 6.1.7601]

版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

 

C:\Users\Administrator>mysqldump -h localhost -u root -p test>d:\csdn-0913.sql

Enter password: ****

 

原创粉丝点击