数据库创建,这里介绍了主键约束,外键约束,唯一性约束,插入,删除,修改,查找等方法
首先这里创建一个数据库
create database schoolmessage;//schoolmessage为你创建的数据库的名称
选择你要创建表的数据库
use schoolmessage;
创你在当前数据库下的第一个表
create table student(//student 是表名
sno char(11) unique ,//使用了唯一性约束
sname varchar(20),
sclass varchar(20) not null,//使用了非空约束
primary key(sno,sname)//多字段联合主键,
) CHARSET =utf8;//utf-8是国际编码,包括汉字
删除数据表
drop table student;
删除数数据库
drop database schoolmessage;
创你在当前数据库下的第二个表
create table stu_course(//stu_course是表名
scno char(11) primary key,//单子段主键
scchinese int(3) not null,
scmath int(3) not null,
sccomputer int(3) not null,
unique(scno),//对scno添加唯一性约束
foreign key (scno) REFERENCES student(sno)//设置外键 scno是从表的某一字段名student是主表名 sno是主表中的字段名
) CHARSET=utf8;
创你在当前数据库下的第三个表
Create table message(
Mgno char(11) unique,//唯一性约束
mgname varchar(20),
mgtel char(11) not null,
mgsex varchar(5) not null,
mgbirthday date not null,
mghome varchar(20) unique,
primary key(mgno,mgname),
foreign key (mgno) references student(sno)
) CHARSET=utf8;
创你在当前数据库下的第四个表
create table joinorg(
jnno char(11) primary key,
jnstuun char(11) not null,
jncorporation char(11) not null,
jnlab char(11) not null,
unique(jnno),
foreign key (jnno) REFERENCES student(sno)
) CHARSET=utf8;
在第一个表中添加数据
insert into Student values(001,'皮皮虾','计算机17-1');
insert into Student values(002, '牛老板','计算机16-1');
insert into Student values(003,'社会雯','计算机17-1');
insert into Student values(004,'开心','计算机17-1');
insert into Student values(005,'王长秀','计算机17-1');
insert into Student values(006,'张三','计算机17-1');
插入完成后查看表里的内容
select *from Student;
然后删除最后一列
delete from student where sno=6;
在查看:
select *from Student;
insert into stu_course values(001,90,91,92);
insert into stu_course values(002,89,95,94);
insert into stu_course values(003,89,77,88);
insert into stu_course values(004,76,87,90);
insert into stu_course values(005,98,97,96);
在第三个表中添加数据
insert into message values(001, '皮皮虾','11111111111','女','1998-1-1','8号楼111');
insert into message values(002, '牛老板','22222222222','男','1997-2-3','11号楼666');
insert into message values(003, '社会雯','33333333333','女','1998-4-5','8号楼333');
insert into message values(004, '开心','44444444444','女','1998-9-9','8号楼222');
insert into message values(005, '王长秀','55555555555','男','1998-6-6','11号楼111');
在第四个表中添加数据
insert into joinorg values(001,'no','yes','yes');
insert into joinorg values(002,'no','yes','yes');
insert into joinorg values(003,'no','no','yes');
insert into joinorg values(004,'no','yes','yes');
insert into joinorg values(005,'no','yes','yes');
修改表结构,增加列:
在student表中添加了toword列
alter table student add toword varchar(15) default "前端";//default默认约束,默认值为 前端,
修改表中的内容:
update student set toword="算法" where sno=1;
update student set toword="后端" where sno=3;
update student set toword="算法" where sno=5;
为表student中的sname添加唯一性约束
alter table student add unique(sname);
查看方法:
select *from student;// 查所有数据
select sno,sName from student;// 查特定列
select * from student where sno=1;// 一个条件查询
select * from student where sno=1 and sclass="计算机17-1";//同时存在的两种内容
select * from stu_course where scchinese>=90 or scchinese<=60; //范围查询
select * from stu_course where scchinese between 80 and 90; //范围查询
select distinct scno,scmath fro m stu_course;//查询不重复结果
select sno,sname,sccomputer from student ,stu_course where student.sno=stu_course.scno;//内连接查询
select distinct mgsex from message;
select count(*) as stu_num from student;//查看学生个数
select sum(scmath) as Mth_sum from stu_course;//查看数学总分
select avg(scchinese) as chinese_avg from stu_course;/查看语文平均分
select max(scchinese) as chinese_max from stu_course;//查看语文最高分
select min(sccomputer) as computer_max from stu_course;//查看计算机最低分