Mysql学习

Mysql的基本使用

一 如何使用终端创建数据库

1.如何登陆数据库

(base) jerry@jurandeMacBook-Pro ~ % mysql -uroot -p

2.如何查询数据库服务器中所有的数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

3.如何选中某一个数据库进行操作

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

4.如何退出数据库

mysql> exit;
Bye

5.如何在数据哭服务器中创建新的数据库

create database test; #创建新的名字为test的数据库
mysql> use test; #切换至test数据库下
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;#显示test数据库下面的所有表单
+----------------+
| Tables_in_test |
+----------------+
| user2          |
+----------------+
1 row in set (0.00 sec)

6.创建一个新的数据表

CREATE TABLE pet(
  name varchar(20),
  owner varchar(20),
  species varchar(20),
  sex char(1),birth DATE,death DATE
);

7.查看数据表是否创建成功

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| pet            |
| user2          |
+----------------+
2 rows in set (0.01 sec)

8.查看数据表中的具体内容

mysql> describe pet;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | varchar(20) | YES  |     | NULL    |       |
| owner   | varchar(20) | YES  |     | NULL    |       |
| species | varchar(20) | YES  |     | NULL    |       |
| sex     | char(1)     | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| death   | date        | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.02 sec)

9.查看表中的记录

mysql> select * from pet;
Empty set (0.01 sec)
mysql> SELECT *FROM pet;
+----------+-------+---------+------+------------+-------+
| name     | owner | species | sex  | birth      | death |
+----------+-------+---------+------+------------+-------+
| puffball | Diane | hamster | f    | 1999-03-30 | NULL  |
+----------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)

10.如何在表中添加数据记录

mysql> INSERT INTO pet
    -> values ('puffball','Diane','hamster','f','1999-03-30',NULL);
Query OK, 1 row affected (0.01 sec)

11.Mysql 常用的数据类型有哪些

MySQL支持多种类型,大致可以分为三类:
  • 数值
类型大小范围(有符号)范围(无符号)用途
TINYINT1 byte(-128,127)(0,255)小整数值
SMALLINT2 bytes(-32 768,32 767)(0,65 535)大整数值
MEDIUMINT3 bytes(-8 388 608,8 388 607)(0,16 777 215)大整数值
INT或INTEGER4 bytes(-2 147 483 648,2 147 483 647)(0,4 294 967 295)大整数值
BIGINT8 bytes(-9,223,372,036,854,775,808,9 223 372 036 854 775 807)(0,18 446 744 073 709 551 615)极大整数值
FLOAT4 bytes(-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38)0,(1.175 494 351 E-38,3.402 823 466 E+38)单精度 浮点数值
DOUBLE8 bytes(-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308)0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308)双精度 浮点数值
DECIMAL对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2依赖于M和D的值依赖于M和D的值小数值
  • 日期/时间
类型大小 ( bytes)范围格式用途
DATE31000-01-01/9999-12-31YYYY-MM-DD日期值
TIME3‘-838:59:59’/‘838:59:59’HH:MM:SS时间值或持续时间
YEAR11901/2155YYYY年份值
DATETIME81000-01-01 00:00:00/9999-12-31 23:59:59YYYY-MM-DD HH:MM:SS混合日期和时间值
TIMESTAMP41970-01-01 00:00:00/2038 结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07YYYYMMDD HHMMSS混合日期和时间值,时间戳
  • 字符串(字符)类型
类型大小用途
CHAR0-255 bytes定长字符串
VARCHAR0-65535 bytes变长字符串
TINYBLOB0-255 bytes不超过 255 个字符的二进制字符串
TINYTEXT0-255 bytes短文本字符串
BLOB0-65 535 bytes二进制形式的长文本数据
TEXT0-65 535 bytes长文本数据
MEDIUMBLOB0-16 777 215 bytes二进制形式的中等长度文本数据
MEDIUMTEXT0-16 777 215 bytes中等长度文本数据
LONGBLOB0-4 294 967 295 bytes二进制形式的极大文本数据
LONGTEXT0-4 294 967 295 bytes极大文本数据

12.数据类型如何选择

多使用

13.如何插入以下数据到数据表

+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL       |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |
| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |
| Bowser   | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |
| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | NULL       |
| Whistler | Gwen   | bird    | NULL | 1997-12-09 | NULL       |
| Puffball | Diane  | hamster | f    | 1999-03-30 | NULL       |
| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL       |
+----------+--------+---------+------+------------+------------+
INSERT INTO pet VALUES('Fluffy', 'Harold', 'cat','f', '1993-02-04',NULL);
INSERT INTO pet VALUES( 'Claws' , 'Gwen', 'cat','m', '1994-03-17' ,NULL);
INSERT INTO pet VALUES( 'Buffy', 'Harold', 'dog','f', '1989-05-13' ,NULL) ;
INSERT INTO pet VALUES('Fang', 'Benny', 'dog', 'm', '1990-08-27' ,NULL);
INSERT INTO pet VALUES( 'Bowser', 'Diane', 'dog','m', '1979-08-31' , '1995-07-29' );
INSERT INTO pet VALUES( 'Chirpy', 'Gwen','bird','f', '1998-09-11' ,NULL) ;
INSERT INTO pet VALUES( 'Whistler', 'Gwen', 'bird' ,NULL, '1997-12-09' ,NULL);
INSERT INTO pet VALUES('Slim', 'Benny', 'snake', 'm', '1996-04-29' ,NULL);
INSERT INTO pet VALUES( 'Puffball', 'Diane' , 'hamster', 'f', '1999-03-30' ,NULL) ;

14.如何删除数据表中的数据

delete from pet where name='Fluffy';
delete from pet where name='Claws';
delete from pet where name='Buffy';
delete from pet where name='Fang';
delete from pet where name='Bowser';
delete from pet where name='Chirpy';
delete from pet where name='Whistler';
delete from pet where name='Slim';
delete from pet where name='Puffball';

15.如何修改数据

update pet set name = 'ryker' where owner = 'harold';

------------总结一下:数据表的常见操作-------------

增加

insert into pet values(.......);

删除

delete from pet where name = 'ryker';

修改

update pet set name = 'ryker' where owner = 'harold';

查询

select * from pet;

13.约束

主键约束:能够唯一确定一张表中的一条记录,也就是通过给某个字段添加约束,就可以使得该字段不能重复且不能为空。
create table user3(
  id int primary key,
  name varchar(20)
);
mysql>insert into user3 values(1,'张三');
Query OK, 1 row affected (0.00 sec)
mysql> select * from user3;
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
+----+--------+
1 row in set (0.00 sec)

mysql> insert into user3 values(1,'张三');
ERROR 1062 (23000): Duplicate entry '1' for key 'user3.PRIMARY'

第一次插入数据是没有问题的,但是第二次插入id名一样时就会报错,所以逐渐约束就是让该字段不允许为空也不允许重复。

create table user4(
  id int,
  name varchar(20),
  password varchar(20),
  primary key(id,name)      --联合组建,只要加起来不重复就可以,但是主键都不能为空
);
insert into user4 values(1,'张三','123');
insert into user4 values(2,'张三','123');
自增约束
create table user5(
  id int primary key AUTO_INCREMENT,
  name varchar(20)
);
insert into user5(name) values('张三');
mysql> insert into user5(name) values('张三');
Query OK, 1 row affected (0.00 sec)

mysql> insert into user5(name) values('张三');
Query OK, 1 row affected (0.01 sec)

mysql> insert into user5(name) values('张三');
Query OK, 1 row affected (0.00 sec)

mysql> insert into user5(name) values('张三');
Query OK, 1 row affected (0.00 sec)

mysql> select * from user5;
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
|  2 | 张三   |
|  3 | 张三   |
|  4 | 张三   |
+----+--------+
4 rows in set (0.00 sec)

创建表的过程中忘记创建约束怎么办?

create table user6(
  id int,
  name varchar(20)
);
alter table user6 add primary key(id);--用这个语句来添加主键约束
alter table user6 modify id int primary key;--使用modify添加主键约束
唯一约束:约束修饰的字段值不可以重复
create table user7(
  id int,
  name varchar(20)
);
alter table user7 add unique(name);
insert into user7 values(1,'张三');--插入两次就会报错,所以唯一约束的作用就是是添加字段元素不能重复

如何删除唯一约束

alter table user6 drop index name;

modify添加

alter table user6 modify name varchar(20) unique;
--总结
--1.建表的时候就添加约束
--2.可以使用alter。。。add。。。
--3.alter。。。modify。。。
--4.删除alter。。。drop。。。
非空约束:创建的字段不能为空
create table user8(
  id int not null
);--修饰的字段不能为空
默认约束
create table user9(
  id int,
  name varchar(20),
  age int default 10
);

mysql> desc user9;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| age   | int         | YES  |     | 10      |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)--传了值就不会使用默认值
外键约束

–涉及到两个表:父表,子表

–主表,副表

–班级

create table classes(
  id int primary key,
  name varchar(20)
);

–学生表

create table student(
  id int primary key,
  name varchar(20),
  class_id int,
  foreign key(class_id) references classes(id)--将主表里面的某一字段和子表的某一字段关联起来
);
insert into classes values(1,'一班');
insert into classes values(2,'二班');
insert into classes values(3,'三班');
insert into classes values(4,'四班');
--1.主表classes中没有数据值,在副表中,是不可以使用的。
--2.主表中的记录被子表使用时是不可以被删除的。

14.数据库的三大范式

第一范式

1NF

数据表中所有字段都是不可分割的原子值

create table user10(
  id int primary key,
  name varchar(20),
  address varchar(30)
);
insert into user10 values(1,'张三','中国四川省成都市武侯区武侯大道100号');
insert into user10 values(2,'李四','中国四川省成都市武侯区武侯大道200号');
insert into user10 values(3,'王五','中国四川省成都市武侯区武侯大道90号');
+----+--------+-----------------------------------------------------+
| id | name   | address                                             |
+----+--------+-----------------------------------------------------+
|  1 | 张三   | 中国四川省成都市武侯区武侯大道100|
|  2 | 李四   | 中国四川省成都市武侯区武侯大道200|
|  3 | 王五   | 中国四川省成都市武侯区武侯大道90|
+----+--------+-----------------------------------------------------+
3 rows in set (0.00 sec)--字段值还可以继续拆分的,就不能满足第一范式
--范式,设计得越详细,对于某些实际操作可能更好,但不一定都是好处。
--第一范式其实就是拆字段
第二范式

2NF

–不需满足第一范式的前提下,第二范式要求,除主键外的每一列都必须完全依赖主键。

–如果要出现不完全依赖,只可能发生在联合主键的情况下。

–订单表

create table myorder(
  product_id int,
  customer_id int,
  product_name varchar(20),
  customer_name varchar(20),
  primary key(product_id,customer_id)
);

–除主键外的其他列,只依赖于主键的部分字段

–拆表

create table myorder(
  order_id int primary key,
  product_id int,
  customer_id int
);
create table product(
  id int primary key,
  name varchar(20)
);
create table customers(
  id int primary key,
  name varchar(20)
);--分成三个表之后就满足了第二范式的设计
第三范式

3NF

必须先满足第二范式,除开主键列的其他列之间不能传递依赖关系

15.Mysql查询练习

学生表Student

学号

姓名

性别

出生年月日

所在班级

create table student(
  sno varchar(20) primary key,
  sname varchar(20) not null,
  ssex varchar(10) not null,
  sbirthday datetime,
  class varchar(20)
);
教师表Teacher

教编号

教师名字

教师性别

出生年月日

职称

所在部门

create table teacher(
  tno varchar(20) primary key,
  tname varchar(20) not null,
  tsex varchar(10) not null,
  tbiethday datetime,
  prof varchar(20) not null,
  depart varchar(20) not null
);
课程表Couse

课程号

课程名称

教师编号

create table course(
  cno varchar(20) primary key,
  cname varchar(20) not null,
  tno varchar(20) not null,
  foreign key(tno) references teacher(tno)
);

成绩表Score

学号

课程号

成绩

create table score(
  sno varchar(20) not null,
  cno varchar(20) not null,
  degree decimal,
  foreign key(sno) references student(sno),
  foreign key(cno) references course(cno),
  primary key(sno,cno)
);
往数据表中添加数据

–添加学生表数据

INSERT INTO student VALUES('101', '曾华', '男', '1977-09-01', '95033');
INSERT INTO student VALUES('102', '匡明', '男', '1975-10-02', '95031');
INSERT INTO student VALUES('103', '王丽', '女', '1976-01-23', '95033');
INSERT INTO student VALUES('104', '李军', '男', '1976-02-20', '95033');
INSERT INTO student VALUES('105', '王芳', '女', '1975-02-10', '95031');
INSERT INTO student VALUES('106', '陆军', '男', '1974-06-03', '95031');
INSERT INTO student VALUES('107', '王尼玛', '男', '1976-02-20', '95033');
INSERT INTO student VALUES('108', '张全蛋', '男', '1975-02-10', '95031');
INSERT INTO student VALUES('109', '赵铁柱', '男', '1974-06-03', '95031');

–添加教师表数据

INSERT INTO teacher VALUES('804', '李诚', '男', '1958-12-02', '副教授', '计算机系');
INSERT INTO teacher VALUES('856', '张旭', '男', '1969-03-12', '讲师', '电子工程系');
INSERT INTO teacher VALUES('825', '王萍', '女', '1972-05-05', '助教', '计算机系');
INSERT INTO teacher VALUES('831', '刘冰', '女', '1977-08-14', '助教', '电子工程系');

–添加课程表数据

INSERT INTO course VALUES('3-105', '计算机导论', '825');
INSERT INTO course VALUES('3-245', '操作系统', '804');
INSERT INTO course VALUES('6-166', '数字电路', '856');
INSERT INTO course VALUES('9-888', '高等数学', '831');

–添加成绩表数据

INSERT INTO score VALUES('103', '3-105', '92');
INSERT INTO score VALUES('103', '3-245', '86');
INSERT INTO score VALUES('103', '6-166', '85');
INSERT INTO score VALUES('105', '3-105', '88');
INSERT INTO score VALUES('105', '3-245', '75');
INSERT INTO score VALUES('105', '6-166', '79');
INSERT INTO score VALUES('109', '3-105', '76');
INSERT INTO score VALUES('109', '3-245', '68');
INSERT INTO score VALUES('109', '6-166', '81');
十大查询练习

–1.查询student表中的所有记录。

select * from student;

–2.查询student表中的所有记录的sname/ssex/class列。

select sname,ssex,class from student;

–3. 查询教师所在单位即不重复的depart列。

–distinct排除重复

mysql> select distinct depart from teacher;
+-----------------+
| depart          |
+-----------------+
| 计算机系         |
| 电子工程系       |
+-----------------+
2 rows in set (0.00 sec)

–4.查询sore在60到80之间的记录。

–查询区间 between 。。。and 。。。

mysql> select * from score where degree >60 and degree<80;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 105 | 3-245 |     75 |
| 105 | 6-166 |     79 |
| 109 | 3-105 |     76 |
| 109 | 3-245 |     68 |
+-----+-------+--------+
4 rows in set (0.00 sec)

–5.查询score中成绩为85,86或88的记录。

mysql> select * from score where degree in(85,86,88);
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-245 |     86 |
| 103 | 6-166 |     85 |
| 105 | 3-105 |     88 |
+-----+-------+--------+
3 rows in set (0.00 sec)

–6.查询student表中‘95031’班或性别为‘女’的同学记录

mysql> select * from student where class='95031' or ssex ='女';
+-----+-----------+------+---------------------+-------+
| sno | sname     | ssex | sbirthday           | class |
+-----+-----------+------+---------------------+-------+
| 102 | 匡明      || 1975-10-02 00:00:00 | 95031 |
| 103 | 王丽      || 1976-01-23 00:00:00 | 95033 |
| 105 | 王芳      || 1975-02-10 00:00:00 | 95031 |
| 106 | 陆军      || 1974-06-03 00:00:00 | 95031 |
| 108 | 张全蛋    || 1975-02-10 00:00:00 | 95031 |
| 109 | 赵铁柱    || 1974-06-03 00:00:00 | 95031 |
+-----+-----------+------+---------------------+-------+
6 rows in set (0.01 sec)

–7.以class降序查询student表的所有记录。

–升序asc,降序desc

mysql> select * from student order by class desc;
+-----+-----------+------+---------------------+-------+
| sno | sname     | ssex | sbirthday           | class |
+-----+-----------+------+---------------------+-------+
| 101 | 曾华      || 1977-09-01 00:00:00 | 95033 |
| 103 | 王丽      || 1976-01-23 00:00:00 | 95033 |
| 104 | 李军      || 1976-02-20 00:00:00 | 95033 |
| 107 | 王尼玛    || 1976-02-20 00:00:00 | 95033 |
| 102 | 匡明      || 1975-10-02 00:00:00 | 95031 |
| 105 | 王芳      || 1975-02-10 00:00:00 | 95031 |
| 106 | 陆军      || 1974-06-03 00:00:00 | 95031 |
| 108 | 张全蛋    || 1975-02-10 00:00:00 | 95031 |
| 109 | 赵铁柱    || 1974-06-03 00:00:00 | 95031 |
+-----+-----------+------+---------------------+-------+
9 rows in set (0.00 sec)

–8.以cno升序/degree降序查询score表中的记录

mysql> select * from score order by cno asc,degree desc;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-105 |     92 |
| 105 | 3-105 |     88 |
| 109 | 3-105 |     76 |
| 103 | 3-245 |     86 |
| 105 | 3-245 |     75 |
| 109 | 3-245 |     68 |
| 103 | 6-166 |     85 |
| 109 | 6-166 |     81 |
| 105 | 6-166 |     79 |
+-----+-------+--------+
9 rows in set (0.00 sec)

–9.查询‘95031’班的学生人数。

–统计

mysql> select count(*) from student where class = '95031';
+----------+
| count(*) |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)

–10.查询score表中的最高分的学生学号和课程号。(自查询或者排序)

mysql> select sno,cno from score where degree = (select max(degree) from score);
+-----+-------+
| sno | cno   |
+-----+-------+
| 103 | 3-105 |
+-----+-------+
1 row in set (0.00 sec)
--1.找到最高分
select max(degree) from score;
--2.找最高分的sno和cno
select sno,cno from score where degree = (select max(degree) from score);

–排序的做法:

select sno,cno,degree from score order by degree limit 0,1;

二 如何使用可视化工具操作数据库

三 如何在编程语言中操作数据库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值