Create Databasee

Write a script that will create a database. Include the creation of a user named user1 with a password of pword1. Save the script as create_database.sql.
lg5050Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve WalesSenior Database AdministratorCommented:
Sorry, this sounds a lot like homework in the way you've phrased the question.

We can't help you with direct answers to homework questions.

See: http://support.experts-exchange.com/customer/portal/articles/1435136-can-i-get-help-with-my-homework-question-?b_id=44

What have you researched or done so far ?
lg5050Author Commented:
so i just do:

CREATE Database Data_1;

CREATE LOGIN user1
WITH PASSWORD = 'pword1';
GO

-- Creates a database user for the login created above.
CREATE USER user1 FOR LOGIN user1;
GO
Steve WalesSenior Database AdministratorCommented:
That pretty much covers it.

SQL Server has extensive documentation available online.

You could add extra parameters to CREATE DATABASE that would specify where you want the datafile and logfile put as well as size and autogrowth options.  If you don't specify these during database creation, files go to default locations.

Documentation links for your perusal:

CREATE DATABASE: https://msdn.microsoft.com/en-us/library/ms176061.aspx
CREATE LOGIN: https://msdn.microsoft.com/library/ms189751.aspx
CREATE USER: https://msdn.microsoft.com/library/ms173463.aspx

The user you've created won't be able to do too much (you haven't granted any permissions) but that doesn't seem to be a requirement.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Jim HornMicrosoft SQL Server Data DudeCommented:
lg5050  -  After reading Steve's link to EE's homework policy, a good read for you would be my article called Top 10 Ways to Ask Better Questions, specifically numbers seven (Homework) and six (Paint My House).
Scott PletcherSenior DBACommented:
Not quite there yet.  You should check to see if the login and/or userid already exist before you CREATE them.  Whether or not you change the password on the login if it already exists is a different q.  But naturally if either did already exist you would get an error on CREATE and that might cause the rest of the code to be skipped.
Steve WalesSenior Database AdministratorCommented:
Actually there may be a couple of other things in there to consider as well as what Scott points out.

Create user works in the current database context which by default is master.

After creating the database and the login, you would need to change your context to the database  you just created:

use Data_1
GO

After doing that, THEN create the user (or else the user would be in the wrong database).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.