数据定义语句(DDL,Data definitionlanguage)

    xiaoxiao2026-08-17  13

    数据定义语句(DDL,Data definitionlanguage)

    create database

    create database [if not exists] db_name

    该语句用于创建数据库。

    其中若添加 if notexists,则该语句会在创建数据库时检查是否已经存在该数据库,若存在则会报警告提示已经存在不能重复创建;若不存在,则成功建立该数据库。

    通过,use db_name 来使用数据库

    create table

    create table [ifnot exists] tbl_name [(create_definition)] 或者 create table tbl_name like old_tbl_name; 其中, create_definition column_definition | primary key (column_name) |key [index_name][index_type] (column_name) |index [index_name][index_type] (column_name) … column_definition: column_name type [not null | null ] [defaultdefault_name] [auto_increment][unique key] [ primary key]

    这里罗列常用的创建数据表的语句。

    例如,

    createtable books( books_id int auto_increment primarykey, author_name varchar(20) not null, pub_datetime datetime, index idx_name (author_name(20)) )engine=innodb;

    该语句创建了名为books的数据表,其中books_id自动增长并且是主键,author_name是变长字符串并且不能为空,pub_datetime的类型是datetime,并且添加了索引,该索引的列是author_name的前20个字符。搜索引擎是innodb。

    另一种创建数据表的方式,可以通过create table tbl_name likeold_tbl_name 复制表结构。

    可以通过desctbl_name;查看表结构

    可以通过 showceate table tbl_name\G 查看数据表

    alter table

    alter tabletbl_name alter_specification alter_specification: add [column] column_definition | add index [index_name] [index_type] (index_column) | change [column] old_column_namenew_column_definition | modify [column] column_difinition | drop [column] column_name | drop primary key | drop index index_name …

    ALTER TABLE 用于该表原表的表结构。例如,增加或者删除列,创建或取消索引,更改原有列的类型,或者重新命名原有的列。

    create index

    create [unique|fulltext|spatial]index index_name [using index_type] on tbl_name(index_column_name) index_column_name col_name [(length)] [asc|desc]

    create index 被映射到一个alter table,用于创建索引。

    例如,

    CREATE TABLE lookup (id INT) ENGINE = MEMORY; CREATE INDEX id_index USING BTREE ON lookup (id);

    drop table

    drop table [if exists] tbl_name [,tbl_name]

    drop table 用于取消一个或多个表。

    drop index

    drop index index_nameon tbl_name;

    drop index 用于从表tbl_name中取消名称为index_name的索引。

    drop database

    drop [database] [if exists] db_name

    drop database 用于取消数据库中所有表和取消数据库。

    rename table

    rename tabletbl_name to new_tbl_name [,tbl2_name to new_tbl2_name]

    本句用于一个或多个表经行重命名。

    转载请注明原文地址: https://ju.6miu.com/read-1311277.html
    最新回复(0)