#11.7
SELECT *FROM apps;
INSERT INTO websites (name,country) SELECT app_name, country FROM apps;#复制 "apps" 中的数据插入到 "Websites" 中
SELECT * FROM websites;
INSERT INTO websites(name,country) SELECT app_name,country FROM apps WHERE id = 1; #只复 id=1 的数据到 "Websites" 中
SELECT *FROM websites;
CREATE DATABASE my_db; #创建一个名为 "my_db" 的数据库
#创建一个名为 "Persons" 的表,包含五列:PersonID、LastName、FirstName、Address 和 City。
CREATE TABLE Persons
(
PersonID int NOT NULL,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
SELECT * from Persons;
INSERT INTO Persons (PersonID,LastName,FirstName,Address,City)
VALUES('1','Hansen','Ola','Timpteivn10','Sandnes'),('2','Svendson','Tove','Borgvn23','Sandnes'),('3','Pettersen','Kair','Storgt20','Stavanger');
SELECT * FROM Persons;
CREATE TABLE ORDERS
(
O_id int NOT NULL,
OrderNo int NOT NULL,
PersonID int NOT NULL
);
INSERT INTO ORDERS
VALUES ('1','77895','3'),('2','44678','3'),('3','22456','2'),('4','24562','1');
SELECT * FROM ORDERS;
ALTER TABLE Persons
ADD primary key(PersonID);
ALTER TABLE ORDERS
ADD primary key(O_id);
#当 "Orders" 表已被创建时,如需在 "P_Id" 列创建 FOREIGN KEY 约束
ALTER TABLE ORDERS
ADD foreign key (PersonID) references Persons(PersonID);
11.7 SQL代码
最新推荐文章于 2024-11-05 14:55:45 发布