ADOQuery and DBNavigator - database

OK. I have simple Database ,DBGrid and DBNavigator . I use this code for my adoquery
and view in this in my DBGrid
select Count(*)As 1 , 2 3 from Table1
Group by 1, 2
The problem is that if i have two or more the same article from 1 and i try to delete it from DBnavigator it send me
Key column information is insufficient or incorrect.Too many rows were affected by update.
I want to delete two or more rows.Not only one.
How to fix that ?

I guess it won't be possible using DBNavigator in a usual way (without override delete button event), because the dataset holds more than one record in a DBGrid row and IMHO the grid doesn't take care if you have data in a group. I would say if you select some grouped row and try to delete it, "DBNavigator" checks if there is some primary (unique) key for a row, but in this case, there is no one.
But it's just my notion, I can't verify it now.

Related

"FOR UPDATE...SELECT * FROM DELETED" Trigger copying to table with two extra columns

I have a very simple trigger that copies the current row of
Authors: AuthorID(PK),AuthorName,Bio
AuthorsHistory: HistoryID(PK), AuthorID, AuthorName, Bio, hWhen(dateTime)
hWhen uses getDate() to fill in its value.
CREATE TRIGGER [dbo].[AuthorHistory]
ON [dbo].[B_Authors]
FOR UPDATE
AS
INSERT INTO B_AuthorsHistory
SELECT *
FROM deleted
There are more columns but they don't matter for this point. All columns are the same types, same null settings, same default values, other than the PK changing.
It works perfectly, but today I wanted to add a column.
I tried to add it to Authors which immediately threw an error, something about column list in the trigger. (I'm using Select *)
I added the column first to AuthorsHistory, tried Authors again, and it threw a different error directly mentioning the trigger.
I found a resolution, remove the trigger, adjust columns, add the trigger, but I also had to remove hWhen from AuthorsHistory temporarily (this isn't an issue at the moment, still building it).
Another option, probably, is to be specific with column names and values. I do this in code, but I like the idea of one less thing to update if I can avoid it.
The question: is there a way I can do this with SELECT *?** I have an identical trigger (ie "Products ==> ProductsHistory") on other tables as well.
As I see, the only way to add a new column to both tables, so that trigger wouldn't fail, is to do it in one query like that:
;disable trigger dbo.AuthorHistory on dbo.B_Authors;
alter table dbo.B_Authors add NewColumn int null;
alter table dbo.B_AuthorsHistory add NewColumn int null;
;enable trigger dbo.AuthorHistory on dbo.B_Authors;
But if you have high loaded system, you may lose some data in dbo.AuthorsHistory if any other transaction would change data in dbo.Authors in the middle of the query.

Best practices to get a new row id from database

hi i am creating a project where i am actually having 3 related tables which are connected to one table like below
table 1
id
name
table 2
id
tb1_id
random_thing
table 3
id
tb1_id
random_thing
i can not basically go with an option where i can create a row in table1 first and then tb2,tb3 . client wants everything to be done on single button . so i am creating a new blank row whenever the page is called and getting the new tb1_id and then linking everything and go with single button but the problem is i can delete unused rows like 2-3 days later but thats ridiculous so is there any other best practices to get over situations such as this?
Edit
Explanation with an example will be really helpful , i am good to go with any database or any language just example has to be good so i can understand how its done. sorry but i am one of those guys who hates theory and love practicals :d
The best practice is to use explicit foreign key relationships and transactions.
So, the basic idea is:
Begin a transaction.
Insert a row in table 1 with the name.
Get the id of the newly created row, ideally using a returning or output clause (depending on the database).
Insert a row in table 2.
Insert a row in table 3.
Commit the transaction.
When using transactions, just be careful to rollback the transaction if it does not complete for any reason.
As for deleting rows, you can have "cascading delete" options on the foreign key definitions, so if the parent row is deleted then the related rows are also deleted.
Some databases (notably Postgres) offer some functionality where you can put all this into a single statement using CTEs that modify the data. The idea is still the same, just easier to code.
I should note that there are perfectly reasonable alternatives. For instance, you could create a view on the "data" columns of the three tables and create insert/update/delete triggers on the view. Personally, I find that hiding this functionality in triggers makes it more difficult to understand and maintain. I think that is a personal opinion and this is also a reasonable approach.

SSRS. How to create new Row Group in right side of the table1_Details_Group?

I'm creating Report with Microsoft Visual Studio 2013.
PROBLEM:
If I'm adding new Row Group It automatically adding before table1_Details_Group
QUESTION: How to change order of the groups or add new group in right side of the table1_Details_Group?
EXPLAINATION:
For now, after adding row group It looks like:
Picture 1
As you see in picture 1 group is created in left side of table1_Details_Group
It should be like that:
Picture 2
As you see in Picture 2 is shown how I want to move Column3 with grouped rows.
If impossible to make that is shown in Picture 2, have you ideas how to add all group with parent and all childs to right side of table1_Details_Group as shown in Picture 3 below?
Picture 3
For anyone else coming here, you can insert a second detailed table inside a cell of the main table that has the detailed breakdown. This detailed table can be in any column without affecting the way rows are broken down in columns to the right or left.
So to solve the original question, first create Columns 1-3 in picture 1, and only include the data groupings required by those columns (ie. don't create the detailed row groupings for columns 4-5). Then insert the column where you want to show the detailed rows (ie between columns 2 and 3), and insert a new table inside that cell. This inner table will already be locked to share the same dataset as the main table. Then you can set the grouping of the inner rows and add whatever cells, rows or columns you need to the inner table. The columns to the right in the main table will still show the total, as in Picture 2.
I found solution, I don't know If It's good practice, but It worked for me.
I've created second table in right side, added similar group as in table1, and from new created table2 deleted all grouped columns, left only column which I needed, in this example Column3. So for now It looks like 1 table. Column1 and Column2 from Table1 are associated with Column3 from Table2.
You can change layoutdirection of table from LTR to RTL.
go to tablix properties and collapse localization and then change layoutdirection
I have had this same issue. The use of a second table was not perfect for me because when the report is viewed in HTML there is a space between the two tables, which looks weird (it worked fine in PDF view).
To resolve the issue, I created the table in picture 1 then removed all non-grouped columns (the table requires one visible column, so I had to keep a dummy column set to hidden).
Then I added new columns inside the grouping. In the cells for these columns I inserted another table. This inner table only has one cell with the value that I needed to display. The inner table then gets a filter so that it only displays values related to the current row of the parent table.

how to copy structure and data with specific column in visual foxpro

Please help, I have large database in DBF where have 34 column and more than 50k record.
In this case, I need 10 column only from 34 column. Logically, if I delete not necessary column will reduce data load.
So I will copy structure and data from source file with specific column, but I do not know how to do it in visual foxpro.
Will you help me for this case ?
Before and after, thanks for your attention to read my problem.
Regards,
Pathic
Use the following syntax:
SELECT col1,col2,col3,col4,col5,col6,col7,col8,col9,col10 FROM table1 INTO TABLE table2
If you only want to REMOVE the column PERMANENTLY, first, for grins, make a backup copy of the file. Then, make sure you are out of any program using that table and start VFP. In the command window, enter
USE C:\SomePath\YourTable EXCLUSIVE {enter}
*/ Now, get the one column you want copied out... you can just add as many
*/ Columns, such as any primary key reference you may also want to include to.
COPY TheOneColumnYouWant to C:\SomePath\NewTableForThisColumnOnly {enter}
*/ Now, to remove the column from the original table, now that is has been moved
*/ to the "other" table.
MODIFY STRUCTURE {enter}
This brings up a modify table structure where you can alter / delete ANY column. Scroll down the list to the column you want to delete, and at the bottom, click the DELETE button, then click the OK button to confirm structure changes. It will then come back with a confirmation prompt to "Make structure changes permanent?" and select Yes. Column now gone.
An alternative to deleting columns in VFP, you can use Alter Table via...
ALTER table C:\SomePath\YourTable DROP COLUMN TheOneColumnYouWant

Deleting a single row from a table that has multiple entries?

So here's my situation.
I have a table in my database, it has multiple entries, but I need only one to be deleted.
When trying to google this, all I could find was questions referring to deleting all except one of these entries, while I need to delete ONLY ONE and leave the rest.
I'm completely confused on this, and don't really understand how to do it.
You need to use a where clause to identify the row that you want to delete, preferably by the primary key of the table. For example:
delete from MyTable where id = 1
If you post your table's schema, we can help with better syntax.
The row you are trying to delete should have a unique way to identify it, typically the primary key. You would then use the WHERE clause to specify your query to DELETE it:
DELETE FROM sparkles WHERE primaryKey = 1
Alternatively, if there are no keys, you would filter by criteria that matches all values of the record you wish to delete.
DELETE FROM sparkles WHERE col1 = 5 AND col2 = 15 AND col3 = 51
If you are trying to achieve this in Java, you would most probably be using JDBC however if this is just a onetime effort you can directly run the delete query with appropriate where clause.
Well I don't see how this is tagged as Java. But it sounds like you want to use limit operator, like this:
DELETE FROM somedatabase WHERE someentry = 5 LIMIT 1;
The response two above me is good to but leaves out LIMIT.
Step by step for doing it in phpMyAdmin....
Go to phpMyAdmin interface
Select the database
Select the table
Click Browse
Look for the row you are looking to delete and click the delete Icon
Confirm deletion of that row
Done.
If you have 5 Fields in table and you want to delete only one field for particular instance then you should fire update query giving the null effect to that filed...
Suppose If table is
Id | Name | MobNum
And you want to delete MobNum of record 5 ...then query can be ike this>>>
update tlbName set MobNum=null where Id='5'

Resources