My SQL資料定義語言 DDL

2021-07-23 22:14:44 字數 2567 閱讀 5638

create  [if

notexists] db_name

[create_specification [, create_specification] ...]

create_specification:

[default] character

set charset_name

| [default] collate collation_name

create database用於建立資料庫,並進行命名。

alter  [db_name]

alter_specification [, alter_specification] ...

alter_specification:

[default] character set charset_name

| [default] collate collation_name

alter database用於更改資料庫的全域性特性。這些特性儲存在資料庫目錄中的db.opt檔案中。

選項說明:

character set子句用於更改預設的資料庫字符集。collate子句用於更改預設的資料庫整序。

資料庫名稱可以忽略,此時,語句對應於預設資料庫。也可以使用alter schema。

create [temporary] table [if not exists] tbl_name

[(create_definition,...)]

[table_options] [select_statement]

create_definition:

column_definition

| [constraint [symbol]] primary key [index_type] (index_col_name,...)

| key [index_name] [index_type] (index_col_name,...)

| index [index_name] [index_type] (index_col_name,...)

create table用於建立帶給定名稱的表。

預設的情況是,表被建立到當前的資料庫中。如果表已存在,或者如果沒有當前資料庫,或者如果資料庫不存在,則會出現錯誤。

alter [ignore] table tbl_name

alter_specification [, alter_specification] ...

alter_specification:

add [column] column_definition [first | after col_name ]

| add [column] (column_definition,...)

| add index [index_name] [index_type] (index_col_name,...)

| add [constraint [symbol]]

primary key [index_type] (index_col_name,...)

| change [column] old_col_name column_definition

[first|after col_name]

| modify [column] column_definition [first | after col_name]

| drop [column] col_name

| drop primary key

alter table用於更改原有表的結構。例如,可以增加或刪減列,建立或取消索引,更改原有列的型別,或重新命名列或表。還可以更改表的評注和表的型別。

要把乙個integer列的名稱從a變更到b,您需要如下操作:

mysql> alter table t1 change a b integer;
如果您想要更改列的型別而不是名稱, change語法仍然要求舊的和新的列名稱,即使舊的和新的列名稱是一樣的。例如:

mysql> alter table t1 change b b bigint not null;
您也可以使用modify來改變列的型別,此時不需要重新命名:

mysql> alter table t1 modify b bigint not null;
索引是一種特殊的檔案(innodb資料表上的索引是表空間的乙個組成部分),它們包含著對資料表裡所有記錄的引用指標。更通俗的說,資料庫索引好比是一本書前面的目錄,能加快資料庫的查詢速度。

create [unique|fulltext|spatial] index index_name

[using index_type]

on tbl_name (index_col_name,...)

index_col_name:

col_name [(length)] [asc | desc]

MySQL 資料定義語言(DDL)

mysql 資料定義語言 ddl 一 create命令 1 建立資料庫 create database if not exists 資料庫名 例 create database aa 2 建立資料表 create table if not exists 表名 欄位名1 列型別 屬性 索引 注釋 欄位...

MySQL之資料定義語言(DDL)

資料定義語言 用來建立資料庫,資料庫物件和定義列的命令。mysql uroot p輸入密碼後進入mysql,exit或quit退出 檢視所有資料庫show databases 建立資料庫create database 資料庫名 刪除資料庫drop database 資料庫名 切換進入某個資料庫use...

MySQL之DDL(資料定義語言)

主要用於資料庫和表的管理和操作 create database ifnot exists studbdrop databses if exists studb 建立表 create table if not exists 表名 欄位名 字段型別 字段約束 欄位名 字段型別 字段約束 欄位名 字段型別...