How to Create User-Defined Database in SQL Programming

February 24, 2014 , , , 0 Comments

In addition to system databases, the SQL Server also contains user-defined databases where the users store and manages their information. When the users create a database, it is stored as a set of files on the hard disk of the computer.

To create a user-defined database, you can use the CREATE DATABASE statement. The syntax of the CREATE DATABASE statement is:

CREATE DATABASE database_name
[ON [ PRIMARY ] [ < filespec >]]
[LOG ON [ < filespec >}}
< filespec > : : =
( [ NAME = logical_file_name , ]
FILENAME = ‘os_file_name’
[ , SIZE = size]
[ , MAXSIZE = { max_size | UNLIMITED } ]
[ , FILEGROWTH = growth_increment ] ) [ ,…n ]

  • Database_name is the name of the new database.
  • ON specifies the disk files used to store the data portion of the database (data files).
  • PRIMARY specifies the associated <filespec> list that defines file in the primary filegroup.
  • LOG ON specifies the disk files used to store the log files.
  • NAME=logical_file_name specifies the logical name for the file.
  • FILENAME=os_file_name specifies the operating-system file name for the file.
  • SIZE=size specifies the initial size of the file defined in the <filespec> list.
  • MAXSIZE=max_size specifies the maximum size to which the file defined in the <filespec> list can grow.
  • FILEGROWTH=growth_increment specifies the growth increment of the file defined in the <filespec> list. The FILEGROWTH setting for a file cannot exceed the MAXSIZE setting.

To create a database, you must be a member of the dbcreator server role. In addition, you must have the CREATE DATABASE, CREATE ANY DATABASE, or ALTER ANY DATABASE permissions.

The following SQL query creates a database named Personnel to store the data related to all the employees:
The preceding statement creates a database named Personnel in the C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data folder. The data file name of the database is Personnel.mdf and the log file name is Personnel_Log.ldf.
You can also create a database in the Object Explorer window by right-clicking the Databases folder and selecting the New Database option from the shortcut menu. When the database is created, the user, who creates the database, automatically becomes the owner of the database. The owner of the database is called dbo.
After a database has been created, you may also need to see the details of the database. For this purpose, you can use he sp_helpdb command. The synbtax of the sp_helpdb command is:
sp_helpdb [database name]

Store information in database
Rename or Drop Database

Rhett Butler

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google