2

I start learning SQL and I have some problem.

I have one database with 4 tables.

All 4 tables have column ID.

Something like this :

Table name................Column #1..................Column #2
  Table1....................  ID......................Data1..
  Table2......................ID......................Data2..
  Table3......................ID......................Data3..
  Table4......................ID......................Data4..

I make select by ID.

SELECT Data1, Data2, Data3, Data4 FROM TABLE1
INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.ID
INNER JOIN TABLE3 ON TABLE1.ID = TABLE3.ID
INNER JOIN TABLE4 ON TABLE1.ID = TABLE4.ID
WHERE TABLE.ID=' X '

How can I delete and update columns in this 4 tables by ID? Can somebody help me? I am beginner.

1
  • Is ID in one table is foreign key in another table?
    – srv
    Oct 24, 2009 at 12:58

3 Answers 3

7

In standard SQL you can SELECT from more than one table at a time, but you cannot DELETE or UPDATE more than one table at a time. Therefore you will use commands like:

UPDATE Table1 SET Column = NewData WHERE . . .

and

DELETE FROM Table1 WHERE . . .

The exception is if the tables are related to each other "officially", by using PRIMARY and FOREIGN KEYS, you can have the UPDATE of the linking columns and the DELETE of rows "flow" through the linked tables. To do this you use the CASCADE option of SQL when declaring the keys.

Some vendors offer further non-standard extensions that allow you to UPDATE or DELETE from more than one table, but for those you'll have to specify the database you're using.

2

Update column values

UPDATE table1
SET data1 = myvalue
WHERE table1.id = ' X '

UPDATE table2
SET data2 = myvalue
WHERE table1.id = ' X '

UPDATE table3
SET data3 = myvalue
WHERE table1.id = ' X '

UPDATE table4
SET data4 = myvalue
WHERE table1.id = ' X '

Remove rows matching certain criteria

DELETE FROM table1
WHERE data1 = myvalue

DELETE FROM table2
WHERE data2 = myvalue

DELETE FROM table3
WHERE data3 = myvalue

DELETE FROM table4
WHERE data4 = myvalue

Remove column from table

ALTER TABLE table1
DROP COLUMN data1


ALTER TABLE table2
DROP COLUMN data2


ALTER TABLE table3
DROP COLUMN data3


ALTER TABLE table4
DROP COLUMN data4
3
  • Although the ALTER TABLE / DROP COLUMN syntax does answer the literal question asked, I think the Michael probably intended to ask about deleting rows, not dropping columns. Oct 24, 2009 at 13:52
  • I wasn't sure what the OP meant, hope my answer helps, updated to reflect this Oct 24, 2009 at 14:03
  • Much better to drop the ID columns and use the natural primary keys :D Oct 24, 2009 at 14:32
0

I don't get why you are doing it that way. I think your design is wrong.

If you are going to update/delete something that is in several tables, add a new table and relate every table to this one.

2
  • 2
    You won't get far with answers that begin with "I don't get why you are doing it that way. I think your design is wrong." Oct 24, 2009 at 14:06
  • @Chris: You are right, it sounds kind of offensive. My bad :(
    – Macarse
    Oct 24, 2009 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.