1.     show databases:查看当前服务器下边有哪些库。

2.     create database NAME:创建一个数据库。

3.     drop database NAME:删除数据库。

4.     use NAME:选择数据库,再进行表行的相关操作时,要先使用该命令。

5.     show tables:查看库下面的所有表。

6.     drop table tableNAME:删除一张表。

         delete from msg where id=2:删除id=2的个体信息。

7.     rename table oldname to newname:修改表名。(但是不能修改数据库的名字)

8.     createtable sc (

    -> name varchar(20),

    -> age int(20),

    -> id int(20)

-> );

       :创建表格以及信息。

9.     desc tablename:查看表结构。

10.  \c:退出不执行。

11.  insert into c

    -> (name,age,id)

    -> values

-> ('张三','20',','171')

:增加表格信息。

12.  select* from tablename:查看信息。(*代表所有)

select * from tablename where sno=1:显示出sno=1的个体信息。

13.  INT命令将使得id域只能保存数字(整数)。

NOT NULL命令保证id域不能为空。

UNIQUE:值唯一。

PRIMARY KEY则指定id域作为数据表的键域。作为键域的域不能包含重复的数据。

AUTO_INCREMENT命令将自动分配递增的值到id域,尤其是将自动分配数字到对应域中。

CHAR(字符)和INT(整数)命令指定相关域中可存储的数据类型。命令旁的数字则指定对应域中可以包括多少字符或多大的整数。

14.   mysql> update table名字
    -> set
    -> id=2,
    -> content='当老大'
    -> where
    -> name='张三'

    -> ;

:修改信息。

15.select id,title from msg:、只查找msg的id列,title列。

select * from msg where id > 2:查看id>2的msg表中的信息。

select id,title
    -> from msg
    -> where
    -> id>3;
+------+---------+
| id   | title   |
+------+---------+
|    4 | 4标题   |
|    5 | 5标题   |
+------+---------+

2 rows in set (0.01 sec)

:查看msg中id大于3的id列,title列。



以下是三个表的信息提示:

mysql> select * from student
    -> ;
+------+--------+------+--------+------+
| sno  | sname  | ssex | sdept  | sage |
+------+--------+------+--------+------+
|    1 | 小明   | 男   | 软件   |   20 |
|    2 | 小亮   | 男   | 软件   |   21 |
|    3 | 小红   | 女   | 软件   |   19 |
+------+--------+------+--------+------+
3 rows in set (0.00 sec)

mysql> select * from sc;
+------+------+-------+
| sno  | cno  | grade |
+------+------+-------+
|    1 |    1 |    80 |
|    2 |    2 |    95 |
|    3 |    3 |    85 |
|    1 |    1 |    80 |
+------+------+-------+
4 rows in set (0.00 sec)

mysql> select * from course;
+------+--------+--------+------+
| cno  | cname  | credit | cpno |
+------+--------+--------+------+
|    1 | 语文   |      2 |    2 |
|    2 | 数学   |      3 |    3 |
|    3 | 英语   |      2 |    1 |
+------+--------+--------+------+

3 rows in set (0.00 sec)



select sno,cno,grade,grade-5 from sc;:查看一系列信息。

select max(grade),min(grade) as 最小成绩,sum(grade),avg(grade) from sc;:查看sc中grade的最大值,最小值(以最小成绩的形式显示出来),和,平均值。

select sno,cno,grade-5,90 from sc;:显示出sc的以上信息,最后一列显示的全部是90。

mysql学习知识点

select sno from sc where grade >=90;

select * from sc where cno in ('02','03');:显示出cno为02 03的全部要求内容。

select * from sc where cno not in ('02','03');:不显示出cno为02 03的全部要求内容。

select * from sc where cno ='02' or cno ='03';:显示出cno为02 03的全部要求内容。

select * from sc where sno like '%02';:显示出sc中sno以02结尾的内容。

select * from sc where sno like '15%';:显示出sc中sno以15开头的内容。

select * from sc where sno like '15__4';:显示出sc中sno以15开头以4结尾的内容。(中间两个_)

select * from sc where sno like '15%4';:显示出sc中sno以15开头以4结尾的内容。

select * from sc where cno like '02';:显示出sc中cno为02的内容。

select * from sc where cno = '02';:显示出sc中cno为02的内容。

select * from sc where cno like '%02';:显示出sc中cno为02的内容。

select * from sc where cno like '_2';:显示出sc中cno为02的内容。

mysql学习知识点

select * from student where sname not like '\%n' escape '\'';:查看全部信息。(使用escape,转义字符后面的%或_就不作为通配符了,注意前面没有转义字符的%和_仍然起通配符作用

select * from student where sname like '\%n' escape '\'';:查处的结果为空。

order by sno:按照sno的顺序排列。

order by sno desc:按照sno的反顺序排列。

order by sage desc,sno:sage反序(从大到小),sno正序(从小到大)排列。

mysql学习知识点

select count(*) from student;:查看学生的数量并显示。

select count(sage) from student;:在学生中查看sage的现有数量并显示。

select count(distinct sage) from student;:在学生中查看sage的现有不重复数量并显示。

select max(distinct sage) from student;:在学生中查看sage的现有不重复的最大值并显示。

select cno,avg(grade) from sc group by cno;:把cno分组并查看各组的成绩平均值。(如下图)

mysql学习知识点

select cno,avg(grade) from sc group by cno order by avg(grade);:从sc表中查看cno,成绩平均值并按cno分组并以平均值次序排列。

select sno from sc group by sno having count(*)>=3 order by avg(grade);:意思懂。

select cno,count(*) from sc group by cno having count(*)>=2;:意思懂。

select * from student,sc where student.sno=sc.sno;:从student表和sc表中查看学生表的sno等于sc表的sno的信息。

select * from student inner join sc on student.sno=sc.sno;:含义如下:

mysql> select * from sc;
+------+------+-------+
| sno  | cno  | grade |
+------+------+-------+
|    1 |    1 |    80 |
|    2 |    2 |    95 |
|    3 |    3 |    85 |
|    1 |    1 |    80 |
+------+------+-------+
4 rows in set (0.02 sec)
mysql> select * from student;
+------+--------+------+--------+------+
| sno  | sname  | ssex | sdept  | sage |
+------+--------+------+--------+------+
|    1 | 小明   | 男   | 软件   |   20 |
|    2 | 小亮   | 男   | 软件   |   21 |
|    3 | 小红   | 女   | 软件   |   19 |
+------+--------+------+--------+------+
3 rows in set (0.00 sec)
mysql> select * from student inner join sc on student.sno=sc.sno;
+------+--------+------+--------+------+------+------+-------+
| sno  | sname  | ssex | sdept  | sage | sno  | cno  | grade |
+------+--------+------+--------+------+------+------+-------+
|    1 | 小明   | 男   | 软件   |   20 |    1 |    1 |    80 |
|    2 | 小亮   | 男   | 软件   |   21 |    2 |    2 |    95 |
|    3 | 小红   | 女   | 软件   |   19 |    3 |    3 |    85 |
|    1 | 小明   | 男   | 软件   |   20 |    1 |    1 |    80 |
+------+--------+------+--------+------+------+------+-------+

4 rows in set (0.01 sec)

select * from student inner join sc on student.sno=sc.sno where ssex='女';:和上边代码结果一样,只不过只看女生。

select sc.sno,sname from student inner join sc on student.sno=sc.sno where ssex='女'  group by sc.sno,sname having count(*)>=2;: 含义如下:

mysql学习知识点

 select c1.cno,c1.cpno,c2.cno,c2.cpno
    -> from course c1,course c2

    -> where c1.cpno=c2.cno;:含义如下:

mysql学习知识点

( select c1.cno,c1.cpno,c2.cno,c2.cpno
    -> from course c1,course c2

    -> where c1.cpno=c2.cno is not null;):结果和上边的一样,就是不显示值为空的项。

select * from student s inner join sc on s.sno=sc.sno;结果如下:

mysql学习知识点

select * from student s left outer join sc on s.sno=sc.sno;:结果同上。

select * from sc join course c on sc.cno=c.cno;:结果如下:

mysql学习知识点

select * from sc right join course c on sc.cno=c.cno;:结果是把course表加到sc表的右边。(同上)

select * from sc left join course c on sc.cno=c.cno;:结果同上。

select * from course c left join sc on sc.cno=c.cno;:结果相反,先写哪个表,哪个表就在左边。

select sname from student s inner join sc on s.sno=sc.sno where grade>90 and cno='02';:根据意思可懂。

 select sname,cname,grade from student s,course c,sc where s.sno=sc.sno and c.cno=sc.cno;:如果如下:

mysql学习知识点

补充:

left join是以A表的记录为基础的,A可以看成左表,B可以看成右表,left join是以左表为准的.
换句话说,左表(A)的记录将会全部表示出来,而右表(B)只会显示符合搜索条件的记录(例子中为: A.aID = B.bID).

B表记录不足的地方均为NULL.

right join:和left join的结果刚好相反,这次是以右表(B)为基础的,A表不足的地方用NULL填充.

3.inner join(相等联接或内联接)

inner join:很明显,这里只显示出了 A.aID = B.bID的记录.这说明inner join并不以谁为基础,它只显示符合条件的记录.

left outer join:

无区别left join 是left outer join的简写,left join默认是outer属性的。

mysql> select sname from student where sno in
    -> (select sno from sc where grade>(select avg(grade) from sc));:查询成绩大于成绩平均数的学生的名字。(结果如下)
+--------+
| sname  |
+--------+
| 小亮   |
+--------+

1 row in set (0.01 sec)


select sno,sname from student where sno in (select sno from sc where cno=(select cno from course where cname='语文'));:查询选课为语文的同学的学号和姓名。
+------+--------+
| sno  | sname  |
+------+--------+
|    1 | 小明   |
+------+--------+

1 row in set (0.00 sec)


select s.sno,sname from student s,sc,course c
    -> where s.sno=sc.sno and c.cno=sc.cno and cname='语文';:查询选课为语文的学生的学号和姓名。
+------+--------+
| sno  | sname  |
+------+--------+
|    1 | 小明   |
|    1 | 小明   |
+------+--------+

2 rows in set (0.01 sec)


select sno,cno
    -> from sc x
    -> where grade>=
    -> (select avg(grade) from sc y where y.sno=x.sno);:找出每个学生超过他选修课程平均成绩的课程号。


select sname,sage
    -> from student
    -> where sage <
    -> (select max(sage) from student where sdept='软件');:查看年龄小于最大年龄且专业课为软件的学生的姓名和年龄。
+--------+------+
| sname  | sage |
+--------+------+
| 小明   |   20 |
| 小红   |   19 |
+--------+------+

2 rows in set (0.00 sec)


 select sname from student where not exists
    -> (select * from sc where sno=student.sno and cno='1');:看代码能懂。
+--------+
| sname  |
+--------+
| 小亮   |
| 小红   |
+--------+

2 rows in set (0.00 sec)


select sname
    -> from student
    -> where not exists

    -> (select * from course where not exists (select * from sc where sno=student.sno and cno=course.cno));:查询选修了全部课程的学生姓名。


 select sno,sname,sdept
    -> from student s1
    -> where exists
    -> (select * from student s2 where s2.sdept=s1.sdept and s2.sname='小明');:查询与“小明”在同一个系学习的学生。
+------+--------+--------+
| sno  | sname  | sdept  |
+------+--------+--------+
|    1 | 小明   | 软件   |
|    2 | 小亮   | 软件   |
|    3 | 小红   | 软件   |
+------+--------+--------+

3 rows in set (0.01 sec)


select sno from sc sc1
    -> where not exists
    -> (select * from sc sc2 where sno='2' and not exists (select * from sc sc3 where sc3.cno=sc2.cno));:查询至少选修了学生2选修的全部课程的学生号码。
+------+
| sno  |
+------+
|    1 |
|    2 |
|    3 |
|    1 |
+------+

4 rows in set (0.01 sec)


select distinct sno from sc sc1
    -> where not exists
    -> (select * from sc sc2 where sno='2' and not exists (select * from sc sc3 where sc3.sno=sc2.sno and sc3.cno=sc2.cno));:查询至少选修了学生2选修的全部课程的学生号码。(不重复)
+------+
| sno  |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)


create table s as select * from student where sno='1';:复制学生表里边编号为1的信息到s表。
Query OK, 1 row affected (0.12 sec)

Records: 1  Duplicates: 0  Warnings: 0


 insert into s select * from student where ssex='女';:向s表里加入学生表中性别为女的信息。
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0


mysql> select * from s;
+------+--------+------+--------+------+
| sno  | sname  | ssex | sdept  | sage |
+------+--------+------+--------+------+
|    1 | 小明   | 男   | 软件   |   20 |
|    3 | 小红   | 女   | 软件   |   19 |
+------+--------+------+--------+------+
2 rows in set (0.00 sec)


select * from student where sdept='软件' intersect select * from student where sage<=19;

select * from student where sdept='软件' and sage<=19;(intersect是两个sql查询出来的结果的交集

这两段代码意思一样。


select distinct sno
    -> from sc scx
    -> where not exists
    -> (select * from sc scy where scy.sno='2' and not exists (select * from sc scz where scz.sno=scx.sno and scz.cno=scy.cno));:意思是什么?
+------+
| sno  |
+------+
|    2 |
+------+

1 row in set (0.00 sec)


select * from student where ssex='女' union select * from student where sage<19;(union

是合并结果集的意思):将查找的两个结果合并。
+------+--------+------+--------+------+
| sno  | sname  | ssex | sdept  | sage |
+------+--------+------+--------+------+
|    3 | 小红   | 女   | 软件   |   19 |
+------+--------+------+--------+------+

1 row in set (0.00 sec)


create table dept_age (      :创建dept_age表。
    -> sdept varchar(15),
    -> avg_age smallint);

Query OK, 0 rows affected (0.05 sec)

 insert into student values     :向dept_age表中加入信息。
    -> ('15008','zs','0','computer',19);

Query OK, 1 row affected (0.01 sec)

 select * from student;
+-------+--------+------+----------+------+
| sno   | sname  | ssex | sdept    | sage |
+-------+--------+------+----------+------+
|     1 | 小明   | 男   | 软件     |   20 |
|     2 | 小亮   | 男   | 软件     |   21 |
|     3 | 小红   | 女   | 软件     |   19 |
| 15008 | zs     | 0    | computer |   19 |
+-------+--------+------+----------+------+

4 rows in set (0.00 sec)

insert into     将student表中的sdept分组并计算平均成绩,然后将sdept avg(age)复制到dept_age表中。
    -> dept_age
    -> (sdept,avg_age)
    -> select sdept,avg(sage)
    -> from student
    -> group by sdept;
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0


mysql> select * from dept_age;
+----------+---------+
| sdept    | avg_age |
+----------+---------+
| computer |      19 |
| 软件     |      20 |
+----------+---------+

2 rows in set (0.00 sec)


select distinct sno,grade from sc where sno in (select sno from student where sdept='软件');
+------+-------+
| sno  | grade |
+------+-------+
|    1 |    80 |
|    2 |    95 |
|    3 |    85 |
+------+-------+
3 rows in set (0.00 sec)


create table sc1 as select * from sc;
Query OK, 4 rows affected (0.12 sec)

Records: 4  Duplicates: 0  Warnings: 0

delete from sc1 where sno in (select sno from student where sdept='软件');:备份一个sc1表,并删除选课为软件的信息。

Query OK, 4 rows affected (0.00 sec)


 create view view_sc as select sno,grade from sc where sno in (select sno from student where sdept='软件');:(视图是存放数据的一个接口,也可以说是虚拟的表。这些数据可以是从一个或几个基本表(或视图)的数据。也可以是用户自已定义的数据。其实视图里面不存放数据的,数据还是放在基本表里面,基本表里面的数据发生变动时,视图里面的数据随之变动。
Query OK, 0 rows affected (0.01 sec)

mysql> select * from view_sc;
+------+-------+
| sno  | grade |
+------+-------+
|    1 |    80 |
|    2 |    95 |
|    3 |    85 |
|    1 |    80 |
+------+-------+

4 rows in set (0.00 sec)


EXCEPT 返回两个结果集的差(即从左查询中返回右查询没有找到的所有非重复值)。

INTERSECT 返回 两个结果集的交集(即两个查询都返回的所有非重复值)。

UNION返回两个结果集的并集。

create view s_c_sc as select s.sno,sname,c.cno,cname,grade from student s,course c,sc where s.sno=sc.sno and c.cno=sc.cno; 如下图:

mysql学习知识点

create view v_s as select sno,sname,sdept from student; 如下图:

mysql学习知识点

insert into s_c_sc
    -> values
    -> ('15004','小刚','4','物理','100');

ERROR 1394 (HY000): Can not insert into join view 'net1.s_c_sc' without fields list:为什么v_s能插入信息,而s_c_sc却不能能插入?

关于 create view s_m as select * from student where ssex='女' with check option(cheak option);如下图

mysql学习知识点

delete from s_m where sno='15010';如下图:

mysql学习知识点

mysql学习知识点


















        

相关文章: