SQL CREATE DATABASE Statement

SQL CREATE DATABASE - . . , , , . , , , .

, . , SQL. CREATE DATABASE Animals CREATE DATABASE Electrical. . , , - .

, Animals. , Animals, . , . Electrical , : , , .

DDL, DML, DCL

SQL - .

SQL Databases




, . , , . :





  • DDL - ;
  • DML - ;
  • DCL - , .

: SELECT, INSERT, UPDATE, DELETE. , GRANT, REVOKE, DENY.

:

  • CREATE;
  • ALTER;
  • DROP.

Create , . SQL - SQL CREATE DATABASE base_name.

Alter . , , , . DROP .

CREATE

CREATE - , . . :

  • ;
  • ;
  • .

:

CREATE TABLE table_name
      
      



table_name , . () .

CREATE TABLE table_name ( arg )
      
      



,

Table creation




Planets , ID OpeningYear, . PlanetName - 10 . Radius SunSeason . HavingRings true false.





CREATE VIEW . , , . , . , .

SQL - CREATE DATABASE base_name.

CREATE DATABASE

SQL Server- CREATE DATABASE. . . SQL. POSTGRESQL CREATE DATABASE . :

Operator Arguments for Creating a Database




CREATE DATABASE db_name. , , .

SQL CREATE DATABASE User Password. .

SQL CREATE DATABASE Character set utf8 .

Database_name

MS SQL CREATE DATABASE . . .

  1. A Z a z.
  2. , @, $, #.
  3. .
  4. .

SQL CREATE DATABASE t.

120 . , database_name. , 123 , 128 .

CONTAINMENT

SQL Server 2012. . :

  • NONE;
  • PARTIAL.

, , . . SQL , . NONE. , .

CONTAINMENT = PARTIAL, .

ON COLLATE

On , . <filespec>.

PRIMARY <filespec> . , CREATE DATABASE.

LOG ON <filegroup> , <filegroup> .

COLLATE collation_name . , SQL Server. FOR ATTACH FOR ATTACH_REBUILD_LOG. .

WITH options:

  1. <filestream_options> , : OFF ( ), READ_ONLY ( ), FULL ( ). DIRECTORY_NAME = <directory_name>. , .
  2. DEFAULT_FULLTEXT_LANGUAGE , . , . . .
  3. DEFAULT_LANGUAGE . 0-32.
  4. TRANSFORM_NOISE_WORDS -, - , 0 . 0, - , 0 . 1, , .
  5. TWO_DIGIT_YEAR_CUTOFF 1753 9999. SQL 1950-2049, 30, 2030, 50 1950.
  6. DB_CHAINING , .
  7. TRUSTWORTHY . off, .

<filespec>

. Logical_file_name , . , .

FILENAME = { 'os_file_name' | 'filestream_path' }
      
      



filename . Size , maxsize - .

. SQL CREATE DATABASE:

Database Operators




SQL. mydb. USE mydb.

USE db1;

SELECT COUNT(*) FROM mytable; # selects from db1.mytable

USE db2;

SELECT COUNT(*) FROM mytable;
      
      



db1 , db2.

mytable PRIMARY KEY 20 name. , AS/SELECT/FROM.

CREATE TABLE new_table_name AS

SELECT column1, column2,...

FROM existing_table_name

WHERE ....;
      
      



, new_table_name, , SELECT. column1 column2. , .

FROM existing_table_name
      
      



, SHOW DATABASES.

, INSERT INTO, INTO . : LOW_PRIORITY, DELAYED, HIGH_PRIORITY IGNORE.

, VALUES, ( 1, Will ), ( 2, Marry ) ( 3, Dean ). VALUES VALUE. SET, , . col = expr.

SELECT . , id, name mytable, id 1. , UPDATE, name Willy.

, . , DROP DATABASE _. , .

Creating a database with files and a log




Sales, saledat.mdf , PRIMARY. SIZE MAXSIZE MB KB, . SIZE - 10 MB, MAXSIZE - 15 MB.

, , . , :

Multiple Data Files




, 200 .

, FILEGROUP group_name.

File grouping




SalesGroup1, SalesGroup2. , 50 . Sales_log. - SPri1_dat SPri2_dat , 10 , 50 .

,

FileStreamDB. , FILESTREAM. .

USE master;

GO

-- Get the SQL Server data path.

DECLARE @data_path nvarchar(256);

SET @data_path = (SELECT SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1)

FROM master.sys.master_files

WHERE database_id = 1 AND file_id = 1);

-- Execute the CREATE DATABASE statement.

EXECUTE ('CREATE DATABASE FileStreamDB

ON PRIMARY

(

NAME = FileStreamDB_data

,FILENAME = ''' + @data_path + 'FileStreamDB_data.mdf''

,SIZE = 10MB

,MAXSIZE = 50MB

,FILEGROWTH = 15%

),

FILEGROUP FileStreamPhotos CONTAINS FILESTREAM DEFAULT

(

NAME = FSPhotos

,FILENAME = ''C:\MyFSfolder\Photos''

-- SIZE and FILEGROWTH should not be specified here.

-- If they are specified an error will be raised.

, MAXSIZE = 5000 MB

),

(

NAME = FSPhotos2

, FILENAME = ''D:\MyFSfolder\Photos''

, MAXSIZE = 10000 MB

),

FILEGROUP FileStreamResumes CONTAINS FILESTREAM

(

NAME = FileStreamResumes

,FILENAME = ''C:\MyFSfolder\Resumes''

)

LOG ON

(

NAME = FileStream_log

,FILENAME = ''' + @data_path + 'FileStreamDB_log.ldf''

,SIZE = 5MB

,MAXSIZE = 25MB

,FILEGROWTH = 5MB

)'

);

GO
      
      



@data_path - , 256 .

SET @data_path = (SELECT SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1)
      
      



SUBSTRING , . , - . CHARINDEX, . physical_name, master.mdf.

EXECUTE . FileStreamDB_data, FileStreamPhotos FileStreamResumes.

FileStreamPhotos FILESTREAM data: FSPhotos FSPhotos2. FileStreamResumes FILESTREAM FileStreamResumes. FileStream_log 25 .




All Articles