查看当前服务器下的所有数据库 show databases;
查看当前数据库下已有的数据表 show tables;
删除数据库 drop database db_name;
1.创建数据库 create database db_name;
2.打开数据库再创建表 use db_name;
3.创建数据库表
create table reader(
readerID char(10) not null primary key,
name varchar(8) not null,
sex char(2) default '男', --------默认值为男
birthdate datetime,
sfzh char(18) unique --------唯一键
)engine=innodb charset=utf8;
create table book(
bookID char(10) primary key,
name varchar(30) not null,
author varchar(10),
publish varchar(20),
price decimal(6,2) unsigned --------设置price 无符号(unsigned)也就是没有负号是正数>0
) engine=innodb charset=utf8;
create table borrow(
rID char(10),
bID char(10),
borrowdate datetime default now()
primary key(rID,bID), ------设置rID,bID为主键
foreign key(rID) references reader(readerID), ------设置外键rID,参照表reader表中的readerID
foreign key(bID) references book(bookID) ------设置外键bID,参照表book表中的bookID
) engine=innodb charset=utf8;
//修改字段数据类型
alter table reader modify profession char(30);
//在reader表增加字段profession数据类型为char(10)
alter table reader add profession char(10);
//在reader表中删除字段profession
alter table reader drop column profession;
//在表borrow中给borrowdate字段增加时间的默认值
alter table borrow modify column borrowdate datetime default now(); ----default默认值 为now()当前时间
//插入一条记录
insert into students(sno,sname,ssex,score,sbirthday,speciality,saddress)
values('20190801','陈昱任','男',666,'2001-06-25','移动应用开发','宁波市镇海区庄市街道');
//查看数据表结构
desc table_name;
//查看表内容
select * from students;
/*修改表的名字*/
alter table students rename student;
/*增加一列系*/
alter table student add sdept char(16);
/*给sdept增加唯一性约束*/
alter table student add unique(sdept);
/*修改学号长度为10位*/
alter table student modify sno char(10);
/*删除表*/
drop table 表名;
//查看sc表中的sno的外键名sc_ibfk_1
show create table sc;
//出现学号(sno)长度无法修改,解决步骤
1:删除外键关联
alter table sc drop foreign key sc_ibfk_1;
2:修改学号长度
alter table student modify sno char(10);
3:增加外键关联
alter table sc add foreign key(sno) references student(sno);
//删除某列字段
alter table student drop score;
//表的复制(结构)
create table student1 like student;
//表的复制(结构+数据)
create table student2 as select * from student;
//添加一条记录
insert into reader(readerId,name,sex,birthdate,sfzh)
values('98765432','asd','1989-04-25','222222222222222222')
//连续添加多条记录
insert into book(bookID,name,author,publish,price)
values('321','网页','万达','纺服',-20.5),
('321','网页','万达','纺服',-20.5),
('321','网页','万达','纺服',-20.5);
查询书名中包含“1%”这几个字的图书信息:图书编号,分类号,书名。
select bookid as ‘图书编号’ ,classid as ‘分类号’,bookname as ‘书名’
from Books
where bookname like '%1/%%' ESCAPE '/'
ESCAPE 对/后面的那个%进行转义
//查询选修了C01号课程的学生最高分和最低分
SELECT MAX(degree),MIN(degree) FROM sc WHERE cno='c01'
//查询学号为2017010112 的学生总成绩跟平均成绩
SELECT SUM(degree),Avg(degree) FROM sc WHERE sno=‘2017010112’
//查询有考试成绩的学生人数
SELECT COUNT(DISTINCT sno) FROM sc WHERE degree is not NULL
//统计各系女生人数
SELECT sdept,COUNT(*)FROM student GROUP BY sdept,ssex HAVING ssex=‘女’
ORDER BY xxx 升序(ASC)或降序(DESC)
2020数据库技术基础课程笔记
最新推荐文章于 2024-07-13 18:10:19 发布