從網(wǎng)上找了一些習題,下面結(jié)合網(wǎng)友的建表SQL語句及workbench的使用柠并,將過程分享一下
第一步:建test數(shù)據(jù)庫弛饭,全過程與前一篇過程一樣,不再進行說明
第二步:建表姆另,共student,course,score,teacher,grade四張表
網(wǎng)友提供的表結(jié)構(gòu)如下所示
CREATE TABLE STUDENT
(SNO VARCHAR(3) NOT NULL,
SNAME VARCHAR(4) NOT NULL,
SSEX VARCHAR(2) NOT NULL,
SBIRTHDAY DATETIME,
CLASS VARCHAR(5))
CREATE TABLE COURSE
(CNO VARCHAR(5) NOT NULL,
CNAME VARCHAR(10) NOT NULL,
TNO VARCHAR(10) NOT NULL)
CREATE TABLE SCORE
(SNO VARCHAR(3) NOT NULL,
CNO VARCHAR(5) NOT NULL,
DEGREE NUMERIC(10, 1) NOT NULL)
NUMERIC即為DECIMAL
CREATE TABLE TEACHER
(TNO VARCHAR(3) NOT NULL,
TNAME VARCHAR(4) NOT NULL, TSEX VARCHAR(2) NOT NULL,
TBIRTHDAY DATETIME NOT NULL, PROF VARCHAR(6),
DEPART VARCHAR(10) NOT NULL)
create table grade(low? number(3,0),upp? number(3),rank? char(1));
NUMBER即為DECIMAL
以student表舉例喇肋,輸入字段名稱,點選數(shù)據(jù)類型即可
自動彈出SQL語句迹辐,后期如熟練可自行寫好SQL語句后執(zhí)行
第三步:錄入數(shù)據(jù)
正常來講蝶防,點擊表名稱后面的表格圖標后,出現(xiàn)的界面是可以直接填寫數(shù)據(jù)的右核,但此次點擊后并沒有出現(xiàn)能夠填寫的表格
經(jīng)查找問題后慧脱,發(fā)現(xiàn)是因為此表沒有設置primary key,在將每個表的唯一字段(如無唯一字段贺喝,可單獨設置一個序號字段)設置了primary key后菱鸥,可正常進行數(shù)據(jù)錄入
數(shù)據(jù)很多,一個一個錄入很麻煩躏鱼,我們可以添加一個SQL表進行錄入
下面是也好的insert語句氮采,將語句復制到軟件中出現(xiàn)了報錯
明明剛才用的是test數(shù)據(jù)庫,為什么報錯顯示world.student不存在呢染苛,經(jīng)過排查鹊漠,發(fā)現(xiàn)在建立test數(shù)據(jù)庫后,我沒有把它設置成默認數(shù)據(jù)庫茶行,于是我要設置一下
發(fā)現(xiàn)仍然報錯躯概,這回我將日期字段加了引號,于是數(shù)據(jù)錄入成功
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,'曾華'
,'男' ,'1977-09-01',95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,'匡明'
,'男' ,'1975-10-02',95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,'王麗'
,'女' ,'1976-01-23',95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,'李軍'
,'男' ,'1976-02-20',95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,'王芳'
,'女' ,'1975-02-10',95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,'陸君'
,'男' ,'1974-06-03',95031);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-105' ,'計算機導論',825)
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-245' ,'操作系統(tǒng)' ,804);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('6-166' ,'數(shù)據(jù)電路' ,856);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('9-888' ,'高等數(shù)學' ,100);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,'3-245',86);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,'3-245',75);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,'3-245',68);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,'3-105',92);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,'3-105',88);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,'3-105',76);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,'3-105',64);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,'3-105',91);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,'3-105',78);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,'6-166',85);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,'6-106',79);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,'6-166',81);
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (804,'李誠','男','1958-12-02','副教授','計算機系');
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (856,'張旭','男','1969-03-12','講師','電子工程系');
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (825,'王萍','女','1972-05-05','助教','計算機系');
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (831,'劉冰','女','1977-08-14','助教','電子工程系');
INSERT INTO grade VALUES(90,100,’A’);
INSERT INTO grade VALUES(80,89,’B’);
INSERT INTO grade VALUES(70,79,’C’);
INSERT INTO grade VALUES(60,69,’D’);
INSERT INTO grade VALUES(0,59,’E’);
第四步:致此畔师,數(shù)據(jù)準備完畢