飞天使-django之数据库简介
文章目录
- 增删改查
- 解决数据库不能存储中文问题
- 创建表
- 数据类型
- 表的基本操作
- 主键
- 唯一键 unique
- 外键
- 实战
增删改查
四个常用的语句查询 : insert delete update select
insert into student(Sno,name) values(95001,"张三")
delete from student where name='张三'
update student set name='李四' where sno = 95001
select sno,name from student
mysql> select * from student;
+-------+-------+
| sno | name |
+-------+-------+
| 95001 | sdgge |
| 95002 | tomas |
+-------+-------+
2 rows in set (0.00 sec)
mysql> select * from student;
+-------+-------+
| sno | name |
+-------+-------+
| 95001 | sdgge |
| 95002 | tomas |
+-------+-------+
2 rows in set (0.00 sec)
mysql> insert into student values(95003,'1faf');
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+-------+-------+
| sno | name |
+-------+-------+
| 95001 | sdgge |
| 95002 | tomas |
| 95003 | 1faf |
+-------+-------+
3 rows in set (0.01 sec)
mysql> update student set name='gsd' where sno=95003;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+-------+-------+
| sno | name |
+-------+-------+
| 95001 | sdgge |
| 95002 | tomas |
| 95003 | gsd |
+-------+-------+
3 rows in set (0.00 sec)
mysql> delete from student where sno=95002;
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+-------+-------+
| sno | name |
+-------+-------+
| 95001 | sdgge |
| 95003 | gsd |
+-------+-------+
2 rows in set (0.00 sec)
解决数据库不能存储中文问题
create database testdb default character set utf8 collate utf8_general_ci;
查看数据当前用的哪个编码
mysql> show variables like 'character_set_database';
+------------------------+---------+
| Variable_name | Value |
+------------------------+---------+
| character_set_database | utf8mb3 |
+------------------------+---------+
1 row in set (0.01 sec)
mysql> alter database testdb chatacter set utf8;
只影响之后创建的表
或者问chatgpt
创建表
创建表,create table
mysql> create table student
-> (
-> sno int,
-> name varchar(10),
-> gender varchar(10),
-> birtthday date,
-> mobile varchar(20),
-> email varchar(50),
-> homeaddress varchar(100)
-> );
Query OK, 0 rows affected (0.05 sec)
# 创建一个表Book
create table book
(
BookId int,
BookName varchar(100),
Author varchar(20),
Press varchar(100),
Price float(6,2)
)
下图显示的是如何用navicat进行命令进行查询语句
数据类型
如下图所示
使用的类型要满足你用的大小
尽量节约空间
字符串 长度明确的用 char
不明确的用varchar
表的基本操作
create table if not exists student
(
SNO int UNSIGNED,
SName varchar(20),
Gender char(3),
Mobile char(11),
Email varchar(100),
Adress varchar(200)
)
#修改表
alter table student add column studentdesc TEXT
# 删除表
drop table student
插入一些数据
INSERT INTO student(SNO, SName, Gender, Mobile, Email, Adress) VALUES (95001, '张三', '男', '1283833333', 'abc@gmail.com', '上海市某某路');
插入多行
INSERT INTO student(SNO, SName, Gender, Mobile, Email, Adress)
VALUES
(95001, '张三', '男', '1283833333', 'abc@gmail.com', '上海市某某路'),
(95002, '李四', '男', '1383833333', 'def@gmail.com', '北京市某某路'),
(95003, '王五', '女', '1483833333', 'ghi@gmail.com', '广州市某某路'),
(95004, '赵六', '女', '1583833333', 'jkl@gmail.com', '深圳市某某路'),
(95005, '钱七', '男', '1683833333', 'mno@gmail.com', '成都市某某路');
修改一条记录
UPdate student set Gender='男' where SNO=95001 Or Sname='张三'
update student set Gender='女' # 这句话会修改所有的
删除
delete from student where Gender='女';
查询
select * from student;
select SNo,SName where Mobile like '%888'
主键
上面表中SNO 有重复的, 对于关键字段不能保持唯一,主键可以做到
主键要唯一,且不能为空,主键只能有一个
create table student01
(
SNO int PRIMARY key,
SName varchar(20)
)
或者,一般用这种,这种有名字,方便管理
create table student02
(
SNO int,
Sname varchar(20),
CONSTRAINT Pk_SNO PRIMARY Key(SNO)
)
复合主键
create table borrowbook
(
sno int,
bookid int,
borrowdata date,
returndata date,
constraint pk_borrowbook primary key(sno,bookid)
)
需要两个行来一起捆绑一起来进行判断
唯一键 unique
保证字段的值不能重复,因为一个表只能有一个主键,其他的字段如果也想唯一,则需要唯一键
一个表中可以为多个
create table student03
(
sno int,
sname varchar(20) not null,
gender char(3),
birthday date,
mobile varchar(20),
email varchar(100),
address varchar(200),
constraint pk_sno primary key(sno),
constraint uq_mobile unique(mobile),
constraint uq_email unique(email)
)
插入10条数据
INSERT INTO student03 (sno, sname, gender, birthday, mobile, email, address) VALUES
(1, '张三', '男', '2000-01-01', '13838383838', 'zhangsan@example.com', '上海市某某路'),
(2, '李四', '男', '2000-02-02', '13939393939', 'lisi@example.com', '北京市某某路'),
(3, '王五', '男', '2000-03-03', '13636363636', 'wangwu@example.com', '广州市某某路'),
(4, '赵六', '女', '2000-04-04', '13737373737', 'zhaoliu@example.com', '深圳市某某路'),
(5, '钱七', '女', '2000-05-05', '13535353535', 'qianqi@example.com', '成都市某某路'),
(6, '孙八', '男', '2000-06-06', '13333333333', 'sunba@example.com', '重庆市某某路'),
(7, '周九', '男', '2000-07-07', '13232323232', 'zhoujiu@example.com', '武汉市某某路'),
(8, '吴十', '女', '2000-08-08', '13131313131', 'wushi@example.com', '南京市某某路'),
(9, '郑十一', '男', '2000-09-09', '13030303030', 'zhengshiyi@example.com', '西安市某某路'),
(10, '王十二', '女', '2000-10-10', '12929292929', 'wangshier@example.com', '杭州市某某路');
外键
保证数据的完整性
CREATE TABLE borrowbook1 (
sno INT,
bookid INT,
borrowdate DATE,
returndate DATE,
CONSTRAINT pk_borrowbook PRIMARY KEY (sno, bookid),
CONSTRAINT fk_sno FOREIGN KEY (sno) REFERENCES student03(sno),
CONSTRAINT fk_bookid FOREIGN KEY (bookid) REFERENCES book(bookid)
);
create table book
(
bookid int,
bookname varchar(100),
author varchar(20),
press varchar(50),
price float(8,2),
constraint pk_bookid primary key(bookid)
)
插入数据
根据您提供的信息,我将为每个表提供两条插入语句的示例。
对于"borrowbook1"表:
sql
INSERT INTO borrowbook1 (sno, bookid, borrowdate, returndate) VALUES
(1, 101, '2023-11-01', '2023-11-08'),
(2, 102, '2023-11-02', '2023-11-09');
上述示例插入了两条数据到"borrowbook1"表中。
对于"book"表:
sql
INSERT INTO book (bookid, bookname, author, press, price) VALUES
(101, '书籍1', '作者1', '出版社1', 29.99),
(102, '书籍2', '作者2', '出版社2', 39.99);
上述示例插入了两条数据到"book"表中。
对于"student03"表:
sql
INSERT INTO student03 (sno, sname, gender, birthday, mobile, email, address) VALUES
(1, '张三', '男', '2000-01-01', '13838383838', 'zhangsan@example.com', '上海市某某路'),
(2, '李四', '男', '2000-02-02', '13939393939', 'lisi@example.com', '北京市某某路');
上述示例插入了两条数据到"student03"表中。
请根据需要修改每条插入语句中的具体值。
如果插入一个学号为空的值,则报错,学号11是没有数据的在student3 表格中
sno和bookid 在 borrowbook1 表中是属于外键,所以插入之前会去student3 表格中去找下 sno 是不是有学号 11 ,没有则如下图一样报错
INSERT INTO borrowbook1 (sno, bookid, borrowdate, returndate) VALUES
(11, 101, '2023-11-02', '2023-11-05')
实战
create table student
(
sno int auto_increment comment '学号',
sname varchar(20) not null comment '姓名',
gender char(3) not null comment '性别',
birthday date comment '出生日期',
mobile varchar(20) comment '手机号',
email varchar(100) comment '邮箱地址',
address varchar(200) comment '家庭住址',
constraint pk_sno primary key(sno),
constraint uq_mobile unique(mobile),
constraint uq_email unique(email)
) auto_increment = 95001;
INSERT INTO student (sname, gender, birthday, mobile, email, address) VALUES ('张三', '男', '1995-01-01', '13812345678', 'zhangsan@example.com', '北京市东城区');
INSERT INTO student (sname, gender, birthday, mobile, email, address) VALUES ('李四', '女', '1996-02-02', '13987654321', 'lisi@example.com', '上海市浦东新区');
INSERT INTO student (sname, gender, birthday, mobile, email, address) VALUES ('王五', '男', '1997-03-03', '13611112222', 'wangwu@example.com', '广州市天河区');
CREATE TABLE book (
bookid INT AUTO_INCREMENT COMMENT '图书编号',
bookname VARCHAR(100) NOT NULL COMMENT '图书名称',
author VARCHAR(20) NOT NULL COMMENT '作者',
press VARCHAR(100) NOT NULL COMMENT '出版社',
price FLOAT(8, 2) NOT NULL COMMENT '图书价格',
CONSTRAINT pk_bookid PRIMARY KEY (bookid)
) AUTO_INCREMENT = 112;
INSERT INTO book (bookname, author, press, price) VALUES ('书籍1', '作者1', '出版社1', 29.99);
INSERT INTO book (bookname, author, press, price) VALUES ('书籍2', '作者2', '出版社2', 39.99);
INSERT INTO book (bookname, author, press, price) VALUES ('书籍3', '作者3', '出版社3', 49.99);
CREATE TABLE borrowbook
(
borrowid INT AUTO_INCREMENT COMMENT '借书编号',
sno INT COMMENT '学号',
bookid INT COMMENT '图书编号',
borrowdate DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '借书时间',
returndate DATETIME DEFAULT NULL,
CONSTRAINT pk_borrowbook PRIMARY KEY (borrowid),
CONSTRAINT fk_sno FOREIGN KEY (sno) REFERENCES student(sno),
CONSTRAINT fk_bookid FOREIGN KEY (bookid) REFERENCES book(bookid)
);
-- 插入示例数据
INSERT INTO borrowbook (sno, bookid) VALUES (95001, 112);
INSERT INTO borrowbook (sno, bookid) VALUES (95002, 113);
INSERT INTO borrowbook (sno, bookid) VALUES (95003, 114);