I have a database with table a with column b.
b
1 a-b
2 a
3 a-b-c
how can I delete all rows from the table where - occurs more than once?
Use like:
delete from t
where b like '%-%-%';
Related
I've been trying to select multiple values from another table (i.e. texts in multiple rows) and display it on main table so that multiple values can be on one cell.
(also, I don't speak English, so some terms might sound different, sorry)
Example:
1st table:
ID
Name
Value1
1
Cell 2
A; B
2
Cell 4
B;C;D
2nd table (column Value1 is connected to the previous table's Value1 column):
ID
Value1
1
A
2
B
3
C
4
D
5
E
So in the first table I want to put both values in one cell from the 2nd table
Thank you!
At the moment, I am able to select only one value for Value1 column in the first table that is referenced to the second table.
On phpMyAdmin, I tried to add several columns on one constraint, but error occurred.
I have an object Head (table Head) which can have 0 or n positions (table Position).
HEAD table
HEAD_ID NAME
1 A
2 B
POSITION table
HEAD_ID POS_ID VALUE
1 1 X
1 2 Y
2 1 Z
3 1 DELETE ME
Unfortunately it is not possible to create foreign keys to maintain the data integrity. Therefore I want to create a delete script to delete Positions which do not have a corresponding head.
My delete script
DELETE POSITION
WHERE HEAD_ID NOT IN (SELECT HEAD_ID FROM HEAD)
Question: How does the command work if rows are inserted during the execution of the delete script into the tables? In my scenario both tables have several 10.000 of rows and the search may take some time.
If I understand it correctly, the list of HEAD_IDs from HEADS is created once at the beginning of the command. Therefore newly added rows will not be in the list and will be deleted. Is that correct?
The command would delete the position with HEAD_ID = 3 and POSITION_ID = 1 on my example, since the head is missing.
But how does it work, if after the SELECT and before the DELETE, new entries will be added to the both tables:
HEAD table
HEAD_ID NAME
1 A
2 B
4 NEW HEAD
POSITION table
HEAD_ID POS_ID VALUE
1 1 X
1 2 Y
2 1 Z
3 1 DELETE ME
4 1 WILL I BE DELETED?
Will the new position with the HEAD_ID = 4 and POSITION_ID = 1 be deleted since the head was not in the SELECT?
Any way to do perform the task in a safer way?
You can use MySQL table locking feature for you delete operation, then no new data will insert until your delete operation finish.
First Lock the tables
LOCK TABLES table_name WRITE
Then do your delete operation and after release the table lock
UNLOCK TABLES;
MySQL allows a client session to explicitly acquire a table lock for preventing other sessions from accessing the same table during a specific period.
A client session can acquire or release table locks only for itself. And a client session cannot acquire or release table locks for other client sessions.
Ref : https://www.mysqltutorial.org/mysql-table-locking/
I have three tables xx_1 , xx_2, xx_3 such that :
xx_1
id obj_version_num location
1 x ubudu
2 x bali
3 x india
xx_2
id name grade
1 abc band 1
2 xyz band 2
3 gdgd band 3
xx_3 has :
Name details col1 p_id
abc A HDHD 10
xyz B HDHD 20
gdgd C HDHD 30
smith D HDHD 40
I want to delete data from xx_1 and xx_2 if the name is smith in xx_3
Currently i am doing :
delete from xx_1
where id in (select distinct id from xx_2 t ,xx_3 k
where t.name=k.name
and k.name ='Smith')
and then
delete from xx_2
where name ='Smith'
Is there anyway i can delete data from both these table together ? without creating two separate scripts ?
There is no way to delete from many tables with a single statement, but the better question is why do you need to delete from all tables at the same time? It sounds to me like you don't fully understand how transactions work in Oracle.
Lets say you login and delete a row from table 1, but do not commit. As far as all other sessions are concerned, that row has not been deleted. If you open another connection and query for the row, it will still be there.
Then you delete from tables 2, 3 and then 4 in turn. You still have not committed the transaction, so all other sessions on the database can still see the deleted rows.
Then you commit.
All at the same time, the other sessions will no longer see the rows you deleted from the 4 tables, even though you did the deletes in 4 separate statements.
EDIT after edit in question:
You can define the foreign keys on the 3 child tables to "ON DELETE CASCADE". Then when you delete from the parent table, all associated rows from the 3 child tables are also deleted.
You cannot delete from multiple tables in a single statement, primary key or not.
I just started using SQL and need to perform the following task.
1) First, I need to dupicate a table 100 times. For all these duplicated tables, I want to keep its content unchanged. But I also want to update the primary key by one.
2) Secend, I want to concatenate all these duplicated tables together by row, and also concatenate them to the original table by row.
Example: The original table looks like:
ID CATEGORY
1 A
2 B
… …
26 Z
And I want to duplicate this table, and concatenate it to the original one. I want to maintain the colums other than the primary key (ID here) unchanged, and update the primary key by one each time. I want to get:
ID CATEGORY
1 A
2 B
...
26 Z
27 A
...
52 Z
How to do this? Thanks!
I have only one column roll number in my oracle table. Lots of data are there in one column.
roll number
-------------
1
2
3
4
5
.
.
.
When i want to delete some roll numbers out of this table then i am writing:
delete from table name where roll number='2'; // 1 row deleted.
if i want to delete 5 from table then
delete from table name where roll number='5'; // 1 row deleted.
Similarly when i want to delete 100 records like this then i have to replace roll number field and it is quite time taking.
Is there any quick method how can i delete selective rows from the table?
If the ids to delete are consecutive you could use BETWEEN:
DELETE FROM your_table WHERE roll_number BETWEEN 5 AND 104
If not, you could use IN:
DELETE FROM your_table WHERE roll_number IN (5, 9, 110, ... )
You can get the list of number from another query and put it in a IN condition, so for example :
delete from table name where roll number IN (select roll number from table WHERE somecondition);
If u already know the range u can use Between else if u know the total numbers to be deleted then u can use IN like....
delete from table where roll number BETWEEN ....;