博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql DDL&DML 语言
阅读量:4935 次
发布时间:2019-06-11

本文共 4409 字,大约阅读时间需要 14 分钟。

DDL:数据定义语言

  CREATE, ALTER, DROP

CREATE相关的常用命令:

CREATE DATABASE

CREATE EVENT
CREATE FUNCTION
CREATE FUNCTION UDF
CREATE INDEX
CREATE PROCEDURE
CREATE SERVER
CREATE TABLE
CREATE TABLESPACE
CREATE TRIGGER
CREATE USER
CREATE VIEW

创建表:CREATE TABLE

(1) 直接创建;

CREATE TABLE tablename(id INT(11) NOT NULL,name VARCHAR(256) NOT NULL);

(2) 通过查询现存的表创建;新表会被直接插入查询而来的数据;

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name[(create_definition,...)][table_options][partition_options]select_statementeg:CREATE TABLE MYTAB2 select a.StuID,a.Name from hellodb.students as a;

(3) 通过复制现存的表的表结构创建;不复制数据;

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name

{ LIKE old_tbl_name | (LIKE old_tbl_name) }

mysql> CREATE TABLE lala123 like hellodb.teachers;

(4) 查看表结构:

DESCRIBE tbl_name;

mysql> desc teachers;+--------+----------------------+------+-----+---------+----------------+| Field  | Type                 | Null | Key | Default | Extra          |+--------+----------------------+------+-----+---------+----------------+| TID    | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment || Name   | varchar(100)         | NO   |     | NULL    |                || Age    | tinyint(3) unsigned  | NO   |     | NULL    |                || Gender | enum('F','M')        | YES  |     | NULL    |                |+--------+----------------------+------+-----+---------+----------------+4 rows in set (0.00 sec)

mysql> show create table teachers\G

*************************** 1. row ***************************
Table: teachers
Create Table: CREATE TABLE `teachers` (
`TID` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(100) NOT NULL,
`Age` tinyint(3) unsigned NOT NULL,
`Gender` enum('F','M') DEFAULT NULL,
PRIMARY KEY (`TID`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

(5) 查看表状态信息:

SHOW [FULL] TABLES [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr]

mysql> show tables;+-------------------+| Tables_in_hellodb |+-------------------+| classes           || coc               || courses           || lala123           || scores            || students          || teachers          || toc               |+-------------------+8 rows in set (0.00 sec)

修改表:ALTER TABLE

ALTER TABLE tablename MODIFY [COLUMN] column_definition [FIRST | AFTER col_name]

(1)增加表字段:

ALTER TABLE tablename ADD [COLUMN] column_definition [FIRST | AFTER col_name];  如给上表添加age字段:  ALTER TABLE tablename ADD age INT(11) NOT NULL;   

(2)删除表字段:

ALTER TABLE tablename DROP [COLUMN] col_name;  如删除上表age字段:  ALTER TABLE tablename DROP age;

(3)修改字段:

ALTER TABLE tablename CHANGE [COLUMN] old_col_name new_col_definition [FIRST | AFTER col_name];  如修改上表字段name名为uname:  ALTER TABLE tablename CHANGE name uname CHAR(128);

CHANGE和MODIFY都可以修改表字段定义,不同的是CHANGE写两次字段名,但是CHANGE可以修改列名,而MODIFY则不能。 

(4)修改字段顺序:

在上表中添加birth字段,并放到列id后面:  ALTER TABLE tablename ADD birth DATETIME AFTER id;  再次修改,把它放到uname表后面:  ALTER TABLE tablename MODIFY birth DATETIME NOT NULL AFTER uname;

(5)修改表明:

ALTER TABLE tablename RENAME [TO] new_tablename;  如把上面表名tablename改成test:  ALTER TABLE tablename RENAME test;

 

删除表:DROP TABLE

mysql> show tables;+----------------+| Tables_in_mydb |+----------------+| mytb1          || mytb2          || mytb3          || mytb4          |+----------------+4 rows in set (0.00 sec)mysql> drop table mytb4;Query OK, 0 rows affected (0.02 sec)mysql> show tables;+----------------+| Tables_in_mydb |+----------------+| mytb1          || mytb2          || mytb3          |+----------------+3 rows in set (0.00 sec)

删除表信息 DELETE  注:一定要添加条件,否则整表信息被删除。

delete from tableName where ......eg:mysql> SELECT * FROM mydb.mytb4;+-----+---------------+-----+--------+| TID | Name          | Age | Gender |+-----+---------------+-----+--------+|   1 | Song Jiang    |  45 | M      ||   2 | Zhang Sanfeng |  94 | M      ||   3 | Miejue Shitai |  77 | F      ||   4 | Lin Chaoying  |  93 | F      |+-----+---------------+-----+--------+4 rows in set (0.00 sec)mysql> DELETE FROM mytb4 WHERE TID = 3;Query OK, 1 row affected (0.01 sec)mysql> SELECT * FROM mydb.mytb4;+-----+---------------+-----+--------+| TID | Name          | Age | Gender |+-----+---------------+-----+--------+|   1 | Song Jiang    |  45 | M      ||   2 | Zhang Sanfeng |  94 | M      ||   4 | Lin Chaoying  |  93 | F      |+-----+---------------+-----+--------+3 rows in set (0.00 sec)

 sql语句在mysql客户端中可以使用help名称查看语句语法。

转载于:https://www.cnblogs.com/Nvax/p/6760056.html

你可能感兴趣的文章
Jquery特效
查看>>
web服务器
查看>>
EV: Workaround to Allow Only One Instance or Window of outlook
查看>>
数据校验,
查看>>
IntelliJ IDEA完美解决tomcat8+乱码问题
查看>>
GDI+ ColorMatrix的完全揭秘
查看>>
破解电信光猫华为HG8120C关闭路由功能方法
查看>>
在Qt示例项目的C ++ / QML源中的//! [0]的含义是什么?
查看>>
【智能家居篇】wifi网络接入原理(上)——扫描Scanning
查看>>
操作引入xml文件的书包(定位到指定节点)
查看>>
操作系统学习笔记系列(一)- 导论
查看>>
已计划将多个默认网关用于提供单一网络
查看>>
CSS实例:图片导航块
查看>>
python进阶七_文件操作(三)
查看>>
window的对象有哪些(笔记)
查看>>
成绩查询方法指引Pmp
查看>>
Boolean Expressions
查看>>
They Are Everywhere
查看>>
数据结构--汉诺塔递归Java实现
查看>>
day14 多态与抽象
查看>>