MYSQL知识点(一)

来源:互联网 发布:linux sort k 编辑:程序博客网 时间:2024/06/04 18:46
查询操作
#查询所有记录的所有字段值
#没有where条件表示所有记录,*表示所有字段
#select * from stu;

#查询所有记录的name与sex
#select stuName, stuSex from stu;

#查询所有记录的除stuNo外的字段值,字段的顺序就是显示的顺序
#select stuName, stuSex, stuBirth, stuTel, stuAddr from stu;

#查询stuNo为1的记录中所有字段的值
#select * from stu where stuNo=1;

#查询stuNo为1或者2的记录中所有字段的值
#select * from stu where stuNo=1 or stuNo=2;

#查询stuNo为1,3,5的记录中所有字段的值
#select * from stu where stuNo in(1,3,5);

#查询stuNo为1,3,5或者stuName为小芳的所有记录中所有字段的值
#select * from stu where stuNo in(1,3,5) or stuName='小芳';

#查询stuName为'翠花或者小红的记录中所有字段的值
select * from stu where stuName in('翠花', '小红');

not in('')   反向查询

select stuName, stuSex from stu where stuNo>3;

更改操作
#格式:update stu set 字段=值 where

#更改stuNo为1的记录中的stuBirth
update stu set stuBirth='2017-02-02' where stuNo=1;

#更改stuNo为2的记录中4个字段的值
update stu set stuName='小芳', stuBirth='2017-03-03', stuSex='女', stuAddr='村里' 
where stuNo=2;

#更改stuNo为1,5,6字段的名字
update stu set stuName='翠花' where stuNo in(1,5,6);

#更改stuTel为112的电话
update stu set stuTel='10000' where stuTel='112';

#更改name为小红并且tel为114的记录的值
update stu set stuSex='男', stuAddr='黑龙江' where stuName='小红' and stuTel='114';

#更改tel为115或者116的记录的值
update stu set stuBirth='11-11-11', stuAddr='甘肃' where stuTel='115' or stuTel='116';

删除操作
#格式:delete from stu where ;
#删除数据时,如果有满足条件的记录则删除,如果没有满足条件的记录则不删除

#删除stuNo为4的记录
delete from stu where stuNo=4;

#删除stuName为小明并且stuTel10010的记录(两个条件同时满足)
delete from stu where stuName='小明' and stuTel='10010';

#删除stuTel位120或者160的记录(只要其中一个条件满足即可)
delete from stu where stuTel='120' or stuTel='160';

#删除stuNo小于3的所有记录,但不包括3
delete from stu where stuNo<3;

#删除stuNo大于等于9的所有记录,包括9
#delete from stu where stuNo>=9;

#删除stuNo小于6或者stuNo>12的所有记录
delete from stu where stuNo<6 or stuNo>12;

#删除8 <= stuNo < 10之间的所有记录[8,10)
delete from stu where stuNo >=8 and stuNo<10;

#删除stuNo为6,7,8,9的记录
delete from stu where stuNo in(6,7,8,9);

#删除stuNo不为8,9,10的所有记录
delete from stu where stuNo not in(8,9,10);

插入操作
#格式:insert into stu(字段名称) values(字段的值);
#这种格式可以选择字段来添加数据
根据指定字段名称依次添加数据:非空和唯一约束的字段必须添加
insert into stu(stuNo, stuName, stuBirth, stuSex, stuAddr, stuTel) 
values(2, '小明', '2017-8-3', '男', '成都', '10010');

insert into stu(stuName, stuBirth, stuSex, stuAddr, stuTel) 
values('小明', '2017-8-3', '男', '成都', '10011');

#格式:insert into stu values()
#这个格式表示需要添加所有字段的值,包括自增长的id,添加数值的顺序与字段在表中的顺序一致
#insert into stu values(9, 'Rose', '1994-12-12', '女', '大西洋', '119');

建表时的关键字
格式:字段名称  字段类型  字段约束
primary key:主键约束
auto_increment:自增长,一般用于主键,并且主键的类型为int
not null:非空约束,值不能为空
check:检查约束,在MySQL中不生效
default :缺省约束,默认值
unique:唯一约束,值不能重复


create table stu(
stuNo integer primary key auto_increment, 
stuName char(12) not null,
stuBirth char(10), 
stuSex char check(stuSex='男', stuSex='女'), 
stuAddr char(10) default '成都',
stuTel char(11) unique
);

#删除表
drop table stu;

#创建数据库
#create database mytest;

#删除数据库
drop database mytest;

#删除表中所有的记录后将编号重置为1
truncate table stu;

#将编号重置并组成新的排序,值从表中最后一条记录的id开始增长
#alter table stu auto_increment=1;

多对多关系

创建主表teacher
create table mteacher(
id int primary key auto_increment, 
name char(10)
);

创建主表stu
create table mstu(
id int primary key auto_increment, 
name char(10),
sex char,
score int
);

创建一个中间表,有2个外键,分别关联teacher与stu表
create table middle(
id int primary key auto_increment, 
f_mteacher_id int not null, 
f_mstu_id int not null
);

为第一个外键添加关联关系
on delete no action表示不能删除主表的记录
alter table middle 
add constraint foreign key(f_mteacher_id) 
references mteacher(id) 
on delete no action;

#为第二个外键添加关联关系
alter table middle 
add constraint foreign key(f_mstu_id) 
references mstu(id) 
on delete no action;

一对多关系

create table tclass(
className char(10) primary key, 
headTeacher char(10) not null
);

create table tstu(
num int primary key auto_increment, 
name char(10), 
age int,
f_tclass_classname char(10)
);

#添加外键约束
#on delete set null表示主表记录删除后,从表中对应的字段设置为null,因此该外键不能添加非空约束
alter table tstu 
add constraint foreign key(f_tclass_classname) 
references tclass(className) 
on delete set null;


一对一关系

创建主表:onecountry
create table onecountry(
id int primary key auto_increment, 
name char(10) not null unique, 
language char(10) 
);

创建从表:onepresident,外键必须加唯一unique约束
create table onepresident(
id int primary key auto_increment, 
name char(10) not null, 
sex char, 
f_onecountry_id int unique
);

#添加外键约束
foreign key表示外键约束,()中的值表示哪一个外键
references表示关联哪一个表中的主键
on delete cascade表示级联删除,主表记录被删除后,从表中与之相关的记录一起被删除

alter table onepresident 
add constraint foreign key(f_onecountry_id) 
references onecountry(id) 
on delete cascade;
原创粉丝点击