Deleting rows past a certain row index [duplicate] - sqlite

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have new data coming into the app all the time, so I want to limit the number of rows in a table to, say, 100 records. I would describe it as a FIFO queue. Whenever there new data (just a few rows a time) coming in, old data at the 'bottom' of the table are flushed out and deleted. Since it's FIFO, I don't want to manually perform a sort, then delete, then insert back in. I guess there must be cheap way to do this, right?
Thanks

A query like this will show all recors, newest first:
SELECT *
FROM MyTable
ORDER BY Date DESC -- or some autoincrementing ID column
With an OFFSET clause, you can skip the first records.
This means that you get all records except the first 100 ones, i.e., you get those records that should be deleted:
SELECT *
FROM MyTable
ORDER BY Date DESC
LIMIT -1 OFFSET 100
You can then use this in a subquery to actually delete the records:
DELETE FROM MyTable
WHERE ID IN (SELECT ID
FROM MyTable
ORDER BY Date DESC
LIMIT -1 OFFSET 100)

Related

Create an index-by table and populate with rows from emp, then loop over and print them [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Create an index-by table and populate with rows from emp, then loop over and print them.
hi can anyone understand this question and give me the suggestions to solve this please..
A collection is an ordered group of elements having the same data type
which can be accessed though subscript/index. Oracle PL/SQL supports
three different types of collection
Index-by tables or Associative array
Nested table
Variable-size array or Varray
Below is an example of how you can define a Index-by table and populate it with rows from Emp table and print them accordingly.
DECLARE
CURSOR c_emp is
select name from emp;
TYPE c_list IS TABLE of emp.name%type INDEX BY binary_integer;
name_list c_list;
counter integer :=0;
BEGIN
FOR n IN c_emp LOOP
counter := counter +1;
name_list(counter) := n.name;
dbms_output.put_line('Employee('||counter|| '):'||name_list(counter));
END LOOP;
END;
/
See Oracle Documentation as well as PL/SQL - Collections for more information on the same.

prevent repeating data retrieved from SQL database by a select statement

I have a database with a question table each question has a level attribute, topic, and the answers. I want to pick up randomly question in an ASP.net project but i don't want the same question to be repeated in the Details View.
This is the select statement:
SELECT TOP 3 [Question Number] AS Question_Number
,[Question Title] AS Question_Title
,[Answer 1] AS Answer_1
,[Answer 2] AS Answer_2
,[Answer 3] AS Answer_3
,QuizID
,Level
FROM Question
WHERE ( Level = 1 )
ORDER BY NEWID()
I don't think so you will get duplicate row until unless you have duplicate record in table. if you have then use DISTINCT to get unique record from table.
The basic syntax of DISTINCT keyword to eliminate duplicate records is as follows:
SELECT DISTINCT column1, column2,...columnN FROM table_name WHERE [condition]
http://www.w3schools.com/sql/sql_distinct.asp

Looping through multiple results in PLSQL [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a select query which returns 50 results. The select query returns id .
The next part i need to run a select query which value for all the 50 ids.
Please can some one tell me what is the best way to get it done.
is using a for loop a good idea
e.g.
begin
declare #count= select id from table1
for 1 to #count ..loop
select value from table2 where id =1
end loop
Can i use cursors what is the best method.
Please elaborate on this statement "The next part i need to run a select query which value for all the 50 ids."
However, if you want to fetch those 50 records in a pl/sql block and manipulate them in some way, you would require a cursor. Records from cursor can be fetched both with and without FOR loop.
Cursor example:
declare
cursor c1 is select col1 from table1;
begin
for a in c1
loop
dbms_output.put_line(a.col1);
end loop;
end;
The above piece of code would display the values from col1 when server output is on.
In a simple case such as you suggest I think it's a bad idea to build up a collection of values and then iterate through them to fetch data from a second table. It's a better idea to use a JOIN to, well, join together the data from the two tables based on whatever criteria you've got. In this case it appears you're select an ID value from TABLE1 which is also on TABLE2. Thus, you might consider something like the following:
FOR myRow IN (SELECT t1.ID, t2.VALUE
FROM TABLE1 t1
INNER JOIN TABLE2 t2
ON (t2.ID = t1.ID))
LOOP
-- Do something useful with the data
DBMS_OUTPUT.PUT_LINE('t1.ID=' || myRow.ID || ' t2.VALUE=' || t2.VALUE);
END LOOP;
Share and enjoy.
Given below can be onw good waye
declare
cursor c1 is select value from table2 where id in (select id from table1)
begin
for i in c1
loop
...
...
end loop;
end;

How to determine the last time when records have been inserted, updated or deleted for a table? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to make a search function for sql database , and how to get time when info from a table was edited?
How can I determine the last time when records have been inserted,
updated or deleted for a table ?
someone told me
select *
from sys.dm_db_index_usage_stats
where database_id = db_id( 'readpasttest' )
But I don't know how to use it I have a database table on asp.net site where I add informations like this:
edit-1.Name : Jax
edit-2.Age : 24
edit-3.Code : 12515
so when the users edit something lets say the name Jax it will be like this
edit-1.Name : newName - This was edited last time : the time/day !
Please help ,ty
For changes, you can add a datetime column to the table called 'LastModified' or something like that, and write the current date/time to it every time you write a record.
Alternatively, if you need to know when a record was deleted you can write a trigger that intercepts inserts/updates/deletes to the table in question, and write the timestamp value to another table, along with identifying information about the affected record.
The best way is to implement 'soft deletes' where a record is marked with some kind of status value (e.g. a 'IsDeleted' column) that can be flipped and subsequently ignored by other code in the application when reading from the table.

last record from database [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Get the last record from a Microsoft SQL database table using ASP.net (VB) onto a web form.
I'm assuming he's trying to retrieve the last inserted record. As Ariel pointed out, the question is rather ambiguous.
SELECT TOP 1 * FROM Table ORDER BY ID DESC
If you have an identity column called ID, this is easiest. If you don't have an identity PK column for example a GUID you wont be able to do this.
Here is a basic solution:
var order = (from i in db.orders
where i.costumer_id.ToString() == Session["costumer_id"]
orderby i.order_id descending
select i).Take(1).SingleOrDefault();
You need to be more specific with actually putting it onto a web form, but the SQL to get the last record is:
SELECT *
FROM TABLE_NAME
WHERE ID = (SELECT MAX(ID) FROM TABLE_NAME)
Where ID is your ID and TABLE_NAME is your table name.

Resources