我的問題是它希望我添加一個約束,這樣公司就不能再購買 2018 年之前制造的汽車。問題是我的資料庫已經有關于 2018 年之前制造的汽車的資訊。我無法洗掉該表,因為它會弄亂我的其他一些查詢。我可以得到一些建議嗎?
我的車輛表代碼如下
create table Vehicle(
LicensePlate varchar(48) primary key, /*Primary key for table Vehicle for License plate, variable can hold up to 48 characters*/
Make varchar (48),
CarYear int
);
insert into Vehicle (LicensePlate,Make,CarYear) values ('1234 AA','Toyota',1970);
insert into Vehicle (LicensePlate,Make,CarYear) values ('1237 AB','Mazda',1995);
insert into Vehicle (LicensePlate,Make,CarYear) values ('1892 BG','Toyota',2000);
insert into Vehicle (LicensePlate,Make,CarYear) values ('1876 FA','Nissan',1999);
insert into Vehicle (LicensePlate,Make,CarYear) values ('3021 AA','Mazda',1950);
insert into Vehicle (LicensePlate,Make,CarYear) values ('2134 FF','Toyota',1992);
-- Company has decided not to purchase any more car that were made before 2018. Add appropriate constraints to the Vehicle table to enforce this requirement.
alter table Vehicle add constraint checker_1 check (CarYear > 2018);
uj5u.com熱心網友回復:
如果您的操作可行,請嘗試此查詢:
SELECT LicensePlate, Make, CarYear,
CASE WHEN CarYear > 2018 THEN 1 ELSE 0 END AS car_year_chk
FROM Vehicle;
根據您當前的樣本資料,您將得到以下結果:
| 車牌 | 制作 | 車年 | car_year_chk |
|---|---|---|---|
| 第1234章 | 豐田 | 1970 | 0 |
| 第1237章 | 馬自達 | 1995 | 0 |
| 1876 年 | 日產 | 1999 | 0 |
| 1892 年 | 豐田 | 2000 | 0 |
| 2134 法郎 | 豐田 | 1992 | 0 |
| 3021 AA | 馬自達 | 1950 | 0 |
演示小提琴
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/525238.html
上一篇:MySql洗掉具有重復列內容的行
