數據庫搭建

數據庫創建,這裏介紹了主鍵約束,外鍵約束,唯一性約束,插入,刪除,修改,查找等方法

首先這裏創建一個數據庫

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;//查看計算機最低分
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章