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.