简体   繁体   English

SQL不更新表也将旧表添加到新表

[英]SQL not updating table also adding old table to new one

it's my first time using SQL, I've followed the syntax but it doesnt work.. 这是我第一次使用SQL,但我遵循了语法,但是没有用。

use cw312;
CREATE TABLE pet (
name VARCHAR(20),
owner VARCHAR(20),
species VARCHAR(20),
sex CHAR(1),
birth DATE,
death DATE
);
Insert into pet values ('Fluffy','Harold','cat','f','1993-03-04',NULL),
('Claws','Gwen','cat','m','1994-03-17',NULL),
('Buffy','Harold','dog','f','1989-05-13',NULL),('Fang','Benny','dog','m','1990-08-      27',NULL),
('Bowser','Diane','dog','m','1998-08-31','1999-07-11'),
('Chirpy','Gwen','bird','f','1998-09-11',NULL),
('Whistler','Gwen','bird','f','1997-12-09',NULL),
('Slim','Benny','snake','m','1996-04-29',NULL),
('Puffball','Diane','hamster','f','1999-03-30',NULL),
('Daddy', 'Jennifer', 'dog', 'm', '1970-03-30', NULL),
('Major', 'Margaret','cat', 'm', '1993-03-30','2000-04-30');

UPDATE pet SET species = 'dog'
WHERE owner = 'Margaret';




DESCRIBE pet;
SELECT 
  *
FROM
pet;

Why doesnt the update pet set species = dog where owner = margaret update the species to dog? 为什么更新宠物集的物种=狗,而所有者=玛格丽特却不更新狗的品种?

Also every time i execute this, sql like remakes the Database and adds it to my previous version.. any idea why? 同样,每次执行此命令时,sql都会重新制作数据库并将其添加到我的先前版本中。

Once table is created and data is inserted, it stay there unless you remove it explicitly. 创建表并插入数据后,除非您明确删除它,否则它将保留在那里。

Do a commit; 做一个承诺; after the update (and the insert) and then select * from pet;. 更新(和插入)后,然后从pet;中选择*。

Do not rerun the create table or insert statement... 不要重新运行create table或insert语句...

To avoid repeatedly creating the table it helps to do 为了避免重复创建表,这样做会有所帮助

CREATE TABLE IF NOT EXISTS pet
..

Your initialisation code should be in a separate file really, as every time you run that script the CREATE statement will fail but the INSERT will succeed again and again. 您的初始化代码实际上应该在一个单独的文件中,因为每次您运行该脚本时,CREATE语句都会失败,但是INSERT将一次又一次地成功。 Also your table could do with a unique identifier to ensure that 2 pets named 'Fluffy' aren't confused. 另外,您的桌子可以使用唯一的标识符来确保不会混淆2个名为“蓬松”的宠物。

Also if you really want to clean out the table, add a TRUNCATE TABLE command just after the create. 另外,如果您确实要清除表,请在创建后添加TRUNCATE TABLE命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM