(base) jerry@jurandeMacBook-Pro ~ % mysql -uroot -p
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
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
mysql> exit;
Bye
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)
CREATE TABLE pet(
name varchar(20),
owner varchar(20),
species varchar(20),
sex char(1),birth DATE,death DATE
);
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| pet |
| user2 |
+----------------+
2 rows in set (0.01 sec)
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)
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)
mysql> INSERT INTO pet
-> values ('puffball','Diane','hamster','f','1999-03-30',NULL);
Query OK, 1 row affected (0.01 sec)
类型 | 大小 | 范围(有符号) | 范围(无符号) | 用途 |
---|---|---|---|---|
TINYINT | 1 byte | (-128,127) | (0,255) | 小整数值 |
SMALLINT | 2 bytes | (-32 768,32 767) | (0,65 535) | 大整数值 |
MEDIUMINT | 3 bytes | (-8 388 608,8 388 607) | (0,16 777 215) | 大整数值 |
INT或INTEGER | 4 bytes | (-2 147 483 648,2 147 483 647) | (0,4 294 967 295) | 大整数值 |
BIGINT | 8 bytes | (-9,223,372,036,854,775,808,9 223 372 036 854 775 807) | (0,18 446 744 073 709 551 615) | 极大整数值 |
FLOAT | 4 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) | 单精度 浮点数值 |
DOUBLE | 8 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) | 范围 | 格式 | 用途 |
---|---|---|---|---|
DATE | 3 | 1000-01-01/9999-12-31 | YYYY-MM-DD | 日期值 |
TIME | 3 | ‘-838:59:59’/‘838:59:59’ | HH:MM:SS | 时间值或持续时间 |
YEAR | 1 | 1901/2155 | YYYY | 年份值 |
DATETIME | 8 | 1000-01-01 00:00:00/9999-12-31 23:59:59 | YYYY-MM-DD HH:MM:SS | 混合日期和时间值 |
TIMESTAMP | 4 | 1970-01-01 00:00:00/2038 结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07 | YYYYMMDD HHMMSS | 混合日期和时间值,时间戳 |
类型 | 大小 | 用途 |
---|---|---|
CHAR | 0-255 bytes | 定长字符串 |
VARCHAR | 0-65535 bytes | 变长字符串 |
TINYBLOB | 0-255 bytes | 不超过 255 个字符的二进制字符串 |
TINYTEXT | 0-255 bytes | 短文本字符串 |
BLOB | 0-65 535 bytes | 二进制形式的长文本数据 |
TEXT | 0-65 535 bytes | 长文本数据 |
MEDIUMBLOB | 0-16 777 215 bytes | 二进制形式的中等长度文本数据 |
MEDIUMTEXT | 0-16 777 215 bytes | 中等长度文本数据 |
LONGBLOB | 0-4 294 967 295 bytes | 二进制形式的极大文本数据 |
LONGTEXT | 0-4 294 967 295 bytes | 极大文本数据 |
+----------+--------+---------+------+------------+------------+
| 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) ;
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';
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;
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.主表中的记录被子表使用时是不可以被删除的。
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
必须先满足第二范式,除开主键列的其他列之间不能传递依赖关系
学号
姓名
性别
出生年月日
所在班级
create table student(
sno varchar(20) primary key,
sname varchar(20) not null,
ssex varchar(10) not null,
sbirthday datetime,
class varchar(20)
);
教编号
教师名字
教师性别
出生年月日
职称
所在部门
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
);
课程号
课程名称
教师编号
create table course(
cno varchar(20) primary key,
cname varchar(20) not null,
tno varchar(20) not null,
foreign key(tno) references teacher(tno)
);
学号
课程号
成绩
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;