【MySQL数据库小白基础入门0611】

创建图书管理系统数据表
-- 创建图书管理系统数据表
use book;
show tables;
drop table bookinfo;
drop table bookcategory;
drop table reader;		/*删除之前创建练习的数据表*/
show tables;	/*查看是否已经完全删除*/

create table bookcategory(					/*创建bookcategory图书类别表*/
	category_id int primary key,			/*类别id设置为主键*/
    category varchar(20) not null unique,	/*添加唯一约束*/
    parent_id int not null					/*parent_id 非空值*/
    );

create table bookinfo(						/*创建bookinfo图书信息表*/
		book_id int primary key,
        book_category_id int,
        book_name varchar(20) not null unique,
        author varchar(20) not null,
        price float(5,2) not null,
        press varchar(20) default '机械工业出版社',	/*press出版社设置默认值*/
        pubdate date not null,
        store int not null,
        constraint fk_bcid foreign key(book_category_id) 	/*外键foreign key关联bookcategory类别表的类别id*/
        references bookcategory(category_id)
);
desc bookinfo;	/*查看信息表*/

create table readerinfo(						/*创建读者信息数据表*/
	card_id char(18) primary key,
    name varchar(20) not null,
    sex enum('男','女','保密') default '保密',
    age tinyint,								/*tinyint类型0-255*/
    tel char(11) not null,						/*char字符11个字节*/
    balance decimal(7,3) default 200			/*decimal定点数类型,7个精度,3个小数点*/
);

create table borrowinfo(
	book_id int,
    card_id char(18),
    borrow_data date not null,
    return_data date not null,
    status char(1) not null,
    primary key(book_id,card_id)			/*复合主键,book_id,card_id都是主键*/
);
show tables;	/*查看所有数据表*/

请添加图片描述

5 数据表记录的操作

5.1 单表数据的插入

-- 为表所有列插入数据
-- 语法格式:insert into table_name(column_list) values (value_list);
insert into 
bookcategory(category_id,category,parent_id)
values(1,'计算机',0);

-- 为表的指定列插入数据,未指定的默认值
insert into 
readerinfo(card_id,name,tel) 
values('210210199901011111','张飞','13777777777');

-- 同时插入多条记录
-- 语法格式:insert into table_name(column_lise) values(values_list1),(values_list2),......;
insert into
bookcategory(category_id,category,parent_id)
values(3,'编程语言',1),(4,'数据库',1),(5,'儿科学',2);

-- 查询结果插入到表中
/*insert 可将select 语句查询结果插入到表中
语法:insert into table_name(column_list1) 
select(column_list2) 
from table_name2 where(condition);
*/
CREATE TABLE test(id INT,NAME VARCHAR(10),pid INT);	/*创建test表*/
SHOW tables;
INSERT INTO
test(id,NAME,pid)
VALUES(5,'网页设计',1),(6,'口腔医学',2),
(7,'眼科学',2),(8,'骨科医学',2),(9,'临床医学',2);

insert into bookcategory
select * from test
where id>5;				/*向bookcategory中插入test表中id>5的数据*/

5.2 设置自动编号

-- 语法格式:列名+数据类型+auto_increment
create table bookcategory_tmp(
	category_id int primary key auto_increment,
    category varchar(20) not null unique,
    parent_id int not null
);

insert into 
bookcategory_tmp(category,parent_id) 	/*插入数据*/
values('计算机',0)
/*auto_increment=n 可以指定初始值*/
create table bookcategory_tmp1(
	category_id int primary key auto_increment,
    category varchar(20) not null unique,
    parent_id int not null
)auto_increment=5;
-- -------------------------------------------

-- 在已有表中创建自动编号
-- 语法格式:alter table+表名+modify+列名+类型+auto_increment;
alter table bookcategory
modify category_id int auto_increment;
-- 修改初始值
create table bookcategory(			/*创建一个含有自动编号的表*/
	category_id int primary key auto_increment,
    category varchar(20) not null unique,
    parent_id int not null
);
	
alter table bookcategory	/*修改自动编号的初始值,从3开始自增*/
auto_increment = 3;

-- 去掉自动编号
alter table bookcategory	/*修改表的方式*/
modify category_id int;	/*有关联关系的数据表,修改自动编号时,需要先去掉关联关系*/

alter table bookinfo 
drop foreign key fk_bcid;	/*删除bookinfo表下的外键,fk_bcid是关联名称*/

ALTER TABLE bookinfo 
ADD CONSTRAINT fk_bcid 
FOREIGN KEY(book_category_id)REFERENCES bookcategory(category_id);
/*bookinfo表中的book_category_id关联bookcategory表中的category_id*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值