So here is my problem, I want to add another entry to my database into a specific row.
eg. I have a table "example" then 2 column "name" "age", there are existing data to my table, I have 3 entry
jude,15,
joy,17,
john,16
respectively but I want to insert another entry
jason,18
in the row before "joy,17".
Is it possible or impossible?
Francis, the data in a SQL database has no implicit order. So, you can't count on the data coming out in the same order in which you inserted it. The order in which data comes out of a SQL database can only be guaranteed by using an 'ORDER BY' clause on the SQL statement that brings it out of the DB.
Thus, if you want a different sort order than sorting by the keys you've provided, you probably have to add a 'sort' column to your table and use that column in the ORDER BY portion of your SELECT statements.
Related
I've a list of records (in excel) which I want to lookup in SQL table to find their entry date in table.
For example I've name of 200 customers in an excel sheet which are also in my SQL table but there are many others as well. I want to compare those users in table and find their date of joining the table i.e the date they were added to the table.
Do you have any column such as "customer_id"? If not, create it in the tables as it will make it easier for you to select and join tables. You can use the alter table statement to add the new column. After adding the columns, join the tables and use the select statement to find the required record.
You must have a key to bind those data. Any other way can be dangerous linking these informations. Maybe, the way is adapt your application or excel to provide this bind between the data.
I have to run a series of checks (governed by the table "Checks") and store the results in a table "Checks_result" (in a mysql database).
The table "Checks" contains an identifier (checkno) and a sql-statement (possibly returning many rows with a single value) to be executed.
The table "Check_results" has to contain all the rows returned from the sql-statement, with a reference to checkno and an autoincrement row checkentry for each returned row from the sql-statement.
Is it possible to do this?
What I was suggesting was when your table has the 2 SQL statements, you should read each record and construct another SQL statement along the lines of:
insert into check_results(checkno, checkresult )
select 1, i.val1-i.val2 from import i;
The select just needs the checkno added into it and the checkentry should be an autoincrement column.
How can I order values from a table, ascending from the time they were inserted. There is no special column for this matter, like a timestamp or autoincrement.
I know this is not recommended to do... Still I would like to know how to do this.
As I understand from the answers, if no sorting columns e.g: timestamp or autoincremental were added before the values were inserted, there is no way of sorting them by insertion.
There is no guarantee that rows will be returned in any particular order without an ORDER BY clause in the query.
Consider a simple query that returns all columns of all rows in a table. For example:
SELECT * FROM mytable ;
For that query, it is likely that MySQL will perform a full table scan, from the beginning of the table. So it is likely that the rows will be returned in the order they are found in physical storage.
This may roughly correspond to the order that rows were inserted, if there have been no deletes, no updates and no reorganization, where space for an inserted row was later reclaimed, and reused to store a newly inserted row.
But this behavior is NOT guaranteed.
To return the rows in the order that they were inserted, the query must specify the sequence that rows are to be returned, by including an ORDER BY clause.
For the rows to be returned in "insertion order", that means the query needs to be able to have that information available, or be able to derive that. For a simple query against a single table, that means the information needs to be stored in the row.
You can ORDER BY something you can get out of your table. If you do not have anything in there that can be used to find out the order you need, you cannot order by it.
Depending on the data in the table, you may be able to order by the id of the data - if the data has a single incremental integer to assure PK uniqueness. There is no other way to sort on insertion order unless the data is captured and recorded in the table.
I don't know of anything in MySQL that retains extra (meta) information on records that you have not specified at the table level.
There needs to be a column to order your query by. Usually this would be an insertion timestamp, or incrementing id/incrementing key. There is no way to guarantee the order otherwise, because there is no record of it.
relevant thread from MySQL forum
I put a sort component to sort my data.
and the data was sorted.
but my destination table is unordered!
How can retain the Order of sorted rows while Inserting them into sql Table with ssis?
There is no inherent ordering of rows in a SQL Server table. You'll need to either add a 'sort order' column or write your queries so that they produce properly sorted result sets.
You can use an IDENTITY column as your 'sort order' columns, since it will increment as things get inserted.
Understand that repeated executions of a given query against a sql database are specifically not guaranteed to return results in the same order, so your queries need to do it each and every time.
Rows in a relational database do not have any "order" - they are like water molecules in a bucket! If you need to have an order then you must include another column that you can use to order by - e.g. an autoincrement field, a timestamp, or some column from external data. You can then use that column to order your data when you query it - otherwise you won't get ordered data.
what if I wanted to update the records in the table by altering values in one of the columns?
I have records in the table that have one column empty(null values). I want to change these values and insert values from another table into those records.
Basically I have a table with one column empty. I do not want to append to the end of the table but start inserting from record 1.
For the existing records, you would have to use UPDATE to update that one column, WHERE thatColumn IS NULL.
Shouldn't the values in that column have some relation to the rest of the record? I could understand initializing the existing records to a non-null value, or using an UPDATE query to populate data from another table in that column, but all related to the original row...
UPDATE old SET old.badColumn = new.newData
FROM oldTable old
JOIN newTable new on old.someID = new.someID
This would find the related data in newTable matching oldTable, and update the badColumn to some data from newTable... let me know if you need more help.
See the "Using the UPDATE statement with information from another table" section from this page of SQL Server Books Online.