Drop Table IF Exists 專案供應零件; Drop Table IF Exists 供應商; Drop Table IF Exists 零件; Drop Table IF Exists 專案; Drop view IF Exists john_view; CREATE TABLE 供應商( 供應商代號 VARCHAR(10) , 供應商名稱 VARCHAR(10), 城市 VARCHAR(10), PRIMARY KEY(供應商代號) ); CREATE TABLE 零件( 零件代號 VARCHAR(10) , 零件名稱 VARCHAR(10) , 顏色 VARCHAR(2), 重量 DEC(5,2), PRIMARY KEY(零件代號) ); CREATE TABLE 專案( 專案代號 VARCHAR(10) , 專案名稱 VARCHAR(10), 城市 VARCHAR(10), PRIMARY KEY(專案代號) ); CREATE TABLE 專案供應零件( 供應商代號 VARCHAR(10) , 零件代號 VARCHAR(10) , 專案代號 VARCHAR(10) , 數量 INT, PRIMARY KEY(供應商代號,零件代號,專案代號), FOREIGN KEY(供應商代號) REFERENCES 供應商(供應商代號), -- ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY(零件代號) REFERENCES 零件(零件代號), -- ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY(專案代號) REFERENCES 專案(專案代號) -- ON DELETE CASCADE ON UPDATE CASCADE ); insert into 供應商 values ('S1', '大勝', '台南'); insert into 供應商 values ('S2', '冠軍', '高雄'); insert into 供應商 values ('S3', '無敵', '台中'); insert into 供應商 values ('S4', '一級棒','高雄'); insert into 零件 values ('P1', '螺絲釘','黑',14.0); insert into 零件 values ('P2', '螺帽','黑',16.0); insert into 零件 values ('P3', '齒輪','綠',27.0); insert into 零件 values ('P4', '板手','藍',63.0); insert into 零件 values ('P5', '撬子','棕',80.0); insert into 專案 values ('J1', '火星','台中'); insert into 專案 values ('J2', '土星','高雄'); insert into 專案 values ('J3', '太陽','台中'); insert into 專案供應零件 values ('S1', 'P1','J2',300); insert into 專案供應零件 values ('S1', 'P1','J3',100); insert into 專案供應零件 values ('S1', 'P4','J1',500); insert into 專案供應零件 values ('S2', 'P2','J1',400); insert into 專案供應零件 values ('S2', 'P3','J3',600); insert into 專案供應零件 values ('S3', 'P1','J2',300); insert into 專案供應零件 values ('S4', 'P3','J1',200); insert into 專案供應零件 values ('S4', 'P4','J1',700); insert into 專案供應零件 values ('S4', 'P5','J2',100); -- ////////////////////////////////////以上是題目table insert into 專案供應零件 values('S1', 'P2', 'J1', 500); insert into 供應商(供應商代號,供應商名稱) values('S5', '第一'); update 零件 set 顏色='黑' where 顏色='灰'; select * from 專案供應零件; select * from 供應商; select * from 零件; select * from 專案; DROP table 專案 CASCADE ; select * from 專案供應零件; select * from 供應商; select * from 零件; IF (OBJECT_ID(專案) IS NOT NULL ) BEGIN PRINT 專案 END ELSE BEGIN PRINT 專案 END -- IF (EXISTS (SELECT * FROM 專案)); -- update 零件 set 顏色='X色' where 顏色='黑'; -- insert into 專案供應零件(供應商代號,零件代號,專案代號,數量) values ('S1', 'P2', 'J1', 500);
Write, Run & Share PostgreSQL queries online using OneCompiler's PostgreSQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for PostgreSQL. Getting started with the OneCompiler's PostgreSQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose database as 'PostgreSQL' and start writing queries to learn and test online without worrying about tedious process of installation.
PostgreSQL is a open source relational database system and is also knows as Postgres.
CREATE command is used to create a table, schema or an index.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
....);
ALTER command is used to add, modify or delete columns or constraints from the database table.
ALTER TABLE Table_name ADD column_name datatype;
TRUNCATE command is used to delete the data present in the table but this will not delete the table.
TRUNCATE table table_name;
DROP command is used to delete the table along with its data.
DROP TABLE table_name;
RENAME command is used to rename the table name.
ALTER TABLE table_name1 RENAME to new_table_name1;
INSERT Statement is used to insert new records into the database table.
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Select statement is used to select data from database tables.
SELECT column1, column2, ...
FROM table_name;
UPDATE statement is used to modify the existing values of records present in the database table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DELETE statement is used to delete the existing records present in the database table.
DELETE FROM table_name where condition;