I have an SQLite database which a set of tables. All data in these tables can be isolated into a groups of sets by some id. For example:
Table A
ID value1 value2
1 asd fgh
2 sdf ghj
Table B
ID ID2 value4
1 10 vbn
2 11 bnm
Table C
ID2 value5 value6
10 asdfg qwer
11 tyui hjkl
Where each ID column will map the other ID and each ID2 will map to the other ID2.
I want to take this database, and generate a series of smaller databases, each of which have the same structure, but will only contain data from 1 ID:
Database1.sqlite
Table A
ID value1 value2
1 asd fgh
Table B
ID ID2 value4
1 10 vbn
Table C
ID2 value5 value6
10 asdfg qwer
Database2.sqlite
Table A
ID value1 value2
2 sdf ghj
Table B
ID ID2 value4
2 11 bnm
Table C
ID2 value5 value6
11 tyui hjkl
I could just create the tables one by one, gather all data per ID through a series of SELECT statements, then add it through a series of INSERT statements, but I think there has to be a better way.
My other idea is that I can create a series of views, each of which isolates the data into the format above. From there, I could just write these series of views an sqlite file as a database.
My question is how realistic is my view generation idea? Would it be possible to generate a series of views that mimic each table's structure, but for say where ID = 1 and then save those views as an sqlite file? All of this will need to be done in C#. Is there a better way to do what I am trying to do?
Some More Info
These tables can have multiple rows with the same IDs. There will also need to be some primary key / foreign keys for each table. Ideally, we could then take these smaller tables, and then compress them all into a larger table in the future.
It is possible to combine INSERT and SELECT.
Together with ATTACHed databases, this allows to do the copying with one statement per table:
ATTACH 'C:\some\where\Database1.sqlite' AS db1;
CREATE TABLE db1.A(ID, value1, value2);
CREATE TABLE db1.B(ID, ID2, value4);
CREATE TABLE db1.C(ID2, value5, value6);
INSERT INTO db1.A SELECT * FROM main.A WHERE ID = 1;
INSERT INTO db1.B SELECT * FROM main.B WHERE ID = 1;
INSERT INTO db1.C SELECT * FROM main.C
WHERE ID2 IN (SELECT ID2 FROM B WHERE ID = 1);
ATTACH 'C:\some\where\Database2.sqlite' AS db2;
CREATE TABLE db2.A(ID, value1, value2);
CREATE TABLE db2.B(ID, ID2, value4);
CREATE TABLE db2.C(ID2, value5, value6);
INSERT INTO db2.A SELECT * FROM main.A WHERE ID = 2;
INSERT INTO db2.B SELECT * FROM main.B WHERE ID = 2;
INSERT INTO db2.C SELECT * FROM main.C
WHERE ID2 IN (SELECT ID2 FROM B WHERE ID = 2);
Related
I am trying to write my first c# app. And don't know how to compare 2 columns to calculate another field
This is table 1 (Prediction table):
Col A Col B
--------------------------------------------
Prediction Home team Prediction Home Score
Liverpool 5
Arsenal 2
And this is table 2 (Result table)
Col A Col B
----------------------------------------
Actual Home team Actual Home Score
Liverpool 5
Arsenal 1
I would like to compare the 2 columns b so if both same score then 5 points or 0 points if not matched.
The SQL part is this, as for the C# part we will need more details
CREATE TABLE prediction
(
prediction_home_team VARCHAR(100),
prediction_home_score INT
);
CREATE TABLE result
(
actual_home_team VARCHAR(100),
actual_home_score INT
);
INSERT INTO prediction (prediction_home_team, prediction_home_score) VALUES ('Liverpool', 5), ('Arsenal', 2);
INSERT INTO result (actual_home_team, actual_home_score) VALUES ('Liverpool', 5), ('Arsenal', 1);
/*
*
* QUERY:
*
*/
SELECT prediction.prediction_home_team Team,
prediction.prediction_home_score PredictedScore,
RESULT.actual_home_score ActualScore,
CASE
WHEN prediction.prediction_home_score = result.actual_home_score THEN 5
ELSE 0
END Points
FROM prediction
JOIN result ON result.actual_home_team = prediction_home_team
To copy data from one database to another in different server with same schema, I am planning to use SqlBulkCopy class from C sharp library. Whether SqlBulkCopy will maintain the same order as it is in the datatable while inserting the records ?
Example: id is the identity column.
Server1, db1
TableA
id name
1 name10
2 name20
3 name30
4 name40
Server2, db1
TableA
id name
1 name1
2 name2
3 name3
4 name4
..........
..........
5000 name22
5001 name33
Step1: var dt = select * from server1.dbo.TableA order by id;
Step2: SQL bulk copy into server2 bulkCopy.WriteToServer(dt);
Step3: var resultDt = select top 4 id from server2.dbo.TableA order by id desc. Since we know the number of records we inserted I am using "top 4".
Step4: resultDt.DefaultView.Sort = "id asc";
Question: Whether id in resultDt will represent id in dt for all the rows ? i.e,.
5002 from server2 = 1 from server1
5003 from server2 = 2 from server1
5004 from server2 = 3 from server1
5005 from server2 = 4 from server1
Note: Just for example purpose I have given less records. Actual table contains some thousands of records.
It looks like there is no guarantee of order while bulk insert. So I added a temporary id column to the destination table. Flow will be as follows:
Step1: var dt = select *, id as tempId from server1.dbo.TableA order by id;
Step2: SQL bulk copy into server2 bulkCopy.WriteToServer(dt);
Step3: var resultDt = select top 4 id, tempId from server2.dbo.TableA order by id desc. Since we know the number of records we inserted I am using "top 4".
Now id will be the new id generated by server2 and tempId will be the id from server1. Problem solved :)
Another solution is available in the below link:
https://social.msdn.microsoft.com/Forums/en-US/ee3d90b0-d212-4bf0-8f2a-5dcb716896eb/sqlbulkcopy-insert-order?forum=adodotnetdataproviders
Create staging table(say tmp_tableA) with same structure of tableA
without identity.
Bulk copy the data into tmp_tableA.
Now insert into tableA from tmp_tableA.
Dear Friends,
i want to select two columns from two different tables in the same db using mysql and set the output of the query to a variable in c#.
currently my code is as shown below:
MySqlCommand logcmdCheck = new MySqlCommand(query, connectionCheck);
string query = "SELECT DB.table1.column1,DB.table1.column2,DB.table2.column1,DB.table2.column2,DB.table2.column3 FROM DB.table1 WHERE DB.table1.column1=?x,DB.table2 WHERE DB.table2.column1=?y";
logcmdCheck.Parameters.AddWithValue("?x",UserName);
logcmdCheck.Parameters.AddWithValue("?y",emailID);
MySqlDataReader ldr = logcmdCheck.ExecuteReader();
A = ldr[0].ToString();
B = ldr[1].ToString();
C = ldr[2].ToString();
D = ldr[3].ToString();
E = ldr[4].ToString();
Error: Mysql query syntax is wrong.
Kindly please help me out with the mysql command to perform the requirement.
Thanks in advance
Suraj
You're going to have to use a SQL Join. Check it out here http://www.w3schools.com/sql/sql_join.asp. You need to have a foreign key in one of the tables that allows you to connect to the primary key of the other table. Every good database should be set up with tables that have foreign keys.
For example:
Table 1:
OrderNumber Name Order Total
1 John Smith 10.00
2 Sally Smith 5.00
3 Berry Jones 25.00
Table 2:
Item Number ItemTotal OrderNumber
1 5.00 1
2 5.00 1
3 2.50 2
4 2.50 2
5 25.00 3
In table 2 the OrderNumber is the foreign key that is able to join to table one. So your syntax would be:
SELECT * FROM table1 JOIN table2 ON table2.OrderNumber = table1.OrderNumber
That will give you one table which you can read from.
I am having trouble getting one of my LINQ to SQL queries to work correctly. I have three tables setup as follows:
Vendors
id - primary key
Manufacturers
id - primary key
ManufacturerVendorRelationships
id - primary key
manufacturer_id - foreign key to manufacturer.id
vendor_id - foreign key to vendor.id
I am trying to write a LINQ to SQL query that will fetch the Vendors that are not currently related to a given manufacturer. For example, the following table data should only result in 2 vendors for the manufacturer with ID of 1. Can someone please help me with the LINQ to SQL for this example? Right now, the application is doing this logic. Thanks in advance.
Vendors
ID
1
2
3
4
5
Manufacturers
ID
1
2
3
4
5
ManufacturerVendorRelationships
ID ManufacturerID VendorID
1 1 1
2 1 2
3 1 3
Maybe something like this:
var result=(
from v in db.Vendors
where !db.ManufacturerVendorRelationships
.Select(s=>s.VendorID)
.Contains(v.ID)
select v
);
Or if you want it like a field:
var result=(
from v in db.Vendors
select new
{
v.ID,
HasManufacturer=db.ManufacturerVendorRelationships
.Select(s=>s.VendorID)
.Contains(v.ID)
}
);
where db is the linq data context
int id= 1;
var result =
dc.Vendors
.Where(v => !dc.ManufacturerVendorRelationships
.Any(rel => rel.VendorId == v.Id && rel.ManufacturerId == id));
Say i have an image :
This signifies that 1 has friends 2,3,4 and 6 is also the friend of 1.
How do i get these ids 2,3,4 and 6 that are friends with 1 using SQL
that depends a bit on which DB you are using - the following will work in MySQL and Oracle and perhaps in MS SQL server (not sure):
SELECT
(CASE WHEN ID1 = 1 THEN ID2 ELSE ID1 END) AS THEFRIENDS
FROM YOURTABLE WHERE
ID2 = 1 OR
ID1 = 1
This one works everywhere but is perhaps less performant:
SELECT ID1 FROM YOURTABLE WHERE ID2 = 1
UNION
SELECT ID2 FROM YOURTABLE WHERE ID1 = 1
select id2 as FriendID from table
where id1 = 1
union
select id1 as FriendID from table
where id2 = 1
Use more descriptive names for starters
SELECT * FROM table WHERE id1=1 or id2=1 and id1<>id2