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)
Related
Say I need a table that looks like this:
CREATE TABLE Record (
Id INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED,
Guid UNIQUEIDENTIFIER UNIQUE NONCLUSTERED,
Version ROWVERSION,
DateOfBirth DATETIME2,
Name VARCHAR(64) NOT NULL
)
What's the recommended way of creating the table above using PetaPoco and NPoco?
PetaPoco is all about using SQL for what SQL does best. Therefore, the correct way to create a table with PetaPoco is to create it with SQL. We've had a few requests to add schema generation from POCOs, but every supported DB has their own take on DDL, and thus to add it (properly) would be a big undertaking.
It may be added some day, but right now development time is better spent on other features.
That said, PetaPoco is open source, so a PR, if done properly, for schema generation would be happily accepted ;)
Finally, although I don't follow NPoco all the closely, I don't think it has support for schema generation from POCOs, and at a guess, it would be due to the same reason listed above.
I use a migration tool (DbUp) to keep up with the changes in the schema.
Is there a way I can force my MySQL database to allow creation of a new table with name table_xyz, only if a record with table_xyz exists in another table?
Basically I want my table name to be a foreign key.
e.g. allowed_tables structure:
table_id | other columns |
--------------------------
table_xyz| bla bla bla |
--------------------------
Because I have a record with table_xyz in allowed_tables, I can create a table with this name. If I delete the record, the table should automatically be deleted. The same must happen if I change the table name.
Edit 1:
The table allowed_structures actually is called loans. It contains loan_id, and other columns required on the contract. After I insert this record, I create a table with the name < loan_id > ( a record which must exists in loans table). If I update/ delete a record with < loan_id > in loans table, I want that table name to be changed automatically, not via a PHP script. Currently is done via a PHP script.
I need to know which is the best option to do this and why. Thanks!
This was originally written for SQL Server, but concept is same for every RDBMS.
Yes it is achievable, but Curse and Blessing Dynamic SQL by Erland Sommarskog
CREATE TABLE #tbl
The desire here is to create a table of which the name is determined at run-time.
If we just look at the arguments against using dynamic SQL in stored procedures, few of them are really applicable here. If a stored procedure has a static CREATE TABLE in it, the user who runs the procedure must have permissions to create tables, so dynamic SQL will not change anything. Plan caching obviously has nothing to do with it. Etc.
Nevertheless: Why? Why would you want to do this? If you are creating tables on the fly in your application, you have missed some fundamentals about database design. In a relational database, the set of tables and columns are supposed to be constant. They may change with the installation of new versions, but not during run-time.
EDIT:
In SQL Server I would use trigger for source table (FOR INSERT/UPDATE/DELETE) and using Dynamic-SQL I can achieve exactly what you want, but still I think this is madness.
If you want to go this path check if MySQL supports the same.
I need to rename a Django Model in the app "myapp" from "Hotel" to "Client" and I would prefer not to use South.
I am wondering it safe to handle the changes in MySQL using queries such as the following?
RENAME TABLE myapp_hotel TO myapp_client;
RENAME TABLE myapp_hotel_sites TO myapp_client_sites;
and for tables with a Foreign Key relationship to the now Client table:
ALTER TABLE myapp_client_sites CHANGE hotel_id client_id int(11);
Can it be as simple as that, or am I missing something?
Changing the table name can be done like this in MYSQL, but take into consideration you will also need to change the class names within the django code also. Else the orm will not map with the mysql database table names.
Using a tool like south will ensure all the code changes are made where required within the database.
Automatic migration creation: South can see what’s changed in your models.py file and automatically write migrations that match your changes.
Is it possible to alter the schema of a mysql table by simply providing the new schema and having the database figure out how to migrate? For example, let's say I have a table with two columns: id, name. I want to modify this table by adding a new column: title. I know that I can issue the command ALTER TABLE tbl ADD COLUMN title. Is there a way I can just provide the complete schema and have mysql figure out that it needs to add a title column?
I hope that makes sense what I am asking.
MySQL can't do this by itself, but I found a blog that says MySQL Workbench can do it.
No - It cannot do that just giving your database a new schema. It would have to go about and second guess what to do with the data. (Also what happens to triggers, stored procedures ...)
You have a couple of choices
Modify each table and decide what needs to be done
Export the data, create a new database and then figure out how to import it into the new regime.
Is it possible to use the create table query (create table t2 like t1) to get the query instead the actual table? Kind of like duplicating some management consoles Send To editor feature?
I think you're looking for SHOW CREATE TABLE tablename;
'tablename' can be the table within the current database, if you've selected one, or you can qualify it as in 'dbname.tablename'. The output can be used to create the table again. In fact, I sometimes use it in scripts that need to either use an existing table or create it if it doesn't exist. First I'll get the table creation info as shown above, and then I'll use something like"
CREATE TABLE IF NOT EXISTS tablename .........
Where '..........' is all the goodies that SHOW CREATE TABLE tablename gives me.
If you're interested in a good tutorial on MySQL (the database), as well as SQL (the language), you might have a look at the O'Reilly book, Learning MySQL. It's pretty good.