How can I create a new table in SQL Server CE for Windows phone 8 c# - windows-phone-8

I want to create new table at runtime but I don't know how to do this.
When I want to insert to database when I haven't for example Person table I've got this error
The specified table does not exist
It's completely logical this table doesn't exist, now I have to create this table at runtime and I don't know how?

You must use the DatabaseSchemaUpdater class, as described here: https://msdn.microsoft.com/en-us/library/windows/apps/hh394018(v=vs.105).aspx

Related

Differences regarding neo4j and mySQL

I have to develop a project for college regarding databases. I have to basically develop the same project in a relational database like mySQL and in a non-relational database like neo4j. I'm pretty versed in mySQL but there is something in node4j that i don't understand.
While in mySQL i can just write this:
CREATE_TABLE 'A' (
'idA' INT not NULL
)
and this will create a table that has a column named idA and i may add rows to the table for specific values.
From what i've understood so far, in neo4j i can't really create something ambiguous like this? Every node or label that i create, has to have specific values for the properties i assigned to it. Is this correct? or am I missing something?
Thank you in advance.
The analogue to your empty table example is literally a label (e.g. A) for which there are no nodes.
If you wanted to ensure that the attribute idA existed on every A table node that is created in your database you could create a constraint on the label A something like this.
CREATE CONSTRAINT ON (a:A) ASSERT exists(a.idA)

MySQL Table does not exist, but it does

I am new to building databases, i have created a database for a website i am creating, and i keep getting this message "Executing SQL script in server
ERROR: Error 1146: Table 'mydb.franch' doesn't exist", when trying to forward engineer my database. The table does exist but it is not being read. (See image below). Please any help will be appreciated.
I can't add a comment, but in your image the table is "Franch" not "franch". Maybe that is the problem?
I may be wrong but I believe you are trying to access your table as (something like)
select * from 'mydb.franch'
or
select * from `mydb.franch`
In either case it's wrong and should be
select * from `mydb`.`franch`
MYSQL is case sensitive when creating a table, which means that you cannot create a table or variable with lower/upper case, and call it with upper/lower cases.
As such you must call the same name you used to create the table. In this case since the table is called mydb.Franch you would do: select * frommydb.Franch;`

Is there a way to copy the structure of a table (fields but no data) to a new table in MySQL?

For example, I have a table named movies. It has the fields/columns title VARCHAR(100) and runtime INT(5). It's loaded with 10,000 rows of data.
I want to create another table, let's call it movies_custom, that has all of the same columns, but with none of the data.
Is there a quick SQL statement to do this?
EDIT: Sorry, I noticed the SQL Server tag on this and assumed that was the technology you were using until I saw MySQL in the question title.
You can use the syntax CREATE movies_custom LIKE movies in MySQL
Sure!
In SQL Server you can do this query:
select top 0 * into movies_custom from movies
That creates the table structure without inserting any rows.
Very easy in MySQL:
CREATE newtable LIKE oldtable;
The other answers led me in the right direction, but I had to add the keyword TABLE in order to make it work with MySQL Workbench 6.
CREATE TABLE movies_custom LIKE movies;

Using MySQL without any procedures or functions

Is it possible to use any sort of logic in MySQL without using any procedures? My web hosting does not let me create any procedures so I'm looking for a workaround.
The type of thing I want to do is only add an item to a table if it doesn't already exist. Or add a column to a table if it's not already there. There are some operations that can be done such as CREATE TABLE IF NOT EXISTS and so on, but some operations I require do not have such luxuries :(
I realised late on that my lovely procs won't work and so I tried writing IF/ELSE logic as top-level queries, but for MySQL, IF ELSE blocks only seem to work inside functions/procs and not at the global scope.
Any workarounds greatfully received - I've already asked the hosting to grant me privileges to create procedures but no reply as yet...
I suppose you don't have access to the INFORMATION_SCHEMA either. You can possibly find solutions but it would be better, in my oninion, to:
Change your hosting provider. Seriously. Pay more - if needed - for a MySQL instance that you can configure to your needs. You only have a crippled DBMS if you are not allowed to create procedures and functions.
Posible workarounds for the specific task: You want to add a column if it doesn't exist.
1) Just ALTER TABLE and add the column. If it already exists, you'll get an error. You can catch that error, in your application.
2) (If you have no access to the INFORMATION_SCHEMA) maintain a version of the schema, for your database.
The best solution that I can think of would be to use an additional language with SQL. For example, you can run a query for a specific record, and based on the response that you get, you can conditionally run an INSERT statement.
For inserting a table if it doesn't exist, try using the SHOW TABLES statement and testing whether or not a name exists in the result set.
MySQL supports INSERT IGNORE. and INSERT ... ON DUPLICATE KEY UPDATE.
The following will insert a new row, but only if there is no existing row with id=10. (This assumes that id is defined as a unique or primary key).
INSERT IGNORE INTO my_table (id, col1, col2) values (10, "abc", "def");
The following will insert a new row, but if there is an existing row with id=10 (again, assuming id is unique or primary), the existing row will be updated to hold the new values, instead of inserting a new row.
INSERT INTO my_table (id, col1, col2) values (10, "abc", "def")
ON DUPLICATE KEY UPDATE col1=VALUES(col1), col2=VALUES(col2)
Also, CREATE TABLE supports the IF NOT EXISTS modifier. So you can do something like:
CREATE TABLE IF NOT EXISTS my_table ...
There are many other similar options and modifiers available in MySQL. Check the docs for more.
Originally I created a big script to create or update the database schema, to make it easier to deploy database changes from my local machine to the server.
My script was doing a lot of "if table 'abc' exists and it doesn't have a FK constraint called 'blah'" then create an FK constraint called 'blah' on table 'abc'... and so on.
I now realise it's not actually necessary to check whether a table has a certain column or constraint etc, because I can just maintain a schema-versioning system, and query the DB schema-version when my app starts, or when I navigate to a certain page.
e.g. let's say I want to add a new column to a table. It works like this:
Add a new migration script to the app code, containing the SQL required to add the column to the existing table
Increment the app's schema-version by 1
On app startup, the app queries the DB for the DB's schema-version
If DB schema-version < app schema-version, execute the SQL migration scripts between the two schema-versions, and then update the DB schema-version to be the same as the app
e.g. if the DB's schema-version is 5 and the app version is 8, the app will apply migration scripts 5-6, 6-7 and 7-8 to the DB. These can just be run without having to check anything on the DB side.
The app is therefore solely responsible for updating the DB schema and there's no need for me to ever have to execute schema change scripts on the local or remote DB.
I think it's a better system than the one I was trying to implement for my question.

MySql TABLE data type

MS SQL Server has a TABLE data type which can be used in stored procedures,
Does anybody know if MySQL has an equivalent data type?
I've had a look over the docs but can't seem to find anything, so I presume it doesn't exist, but perhaps somebody has created a workaround
Neil,
MySQL has no such data type and I believe it is good that it doesn't. To achieve similar results, use CREATE TEMPORARY TABLE construction. Name clashes are avoided by having per connection temporary tables:
A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed. This means that two different connections can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.)
Hope it helps.