1. 程式人生 > >SQL語句,資料庫的建立、定義修改刪除基本表

SQL語句,資料庫的建立、定義修改刪除基本表

/*CREATE DATABASE JWGL AUTHORIZATION 張銘     資料庫的建立*/
CREATE TABLE S   /*建立基本表s*/
(SNO CHAR(9) PRIMARY KEY,
SNAME CHAR(20) UNIQUE,
SEX CHAR(1),
AGE SMALLINT,
SDEPT CHAR(20)
);
INSERT INTO S VALUES(200915121,'李小勇','M',20,'CS');
INSERT INTO S VALUES(200915122,'劉晨','F',19,'CS');
INSERT INTO S VALUES(200915123,'王洪敏','F',18,'MA');
INSERT INTO S VALUES(200915124,'張力','M',19,'IS');
drop table S;
/*建立基本表c*/
CREATE TABLE C   
(CNO CHAR(4) PRIMARY KEY,
CNAME CHAR(40),
CPNO CHAR(4),
CCREDIT SMALLINT
);
INSERT INTO C VALUES('1','資料庫','5',4);
INSERT INTO C VALUES('2','數學','',2);
INSERT INTO C VALUES('3','資訊系統','1',4);
INSERT INTO C VALUES('4','作業系統','6',3);
INSERT INTO C VALUES('5','資料結構','7',4);
INSERT INTO C VALUES('6','資料處理','',2);
INSERT INTO C VALUES('7','C語言','6',4);
drop table C;
/*建立基本表sc*/
CREATE TABLE SC   
(SNO CHAR(9),
CNO CHAR(4),
GRADE SMALLINT,
PRIMARY KEY(SNO,CNO),
FOREIGN KEY(SNO) REFERENCES S(SNO),
FOREIGN KEY(CNO) REFERENCES C(CNO) 
);
INSERT INTO SC VALUES('200915121','1',92);
INSERT INTO SC VALUES('200915121','2',85);
INSERT INTO SC VALUES('200915121','3',88);
INSERT INTO SC VALUES('200915122','2',90);
INSERT INTO SC VALUES('200915122','3',80);
drop table SC;
--向s表增加入學時間這個變數S_ENTRANCE
ALTER TABLE S ADD S_ENTRANCE DATE;  
/*不成功,將s表中的年齡改為數值型,修改型別或者是大改小的話,需要先清除掉被修改的欄位裡的值*/
--ALTER TABLE S ALTER COLUME AGE INT; 
ALTER TABLE C ADD UNIQUE(CNAME); /*增加課程名稱必須取唯一值的約束條件*/