主题:mysql 分享!!
大家好,下面是我积累的知识与大家分享一下,嘻嘻[url=http://www.bdqn1.cn]编程[/url]
1.创建学生基本信息表student
use test;
create table student
(name varchar(50) ,
sex varchar(10),
address varchar(100),
score float(20)
);
2.创建非空约束,不允许name列为空
alter table student modify name varchar(50) not null;
2.向student表添加一条记录
insert into student
values('张明','男','北京',90),('李山','女','上海',80);
3.修改student表对name列创建唯一性约束
alter table student
add constraint student_name_uk unique(name);
4.在student表中增加一列stu_id并放在表的第一列
alter table student add stu_id int first ;
5.将张明和李山的id修改为1和2
update student set stu_id=1 where name='张明';
update student set stu_id=2 where name='李山';
6.将stu_id设置为student表的主键
alter table student add constraint primary key(stu_id);
7.创建学生成绩表stuscore
create table stuscore
(stu_id int not null,
score float(10));
8.在stuscore表中添加外键,主键在student表中,外键是stuscore表的stu_id主键是student表的stu_id,并实现级联更新
alter table stuscore
add constraint s_s_fk foreign key (stu_id)
references student(stu_id)
on delete cascade;
9.向stuscore表中添加一条记录,其中stu_id在student表中不存在
insert into stuscore
values(19,80);
1)这是违反外键约束的,不能创建,因为student表中只有id是1和2的两个学生。而下面是可以的
insert into stuscore
values(2,90);
2)删除主表中id为2的同学,观察子表(stuscore)对应的记录是否也删除了
delete from student
where stu_id=2;
3)用select * from student;
select* from stuscore;分别看看两表对应记录是否删除了
10.将stu_id修改为自增列,并向此表插入一条记录其中id列不输入值
alter table student modify stu_id int auto_increment;
insert into student(name,sex,address,score) values('zang','dd','dd',100);
11.为stuscore表的score设置默认值0。并向此表插入一个id为10的同学,成绩默认值为0分
alter table stuscore alter score set default 0;
insert into stuscore values(10,default);
12.设置score列的值小于100分
alter table stuscore add constraint chk check(score<100);
13.修改stuscore表的默认存储引擎为innodb
alter table stuscore
engine=innodb;
14.更改表的字符集
alter table stuscore
character set =gbk;
create table t4
(id int primary key auto_increment,
name char(30) not null unique ,
score float default 0) engine=innodb charset=gbk;
[em9]
1.创建学生基本信息表student
use test;
create table student
(name varchar(50) ,
sex varchar(10),
address varchar(100),
score float(20)
);
2.创建非空约束,不允许name列为空
alter table student modify name varchar(50) not null;
2.向student表添加一条记录
insert into student
values('张明','男','北京',90),('李山','女','上海',80);
3.修改student表对name列创建唯一性约束
alter table student
add constraint student_name_uk unique(name);
4.在student表中增加一列stu_id并放在表的第一列
alter table student add stu_id int first ;
5.将张明和李山的id修改为1和2
update student set stu_id=1 where name='张明';
update student set stu_id=2 where name='李山';
6.将stu_id设置为student表的主键
alter table student add constraint primary key(stu_id);
7.创建学生成绩表stuscore
create table stuscore
(stu_id int not null,
score float(10));
8.在stuscore表中添加外键,主键在student表中,外键是stuscore表的stu_id主键是student表的stu_id,并实现级联更新
alter table stuscore
add constraint s_s_fk foreign key (stu_id)
references student(stu_id)
on delete cascade;
9.向stuscore表中添加一条记录,其中stu_id在student表中不存在
insert into stuscore
values(19,80);
1)这是违反外键约束的,不能创建,因为student表中只有id是1和2的两个学生。而下面是可以的
insert into stuscore
values(2,90);
2)删除主表中id为2的同学,观察子表(stuscore)对应的记录是否也删除了
delete from student
where stu_id=2;
3)用select * from student;
select* from stuscore;分别看看两表对应记录是否删除了
10.将stu_id修改为自增列,并向此表插入一条记录其中id列不输入值
alter table student modify stu_id int auto_increment;
insert into student(name,sex,address,score) values('zang','dd','dd',100);
11.为stuscore表的score设置默认值0。并向此表插入一个id为10的同学,成绩默认值为0分
alter table stuscore alter score set default 0;
insert into stuscore values(10,default);
12.设置score列的值小于100分
alter table stuscore add constraint chk check(score<100);
13.修改stuscore表的默认存储引擎为innodb
alter table stuscore
engine=innodb;
14.更改表的字符集
alter table stuscore
character set =gbk;
create table t4
(id int primary key auto_increment,
name char(30) not null unique ,
score float default 0) engine=innodb charset=gbk;
[em9]