简体   繁体   中英

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..

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?

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;.

Do not rerun the create table or insert statement...

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. Also your table could do with a unique identifier to ensure that 2 pets named 'Fluffy' aren't confused.

Also if you really want to clean out the table, add a TRUNCATE TABLE command just after the create.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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