数据库基础

1.创建数据库
create datebase 数据库名称;

2.查看已经存在的数据库
show datebases;

3.查看已经创建好的数据库的信息
show create datebase 数据库名称;

4.修改数据库编码
alter datebase 数据库名称 default character set 编码方式 collate 编码方式_bin;
例子:将数据库mydb的编码修改为gbk
alter datebase 数据库名称 default character set gbk collate gbk_bin;

5.删除数据库(含数据库下所有表格)
drop datebase 数据库名称;

6.创建表
create table 表名
(
	字段名1 数据类型 [完整性约束条件],
	字段名2 数据类型 [完整性约束条件],
	...
	字段名n 数据类型 [完整性约束条件]
);
例子:创建一个学生表student
create datebase mydb;
use mydb;//选定数据库
create table student
(
	id int(10),
	name varchar(20),
	age int(3)
);

7.查验数据表是否创建成功
show tables;

8.查看数据表的两种方式:
show create table 表名;
describe 表名;
desc 表名;//简写形式

【数据表的基本操作(9-14点)】
9.修改表名
alter table <旧表名> rename [to] <新表名>;
例子:将数据库mydb的student表改为tb_stu表
use mydb;//指定数据库
show tables;//显示数据表
alter table student rename to tb_stu;

10.修改字段名
alter table <表名> change <旧字段名> <新字段名> <新数据类型>;
例子:将数据表tb_stu中的name字段改为sname,数据类型不变
desc tb_stu;//查看数据表
alter table tb_stu change name sname varchar(20);

11.修改字段的数据类型
alter table <表名> modify <字段名> <新数据类型>;
例子:将数据表tb_stu中的id字段的数据类型改为int(15)
alter table tb_stu modify id int(15);

12.添加字段
alter table <表名> add <新字段名> <数据类型> [约束条件] [first|after 已存在的字段名];
//[first|after 已存在的字段名]说明:first可选参数,将新字段添加到表的第一个字段;after可选参数,将新字段添加到已存在的字段的后面
例子:在数据表tb_stu中添加一个没有约束条件的garde字段,数据类型为float
alter table tb_stu add garde float after sname;

13.删除字段
alter table <表名> drop <字段名>;
例子:删除tb_stu中garde字段
alter table tb_stu drop garde;

14.修改字段的排列位置
alter table 表名 modify 字段名1 字段类型 first|after 字段名2;
说明:修改字段名1的字段位置。first可选参数,将字段1修改为表的第一个字段;after 字段名2 将字段1插入到字段2的后面。
例子:将数据表tb_stu中的sname字段改为表的第一个字段
alter table tb_stu modify sname varchar(20) first;

15.删除表(同时删除表的结构、表中的数据,建议删除之前先备份)
drop table 表名;

【表的约束】
1.主键约束(primary key):是一个列或列的组合,其值是能唯一地标识出表的每一行。
指定单字段主键:
<字段名> <数据类型> primary key;
指定多字段主键:
primary key [字段1,字段2,...,字段n]

2.非空约束(not null):限制该列取值不能为空。
<字段名> <数据类型> not null;

3.唯一约束(unique key):要求该列唯一,允许为空,但只能出现一个空值。
<字段名> <数据类型> unique;

4.默认约束(default 默认值):表中插入一条记录时,若该值未赋值,则系统自动插入默认值。
<字段名> <数据类型> default <默认值>;

例子1:在mysb数据库中创建student表,含学号sid、姓名name、年龄age、地址address,要求如下,
学号主键约束,自动增加;
姓名唯一约束
年龄默认约束,默认20
地址非空约束
create table student
(
	sid int primary key auto_increment,
	name varchar(20) unique,
	age int default 20,
	address varchar(50) not null
)

例子2:在mydb数据库中创建course表,字段属性如下,
课程号	courseid	int	复合主键
学号	sid		int	复合主键
成绩	garde		float
create table course
(
	courseid int,
	sid int
	garde float,
	primary key(courseid,sid)
)

【数据的增删改】
1.添加数据
1.1指定字段名的插入语法:
插入一条记录:
insert into 表名(字段1,字段2,...)
values(值1,值2,...);
例子:
insert into student(name,age,sid)
values('suxi',18,1);

同时添加多条记录:
insert into 表名[(字段1,字段2,...,字段n)]
values	(值1,值2,...,值n),
	(值1,值2,...,值n),
		...
	(值1,值2,...,值n);
例子:
insert into student(name,age,sid)
values	('xiaozhang',18,5),
	('xiaochen',19,6);

1.2不指定字段名的插入语法,添加值的顺序必须和字段在表中定义的顺序相同:
insert into 表名 values(值1,值2,...);
例子:
insert into student values(3,'qiaozhi',14);

2.更新数据
update 表名
set 字段1=新值1[,字段2=新值2,...]
[where 条件];
例子:
update student
set name='cici',age=25
where sid=1;

3.删除数据
delete from 表名 [where 条件];
truncate [table] 表名;//删除表中所有数据
例子:
delete from student where sid=8;
delete from student;//删除所有记录

4.查询数据
select [distinct] *|字段1[,字段2,...字段n]  //distinct 可选参数,剔除查询结果中重复的数据
from 表名
[where 条件]
[group by 列名 [having 条件]]  //group by 将查询结果按指定的列进行分组
[order by 列名 [asc|desc],...] //order by 将查询结果按指定的列进行排序 asc升序 desc降序
[linit [offset] 记录数];	   //linit 限制查询结果的行数 offset偏移量 offset为0从第1条记录开始,offset为1从第2条记录开始

select * from 表名;
select 字段1,字段2,...字段n from 表名;
例子:
select name,score+20 from student; //在原基础上所有成绩+20分显示
select name as 姓名,score+20 as 成绩 from student; //字段名 [as] 别名
select distinct sex from student; //显示性别 过滤掉重复值

5.条件查询
select 字段1,字段2,...字段n
from 表名
where 条件表达式;

通配符:% 匹配任意长度的字符串,_ 匹配单个字符
逻辑运算符:and、or、not,and的优先级高于or
where [not] 表达式1 and|or 表达式2
例子:
select * from student where sid='G19002';
select * from student where score>85;
select * from student where score between 85 and 95;//查询成绩85-95区间内(含)的数据
select * from student where score not between 85 and 95;//查询成绩不在85-95区间内的数据
select * from student where name in('小明','小白','小红');//查询这三人的数据
select * from student where name not in('小明','小白','小红');//查询除了这三人以外的其他人的数据
select * from student where name like '李%';//查询name李姓开头的数据
select * from student where name like '__杰%';//查询name第3个字是“杰”的数据
select * from student where sex is null;//查询性别为空值的数据
select * from student where sex is not null;//查询性别不为空值的数据
select * from student where score>90 and sex='女';//查询score为90以上的女生数据
select * from student where score>90 or gid=1004;//查询score为90以上或gid为1004的数据
select * from student where sex='女' or sex='男' and score=100;//查询性别为女生或者成绩100的男生的数据

6.数据排序
order by 字段名1 [asc|desc],字段名2[acs|desc] //asc升序 desc降序,不写asc和desc则默认升序
select * from student order by score asc;
select * from student order by score desc,gid asc;//查询结果按成绩降序、gid升序进行排序
select * from student where gid>1002 order by score;//查询gid大于1002的数据并按score升序排序
备注:order by子句要写在where子句的后面!!

7.数据分组统计
select 字段1,字段2...
from 表名
[where 条件表达式]
group by 列名列表
[having 条件表达式];

聚合函数:实现在数据表在指定列上的值,或对一组指定的劣质进行特定的运算,并返回单个数值
sum 求和
avg 求平均值
max 求最大值
min 求最小值
count 求项数
例子:
select sum(score) from student;//统计student表中score的总分
select count(*)from student;//统计student表中有多少条记录
select count(name) from student;
select count(*),sex from student group by sex;//根据性别划分,统计学生人数
select sum(score),gid from student group by gid;//统计每个班级gid的总分
select sum(score),gid from student where gid in(1001,1002,1003) group by gid;//统计1001,1002,1003这三个班级的总分
select sum(score),gid from student where gid in(1001,1002,1003) group by gid having sum(score)>175;//筛选出1001,1002,1003三个班级中总分大于175的班级
where子句和having子句的区别:where子句中不能使用集合函数,having子句中允许使用集合函数;where子句作用在group by之前,having子句作用在group by之后

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值