数据库实验一:创建数据库和表

1.1 实验目的

  1. 掌握MySQL下创建、修改、删除数据库和表的方法;

  2. 掌握查看数据库及其构成、数据库文件、数据库引擎、数据库对象、字符集、校对规则等应用;

  3. 掌握数据库对象命名规则、数据类型的选择、索引使用规则等。
    1.2 实验内容

  4. 创建数据库stu后,查看数据库,查找数据库文件所在位置,修改数据库stu字符集为gbk,校对规则为gbk_chinese_ci;

  5. 使用数据库stu,创建如课本P79图3.2所示的三个表;

  6. 复制表Student的表结构给新表sbak,修改Sage为Syear,数据类型为datatime;

  7. 查看表及sbak的表结构;

  8. 在表sbak上的Sdept列创建索引,并查看索引详情。

1.2实验过程

1.打开命令窗口输入mysql -h localhost -u root -p
创建数据库: CREATE DATABASE stu;
查看数据库文件位置:show global variables like “%datadir%”;
修改数据库字符集及校队规则:alter database stu character set gbk collate gbk_chinese_ci;
2.创建P79的三个表
CREATE TABLE stu.stu_student (
sno CHAR(9) NOT NULL,
sname CHAR(20) NULL,
ssex CHAR(2) NULL,
sage SMALLINT NULL,
sdept CHAR(20) NULL,
PRIMARY KEY (sno),
UNIQUE INDEX sname_UNIQUE (sname ASC) VISIBLE);

CREATE TABLE stu.stu_course (
Cno CHAR(4) NOT NULL,
Cname CHAR(40) NOT NULL,
Cpno CHAR(4) NULL,
Ccredit SMALLINT NULL,
PRIMARY KEY (Cno));

CREATE TABLE stu.stu_sc (
sno CHAR(9) NOT NULL,
Cno CHAR(4) NOT NULL,
Grade SMALLINT NULL,
PRIMARY KEY (sno,Cno),

INSERT INTO stu.stu_student (sno, sname, ssex, sage, sdept) VALUES (‘201215121’, ‘李勇’, ‘男’, ‘20’, ‘CS’);
INSERT INTO stu.stu_student (sno, sname, ssex, sage, sdept) VALUES (‘201215122’, ‘刘晨’, ‘女’, ‘19’, ‘CS’);
INSERT INTO stu.stu_student (sno, sname, ssex, sage, sdept) VALUES (‘201215123’, ‘王敏’, ‘女’, ‘18’, ‘MA’);
INSERT INTO stu.stu_student (sno, sname, ssex, sage, sdept) VALUES (‘201215125’, ‘张立’, ‘男’, ‘19’, ‘IS’);

INSERT INTO stu.stu_course (Cno, Cname, Cpno, Ccredit) VALUES (‘1’, ‘数据库’, ‘5’, ‘4’);
INSERT INTO stu.stu_course (Cno, Cname, Ccredit) VALUES (‘2’, ‘数学’, ‘2’);
INSERT INTO stu.stu_course (Cno, Cname, Cpno, Ccredit) VALUES (‘3’, ‘信息系统’, ‘1’, ‘4’);
INSERT INTO stu.stu_course (Cno, Cname, Cpno, Ccredit) VALUES (‘4’, ‘操作系统’, ‘6’, ‘3’);
INSERT INTO stu.stu_course (Cno, Cname, Cpno, Ccredit) VALUES (‘5’, ‘数据结构’, ‘7’, ‘4’);
INSERT INTO stu.stu_course (Cno, Cname, Ccredit) VALUES (‘6’, ‘数据处理’, ‘2’);
INSERT INTO stu.stu_course (Cno, Cname, Cpno, Ccredit) VALUES (‘7’, ‘PASCAL语言’, ‘6’, ‘4’);

INSERT INTO stu.stu_sc (sno, Cno, Grade) VALUES (‘201215121’, ‘1’, ‘92’);
INSERT INTO stu.stu_sc (sno, Cno, Grade) VALUES (‘201215121’, ‘2’, ‘85’);
INSERT INTO stu.stu_sc (sno, Cno, Grade) VALUES (‘201215121’, ‘3’, ‘88’);
INSERT INTO stu.stu_sc (sno, Cno, Grade) VALUES (‘201215122’, ‘2’, ‘90’);
INSERT INTO stu.stu_sc (sno, Cno, Grade) VALUES (‘201215122’, ‘3’, ‘80’);
3.
复制表student的表结构给sbak: create table stu.sbak like stu.stu_student;
修改Sage为Syear:ALTER TABLE stu.sbak CHANGE Sage Syear SMALLINT;
修改数据类型:ALTER TABLE stu.sbak MODIFY COLUMN Syear datetime;
4.
查看表及表结构:desc stu.sbak;
5.
创建索引并查看索引详情:
ALTER TABLE stu.sbak ADD INDEX sbak_1 (Sdept);
show index from stu.sbak;

你可能感兴趣的:(数据库)