Just wondered if there was a secret to do something like Database.Security.Users like AdventureWorks DB is setup. Seems no matter what I do to try to setup "Security.Users", I always get the dbo in front of it and have a hell of a time in C# accessing the info. Am I doing something wrong?
Are you trying to create an object called Security.Users with a dot? (as opposed to Users in the Security schema?) That's probably best avoided as you're seeing, but if you are then the best way to quote the name is probably in square brackets, i.e. [Security.Users].
dbo is the default database schema name. Unless you've configured a different default schema for your users etc. you can usually just ignore it, although it's still needed if you're referencing another database by name.
you first need to create a schema and make that schema the default schema for that user. Examples and more info can be found here: http://msdn.microsoft.com/en-us/library/ms190387.aspx
If you are using the Wizard to create this, you will always get it. Write the SQL statements and you should be fine.
Related
In my project I'm using Speedment for ORM. Of course I want my code properly tested. So I decided to create an identical copy of my default database schema which I wanted to use for unit testing. In this case the name of the original schema is "project" and the name of the copy is "test_project"
My problem is that I don't know how to properly address the other database schema.
I know that, upon establishing a connection, I can use the method withSchema("test_project") to tell speedment which schema to use.
This works just fine as long as I don't have any columns identifiers in my query.
So this works:
List <User> users = userManager.stream().collect(Collectors.toList());
whereas this doesn't:
List <User> users = userManager.stream().filter(User.UID.equal(id)).collect(Collectors.toList());
It's telling me this: Unknown column 'project.User.uid' in 'where clause
I don't really understand what's going on there. (Note: I'm quite new to Speedment).
My question is: How can I access my other schema with all its rows properly addressed to it?
This was a bug in Speedment. Changing schema withSchema("test_project") is the correct way. This will be fixed in Speedment version 3.0.23.
Basically i need to know if there is any way to get the current schema name using Mybatis.
The DB engine I'm using is MySQL
The most easy way, for which you don't even need to do anything MyBatis-specific, would simply be a query:
SELECT DATABASE();
This should, according to the documentation, return the current database.
Alternatively, you should be able to get the Configuration from your SqlSession via getConfiguration() and get it from there somewhere, perhaps from the environment which allows you access to the DataSource, but you will probably need some database-specific code there.
We are asking users to not create objects in their own schema (e.g. bill.Table rather than using dbo.Table). Ultimately, we want to force this rather than "ask nicely". I've Googled around and found nothing obvious. Can someone point me in the right direction?
Thanks
Clay
When adding users to your database, the CREATE USER command has an optional parameter, viz WITH DEFAULT_SCHEMA
The option to set a default schema for the user is also available in ManagementStudio under the database's Security / Users tree.
I'm not sure that you can specifically allow users access to create objects like tables in a database while at the same time preventing them CREATE SCHEMA access - the folk at dba.stackexchange might know.
I would like to implement a custom database initialization strategy so that I can:
generate the database if not exists
if model change create only new tables
if model change create only new fields without dropping the table and losing the data.
Thanks in advance
You need to implement IDatabaseInitializer interface.
Eg
public class MyInitializer : IDatabaseInitializer<MyDbContext>
{
public void InitializeDatabase(MyDbContext context)
{
//your logic here
}
}
And then set your initializer at your application startup
Database.SetInitializer<ProductCatalog>(new MyInitializer());
Here's an example
You will have to manually execute commands to alter the database.
context.ObjectContext.ExecuteStoreCommand("ALTER TABLE dbo.MyTable ADD NewColumn VARCHAR(20) NULL");
You can use a tool like SQL Compare to script changes.
There is a reason why this doesn't exist yet. It is very complex and moreover IDatabaseInitializer interface is not very prepared for such that (there is no way to make such initialization database agnostic). Your question is "too broad" to be answered to your satisfaction. With your reaction to #Eranga's correct answer you simply expect that somebody will tell you step by step how to do that but we will not - that would mean we will write the initializer for you.
What you need to do what you want?
You must have very good knowledge of SQL Server. You must know how does SQL server store information about database, tables, columns and relations = you must understand sys views and you must know how to query them to get data about current database structure.
You must have very good knowledge of EF. You must know how does EF store mapping information. You must be able to explore metadata get information about expected tables, columns and relations.
Once you have old database description and new database description you must be able to write a code which will correctly explore changes and create SQL DDL commands for changing your database. Even this look like the simplest part of the whole process this is actually the hardest one because there are many other internal rules in SQL server which cannot be violated by your commands. Sometimes you really need to drop table to make your changes and if you don't want to lose data you must first push them to temporary table and after recreating table you must push them back. Sometimes you are doing changes in constraints which can require temporarily turning constrains off, etc. There is good reason why tools which do this on SQL level (comparing two databases) are probably all commercial.
Even ADO.NET team doesn't implemented this and they will not implement it in the future. Instead they are working on something called migrations.
Edit:
That is true that ObjectContext can return you script for database creation - that is exactly what default initializers are using. But how it could help you? Are you going to parse that script to see what changed? Are you going to execute that script in another connection to use the same code as for current database to see its structure?
Yes you can create a new database, move data from the old database to a new one, delete the old one and rename a new one but that is the most stupid solution you can ever imagine and no database administrator will ever allow that. Even this solution still requires analysis of changes to create correct data transfer scripts.
Automatic upgrade is a wrong way. You should always prepare upgrade script manually with help of some tools, test it and after that execute it manually or as part of some installation script / package. You must also backup your database before you are going to do any changes.
The best way to achieve this is probably with migrations:
http://nuget.org/List/Packages/EntityFramework.SqlMigrations
Good blog posts here and here.
Is there a way to allocate a default database to a specific user in MySQL so they don't need to specify the database name while making a query?
I think you need to revisit some concepts - as Lmwangi points out if you are connecting with mysql client then my.cnf can set it.
However, your use of the word query suggests that you are talking about connecting from some programming environment - in this case you will always need a connection object. To create connection object and in this case having default database to connect to will lead to no improvement (in terms of speed or simplicity). Efficiently managing your connection(s) might be interesting for you - but for this you should let us know exactly what is your environment.
If you use a database schema you don't need to specify the database name every time, but you need to select the database name.
The best thing to do would be to use a MySQL trigger on the connection. However, MySQL only accepts triggers for updates, deletes and inserts. A quick Google search yielded an interesting stored procedure alternative. Please
see MySQL Logon trigger.
When you assign the permissions to every user group, you can also specify, at the same file, several things for that group, for example the database that users group need to use.
You can do this with a specification file, depending on the language you are working with, as a simple variable. Later, you only have to look for that variable to know which database you need to work with. But, I repeat, it depends on the language. The specification file can be an XML, phpspecs file, or anything like this.