数据定义
表基础
使用CRATE TABLE命令创建一个表。你必须至少为一个表指定一个名称和它的列名。查看数据类型来获取支持的数据类型的更详细的信息。
我们来创建一个简单的带两个列的的表,其中一个列为integer,一个为string:
cr> create table my_table (
... first_column integer,
... second_column string
... );
CREATE OK, 1 row affected (... sec)
可以使用DROP TABLE的命令来删除一个表:
cr> drop table my_table;
DROP OK, 1 row affected (... sec)
DROP TABLE命令有一个可选择的语句IF EXISTS,这将会防止生成一个错误如果指定的表不存在:
cr> drop table if exists my_table;
DROP OK, 0 rows affected (... sec)
Schemas(模式)
表可以在不同的schema中创建。这是在创建表时隐式创建的而且不会显式创建。如果一个schema不存在,它将会被创建:
cr> create table my_schema.my_table (
... pk int primary key,
... label string,
... position geo_point
... );
CREATE OK, 1 row affected (... sec)
cr> select table_schema, table_name from information_schema.tables
... where table_name='my_table';
+--------------+------------+
| table_schema | table_name |
+--------------+------------+
| my_schema | my_table |
+--------------+------------+
SELECT 1 row in set (... sec)
以下的schema的名字是被限制的而且不能被使用:
blob
information_schema
sys
注意
Schema是表基本的命名空间。没有访问控制。每个人都能查看和操作每个schema中的表。
只要存在有相同schema的表,用户创建的schema就存在。若果那个schema的最后一张表被删除,这个schema也就不存在了。
cr> drop table my_schema.my_table ;
DROP OK, 1 row affected (... sec)
每一个表创建的时候没有一个隐式的schema的名字,将会创建在doc这个schema中:
cr> create table my_doc_table (
... a_column byte,
... another_one geo_point
... );
CREATE OK, 1 row affected (... sec)
cr> select table_schema, table_name from information_schema.tables
... where table_name='my_doc_table';
+--------------+--------------+
| table_schema | table_name |
+--------------+--------------+
| doc | my_doc_table |
+--------------+--------------+
SELECT 1 row in set (... sec)
Naming restrictions(命名限制)
表,schema和列标识将不会和保留关键字有相同的名字。请查看Lexical Structure这一章获取关于命名的详细信息。
总之,表和schema的名称的字符和长度被严格限制。如下:
不能包含如下的字符: \ / * ? " < > |
, # 不能包含大写的字母
不能以下划线开头: _
当使用utf-8编码是不能超过255个字节大小(此限制适用于可选的模式限制的表名)
列名称受字符方面的限制。如下:
不能包含以下字符: [ ' ] .
不能以下划线开头: _