Removing and Updating SQL

I am starting to learn SQL and I have some problems.

I have one database with 4 tables.

All 4 tables have a column ID.

Something like that:

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

      

I am making a selection 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 these 4 tables by ID? Can anyone help me? I start.

+2


source to share


3 answers


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 TABLE 1 WHERE.,.

The exception is that tables are linked to each other "officially" using PRIMARY and FOREIGN KEYS, you can UPDATE link columns and DELETE rows "stream" across linked tables. To do this, you use the CASCADE SQL parameter when declaring keys.

Some vendors offer additional custom extensions that allow you to UPDATE or DELETE from more than one table, but for those to whom you have to tell the database you are using.

+7


source


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 '

      

Delete lines that match 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

      

+2


source


I don't understand why you are doing this. I think your design is wrong.

If you are going to update / delete what is in multiple tables, add a new table and bind each table to that.

0


source







All Articles