SQL Server creates a database

Source: Internet
Author: User

Today, we will spend some time learning about SQL Server databases, including how to create databases, create tables, SQL Server data types, data integrity, and design databases, SQL Server security models and T-SQL statements. I hope you will have some gains after reading this article. Good. First, let's take a look at how to create a database. The database we created and the table created in the next article will be used all the time. I hope you will pay attention to it!

 

The syntax for the T-SQL to create a database is as follows:

Create DatabaseDatabase Name

On [primary]

(

<Data file parameters> [,... N] [<file group parameters>]

)

[Log On]

(

<Log File parameters> [,... N]

)

 

[] Represents optional parameters, the T-SQL syntax often needs to query the help of SQL Server, and these symbols are often seen in help. We all know that the simplest statement for creating a database is the name of the create database database.,That is, some options with default values are omitted! Let's useT-SQLCreate a complete database:

 

Use master -- set the current database as the master to access the sysdatabases table

Go

If exists (select * From sysdatabases where name = 'studb ')

Drop database studb

 

Create Database studb

On Primary -- by default, it belongs to the primary main file group, which can be omitted

(

Name = 'db _ data', -- Logical name of the master data file

Filename = 'e:/project/studb_data.mdf ', -- physical name of the master data file

Size = 5 MB, -- initial size of the master data file

Maxsize = 100 MB, -- maximum growth of the master data file

Filegrowth = 15% -- growth rate of the master data file

)

Log On

(

Name = 'db _ log ',

Filename = 'e:/project/studb_log.ldf ',

Size = 2 MB,

Filegrowth = 1 MB

)

Go

 

Before creating a database, we will first check whether a studb database exists. If so, we will delete it and re-create it. Where should the data inventory be placed? Where can we detect it? Because the newly created database is registered in the sysdatabases table of the system database master, you only need to check the name column of the sysdatabases table of the master database!

 

 

Note the usage of the exists (query statement) Check statement. If the query statement returns more than one record, that is, if there is a record that meets the conditions, true is returned; otherwise, false is returned. In this way, the database is successfully created.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.