MySQL DDL DML DCL
mysql 5.6之前数据库alter table 原理:
ALTER TABLE运行时会对原表进行临时复制,在副本上进行更改,然后删除原表,再对新表进行重命名。
在执行ALTER TABLE时,其它用户可以阅读原表,但是对表的更新和修改的操作将被延迟,直到新表生成为止。
新表生成后,这些更新和修改信息会自动转移到新表上。
SQL语言分类:(以下整理来自互联网)
#--------------------------------------------------------------------------------------------
DML(data manipulation language):
它们是SELECT、UPDATE、INSERT、DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据进行操作的语言
DML的概述
DML(Data Manipulation Language 数据操控语言)用于操作数据库对象中包含的数据,也就是说操作的单位是记录。
DML的主要语句(操作)
Insert语句:向数据表张插入一条记录。
Delete语句:删除数据表中的一条或多条记录,也可以删除数据表中的所有记录,但是,它的操作对象仍是记录。
Update语句:用于修改已存在表中的记录的内容。
DML的操作对象——记录
当我们对记录进行Insert、Delete和Update操作的时候,一定要注意,一定要清楚DDL对其的一些操作。
DDL(data definition language):
DDL(Data Definition Language 数据定义语言)用于操作对象和对象的属性,这种对象包括数据库本身,
以及数据库对象,像:表、视图等等,DDL对这些对象和属性的管理和定义具体表现在Create、Drop和Alter上。
特别注意:DDL操作的“对象”的概念,”对象“包括对象及对象的属性,而且对象最小也比记录大个层次。
以表举例:Create创建数据表,Alter可以更改该表的字段,Drop可以删除这个表,从这里我们可以看到,
DDL所站的高度,他不会对具体的数据进行操作。
DDL的主要语句(操作)
Create语句:可以创建数据库和数据库的一些对象。
Drop语句:可以删除数据表、索引、触发程序、条件约束以及数据表的权限等。
Alter语句:修改数据表定义及属性。
主要操作对象为表。
DCL(Data Control Language):
是数据库控制功能。是用来设置或更改数据库用户或角色权限的语句,包括(grant,deny,revoke等)语句。
在默认状态下,只有sysadmin,dbcreator,db_owner或db_securityadmin等人员才有权力执行DCL
DCL的概述
DCL(Data Control Language 数据控制语句)的操作是数据库对象的权限,这些操作的确定使数据更加的安全。
DCL的主要语句(操作)
Grant语句:允许对象的创建者给某用户或某组或所有用户(PUBLIC)某些特定的权限。
Revoke语句:可以废除某用户或某组或所有用户访问权限
DCL的操作对象(用户)
此时的用户指的是数据库用户。
#--------------------------------------------------------------------------------------------
下面是从官方文档拷贝的关于Online ddl对于当前ddl操作的支持:
参考文档:http://dev.mysql.com/doc/refman/5.6/en/innodb-create-index-overview.html
Operation | In-Place? | Copies Table? | Allows Concurrent DML? | Allows Concurrent Query? | Notes |
---|---|---|---|---|---|
CREATE INDEX , ADD INDEX |
Yes* | No* | Yes | Yes | Some restrictions for FULLTEXT index; see next row. Currently, the operation is not in-place (that is, it copies the table) if the same index being created was also dropped by an earlier clause in the same ALTER TABLE statement. |
ADD FULLTEXT INDEX |
Yes | No* | No | Yes | Creating the first FULLTEXT index for a table involves a table copy, unless there is a user-suppliedFTS_DOC_ID column. SubsequentFULLTEXT indexes on the same table can be created in-place. |
DROP INDEX |
Yes | No | Yes | Yes | Modifies .frm file only, not the data file. |
OPTIMIZE TABLE |
Yes | Yes | Yes | Yes | Uses ALGORITHM=INPLACE as of MySQL 5.6.17. ALGORITHM=COPY is used if old_alter_table=1 ormysqld --skip-new option is enabled. OPTIMIZE TABLE using online DDL (ALGORITHM=INPLACE ) is not supported for tables with FULLTEXT indexes. |
Set default value for a column | Yes | No | Yes | Yes | Modifies .frm file only, not the data file. |
Change auto-incrementvalue for a column | Yes | No | Yes | Yes | Modifies a value stored in memory, not the data file. |
Add a foreign key constraint | Yes* | No* | Yes | Yes | To avoid copying the table, disableforeign_key_checks during constraint creation. |
Drop a foreign key constraint | Yes | No | Yes | Yes | The foreign_key_checks option can be enabled or disabled. |
Rename a column | Yes* | No* | Yes* | Yes | To allow concurrent DML, keep the same data type and only change the column name. |
Add a column | Yes | Yes | Yes* | Yes | Concurrent DML is not allowed when adding an auto-incrementcolumn. AlthoughALGORITHM=INPLACE is allowed, the data is reorganized substantially, so it is still an expensive operation. |
Drop a column | Yes | Yes | Yes | Yes | Although ALGORITHM=INPLACE is allowed, the data is reorganized substantially, so it is still an expensive operation. |
Reorder columns | Yes | Yes | Yes | Yes | Although ALGORITHM=INPLACE is allowed, the data is reorganized substantially, so it is still an expensive operation. |
Change ROW_FORMAT property |
Yes | Yes | Yes | Yes | Although ALGORITHM=INPLACE is allowed, the data is reorganized substantially, so it is still an expensive operation. |
ChangeKEY_BLOCK_SIZE property |
Yes | Yes | Yes | Yes | Although ALGORITHM=INPLACE is allowed, the data is reorganized substantially, so it is still an expensive operation. |
Make column NULL |
Yes | Yes | Yes | Yes | Although ALGORITHM=INPLACE is allowed, the data is reorganized substantially, so it is still an expensive operation. |
Make column NOT NULL |
Yes* | Yes | Yes | Yes | When SQL_MODE includesstrict_all_tables orstrict_all_tables , the operation fails if the column contains any nulls. AlthoughALGORITHM=INPLACE is allowed, the data is reorganized substantially, so it is still an expensive operation. |
Change data type of column | No | Yes | No | Yes | |
Add primary key | Yes* | Yes | Yes | Yes | Although ALGORITHM=INPLACE is allowed, the data is reorganized substantially, so it is still an expensive operation.ALGORITHM=INPLACE is not allowed under certain conditions if columns have to be converted toNOT NULL . See Example 14.10, “Creating and Dropping the Primary Key”. |
Drop primary key and add another | Yes | Yes | Yes | Yes | ALGORITHM=INPLACE is only allowed when you add a new primary key in the same ALTER TABLE ; the data is reorganized substantially, so it is still an expensive operation. |
Drop primary key | No | Yes | No | Yes | Restrictions apply when you drop a primary key primary key without adding a new one in the sameALTER TABLE statement. |
Convert character set | No | Yes | No | Yes | Rebuilds the table if the new character encoding is different. |
Specify character set | No | Yes | No | Yes | Rebuilds the table if the new character encoding is different. |
Rebuild with FORCE option |
Yes | Yes | Yes | Yes | Uses ALGORITHM=INPLACE as of MySQL 5.6.17. ALGORITHM=COPY is used if old_alter_table=1 ormysqld --skip-new option is enabled. Table rebuild using online DDL (ALGORITHM=INPLACE ) is not supported for tables with FULLTEXT indexes. |
Rebuild with “null” ALTER TABLE ... ENGINE=INNODB |
Yes | Yes | Yes | Yes | Uses ALGORITHM=INPLACE as of MySQL 5.6.17. ALGORITHM=COPY is used if old_alter_table=1 ormysqld --skip-new option is enabled. Table rebuild using online DDL (ALGORITHM=INPLACE ) is not supported for tables with FULLTEXT indexes. |
Set table-level persistent statistics options (STATS_PERSISTENT ,STATS_AUTO_RECALC STATS_SAMPLE_PAGES ) |
Yes | No | Yes | Yes | Modifies .frm file only, not the data file. |