SQL 基础
SQL 简介
SQL 入门
SQL 语法
SQL 创建数据库
SQL 创建表
SQL Constraints
SQL Insert
SQL Select
SQL Where
SQL AND & OR
SQL IN & Between
SQL Order By
SQL Top/Limit
SQL Distinct
SQL Update
SQL Delete
SQL Truncate Table
SQL Drop
SQL 连接
SQL Joining Tables
SQL Inner Join
SQL Left Join
SQL Right Join
SQL Full Join
SQL Cross Join
高级 SQL
SQL Union
SQL Like
SQL Alter Table
SQL Aliases
SQL Group By
SQL Having
SQL Create View
SQL Create Index
SQL Dates and Times
SQL Cloning Tables
SQL 临时表
SQL 子查询
SQL 注入
SQL 参考
SQL 数据类型
MySQL 数据类型
SQL Server 数据类型
SQL 方法
SQL 创建数据库 - SQL基础教程 - 笔下光年
网站首页
SQL 创建数据库
In this tutorial you will learn how to create database in a relational database management system like, MySQL, SQL Server, etc. using SQL. ## Creating a Database Before doing anything with the data we must need to create a database first. We're assuming that you already have a MySQL, or SQL Server available for your use, as well as you've all the necessary privileges, if not please check out the [getting started guide](http://www.bixiaguangnian.com/manual/sql/3387.html "getting started guide"). The SQL `CREATE DATABASE` statement is used to create a database. ## Syntax The basic syntax for creating a database can be given with: ```sql CREATE DATABASE database_name; ``` The following SQL statement creates a database named demo: ```sql CREATE DATABASE demo; ``` Creating a database does not select it for use. So, before moving further we must need to select the target database with the `USE` statement. For example, the `USE demo`; command sets the demo database as target database for all future commands. <div class="callout callout-info mb-3">Note: In Unix, the database and table names are case-sensitive, so you must always refer to your database as demo, not as Demo, DEMO, or something else. But, SQL keywords are case-insensitive, like CREATE DATABASE is same as create database.</div> ## Creating Database in MySQL Let's create a database in MySQL using the command-line tool. ### Step 1: Invoke the MySQL command-line tool To invoke the MySQL command line, we've to log in to the MySQL server first. To log in as `root` user, type the following command in terminal and press enter. You will be asked for your password. Enter your password and press enter, if it is correct the `mysql>` prompt will appear, via which you will be able to issue SQL statements and view the results. ```shell shell> mysql -u root -p ``` ### Step 2: Creating a MySQL Database Now, execute the following command to create the database named demo. ```shell mysql> CREATE DATABASE demo; ``` If the database created successfully you'll see the output something like this: ``` Query OK, 1 row affected (0.03 sec) ``` If you try to create a database that is already exists you'll get an error message. To avoid this in MySQL you can use an optional clause `IF NOT EXISTS` as follow: ```shell mysql> CREATE DATABASE IF NOT EXISTS demo; ``` ### Step 3: Selecting the Database Type the following command and press enter. You will see the output "Database changed". Now our demo database is selected as default database for all future operations. ```shell mysql> USE demo; ``` <div class="callout callout-success mb-3">Tip: If you want to see a list of existing databases on the MySQL server, you can execute the statement SHOW DATABASES; on the command line.</div>
上一篇:
SQL 语法
下一篇:
SQL 创建表