当前位置:网站首页>MySQL book borrowing system project database creation TABLE statement (combined primary key and foreign key settings)

MySQL book borrowing system project database creation TABLE statement (combined primary key and foreign key settings)

2022-06-26 01:25:00 Mixed with bean curd and

Sword manual page 2 , Watch out for smelly women
/*
Switch database supperdatabase
*/

use supperdatabase;
SELECT * FROM `supperuser`

 Insert picture description here

Fuzzy query
Be careful % The matching position

select supperuser.address from supperuser where supperuser.address like " Henan %"

 Insert picture description here

select * from supperuser where supperuser.address like " Henan % zheng %"

 Insert picture description here

/*

**

Book borrowing system project database

**
datbases bookdb The specified character set is utf8
*/

create database  bookdb ENGINE=INNODB DEFAULT CHARACTER=utf8;

/*
Get into bookdb Lower operation
*/

use bookdb;

/*
bookinfo ( Book list )
*/

create table  bookinfo(
bookld int primary key default auto_increment COMMENT ' Book number ',
bookName varchar(50)  not null COMMENT ' Book name ',
bookWrite varchar(20) not null COMMENT ' Book author ',
bookContent text not null COMMENT ' Book Introduction ' 
)CHARACTER set=utf8;

/*
borrowInfo ( Loan information sheet )
*/

create table borrowInfo(
borrowld  int primary key auto_increment COMMENT ' Loan No ',
stuName  varchar(20) not null COMMENT ' The student's name ',
clazz  varchar(50) not null COMMENT ' class ',
tel   varchar(11) not null COMMENT ' contact number ',
borrowDate  date not null COMMENT ' Borrowing date ',
returnDate  date COMMENT ' Return date ',
status  int COMMENT ' Return status   The value is 0 or 1 0: Returned books  1: Outstanding books ',
bookld int  COMMENT ' Foreign keys , The number of the book borrowed '
)CHARACTER set=utf8;

/*ENGINE=INNODB DEFAULT CHARACTER=utf8; Set up table The character set is utf8 */

INSERT  into bookinfo VALUES(null," The romance of The Three Kingdoms "," Luo Guanzhong "," From the late Eastern Han Dynasty to the early Western Jin Dynasty ...");
INSERT  into bookinfo VALUES(null," Journey to the west "," Wu chengen "," After the birth of Monkey King and the havoc in heaven ...");
INSERT  into bookinfo VALUES(null," Water margin "," Shi Naian "," Liangshan hero resists oppression ...");
INSERT  into bookinfo VALUES(null," A dream of red mansions "," Cao xueqin "," The rise and fall of the four great families of Jia Shi Wang Xue ...");
INSERT  into borrowInfo VALUES(null," Zhang San ","2020 First class software ",18738171861,"2021-02-01","2021-02-09",0,1);
INSERT  into borrowInfo VALUES(null," Li Si ","2020 Class 2 of level 2 software ",18738171862,"2021-02-03","2021-02-07",0,2);
INSERT  into borrowInfo VALUES(null," Wang Wu ","2020 First class software ",18738171863,"2021-02-09",null,1,3);
INSERT  into borrowInfo VALUES(null," Zhang San ","2020 First class software ",18738171864,"2021-02-10",null,1,4);

/*
Query the table to view the inserted data
*/

select * from bookinfo;

select * from borrowInfo;

bookinfo surface
 Insert picture description here
borrowInfo surface
 Insert picture description here

/*
Set a single primary key for a single table or Without foreign keys
*/

use supperdatabase;
create table t3(
nid int   not null  auto_increment  primary key COMMENT ' Common primary key ' ,
pid int(11)  not   null ,
num int(11)  null
)CHARACTER set=utf8;
insert into t3 values(1,12,123);
insert into t3 values(DEFAULT,122,1234); /*default   The unique constraint is used with autoincrement */
insert into t3 values(DEFAULT,123,12345);

/*
see t3 Table data
*/

 select * from t3;

 Insert picture description here

/*
The settings match (2 individual ) Primary key Foreign keys (2 individual )
*/

create table t1(
nid int(11) not null auto_increment COMMENT' Common primary key ' ,
pid int(11) default null,
num int(11) default null,
primary key (nid,pid)
)CHARACTER set=utf8;
insert into t1 values(DEFAULT,12,123);
insert into t1 values(DEFAULT,123,1234); /*default   The unique constraint is used with autoincrement */
insert into t1 values(DEFAULT,124,12345);

/*
see t1 Table data
*/

select * from t1;

 Insert picture description here

create table t2(
id int(11) not null auto_increment primary key COMMENT ' Common primary key ',
/*pid int(11) default null,*/
id1 int,
id2 int ,
num int(11) default null,
CONSTRAINT fk_t2_t1 foreign  key (id1, id2) references t1(nid, pid) 
)CHARACTER set=utf8;

/*
When the primary foreign key is associated ,
When adding foreign key values It needs to be done according to the primary key value Add... In turn
*/

insert into t2 values(DEFAULT,1,12,DEFAULT);
insert into t2 values(DEFAULT,2,123,DEFAULT); /*default   The unique constraint is used with autoincrement */
insert into t2 values(DEFAULT,3,124,DEFAULT);

/*
see t2 Table data
*/

select *from t2;
 Insert picture description here

/*
see t1 Table structure
*/
desc t1;

 Insert picture description here
/*
see t2 Table structure
*/
desc t2;
 Insert picture description here

原网站

版权声明
本文为[Mixed with bean curd and]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252353285368.html