挿入データ
インサート
完全な行を挿入
INSERT INTO Customers
VALUES('1000000006',
'Toy Land',
'123 Any Street',
'New York',
'NY',
'11111',
'USA',
NULL,
NULL);
一つの属性の一つ
INSERT INTO Customers(cust_id,
cust_name,
cust_city,
cust_address,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email)
VALUES('1000000006',
'Toy Land',
'123 Any Street',
'New York',
'NY',
'11111',
'USA',
NULL,
NULL);
ラインの一部を挿入
INSERT INTO Customers(cust_id,
cust_name,
cust_city,
cust_address,
cust_state,
cust_zip,
cust_country)
VALUES('1000000006',
'Toy Land',
'123 Any Street',
'New York',
'NY',
'11111',
'USA');
属性にはデフォルト値が与えられ、またはされ、一部がNULLであることができます
取得したデータの挿入
INSERT SELECT
声明
INSERT INTO Customers(.....)
SELECT ..... FROM CustNew;
他に一つのテーブルからコピー
CREATE TABLE CustCopy AS
SELECT * FROM Customers;
更新、削除データ
更新
UPDATE Customers
SET cust_contact = 'Sam Roberts',
cust_email = '[email protected]'
WHERE cust_id = '1000000006';
DELETE
DELETE FROM Customers
WHERE cust_id = '1000000006';
場合UPDATE
とDELETE
することなくWHERE
変更することである単語/各列を削除
テーブルを作成し、操作します
CREATE
CREATE TABLE Orders
(
order_num INTEGER NOT NULL,
order_date DATETIME NOT NULL,
cust_id CHAR(10) NOT NULL
);
デフォルト値を指定します。
続きますDEFAULT
CREATE TABLE OrderItems
(
order_num INTEGER NOT NULL,
order_item INTEGER NOT NULL,
prod_id CHAR(10) NOT NULL,
quantity INTEGER NOT NULL DEFAULT 1,
item_price DECIMAL(8, 2) NOT NULL
);
ALTER
列を増やします
ALTER TABLE Vendors
ADD vend_phone CHAR(20);
列を削除
ALTER TABLE Vendors
DROP COLUMN vend_phone;
削除テーブル
DROP TABLE CustCopy;
ビューの使用
VIEW
ビューを作成します。
CREATE VIEW ProductCustomers AS
SELECT cust_name, cust_contact, prod_id
FROM Customers, Orders, OrderItems
WHERE Customers.cust_id = Orders.cust_id
AND OrderItems.order_num = Orders.order_num;
ストアドプロシージャ
すべてのカプセル化プロセス簡素化操作
業務
カーソル
高度なSQLの機能
制約
主キー
固有の識別
CREATE TABLE Orders
(
order_num INTEGER NOT NULL, PRIMARY KEY,
order_date DATETIME NOT NULL,
cust_id CHAR(10) NOT NULL
);
またはテーブルを作成し、追加します
ALTER TABLE Orders
ADD CONSTRAINT PRIMARY KEY (order_num);
外部キー
CREATE TABLE Orders
(
order_num INTEGER NOT NULL, PRIMARY KEY,
order_date DATETIME NOT NULL,
cust_id CHAR(10) NOT NULL , REFERENCES Customers(cust_id)
);
若しくは
ALTER TABLE Orders
ADD CONSTRAINT
FOREIGN KEY (cust_id) REFERENCES Customers (cust_id);
チェック制約
CREATE TABLE OrderItems
(
order_num INTEGER NOT NULL,
order_item INTEGER NOT NULL,
prod_id CHAR(10) NOT NULL,
quantity INTEGER NOT NULL CHECK (quantity > 0),
item_price DECIMAL(8, 2) NOT NULL
);
若しくは
ADD CONSTRAINT CHECK (gender LIKE '[MF]');