时间:2021-07-01 10:21:17 帮助过:4人阅读
mysql> desc test1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
2、设置多字段主键
mysql> create table test2(id int not null, -> name int not null, -> primary key(id,name)); Query OK, 0 rows affected (0.00 sec) mysql> desc test2; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | int(11) | NO | PRI | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
3、设置外键
mysql> create table test4(id int primary key, -> name int not null, -> constraint fk foreign key(id,name) -> references test3(id,name)); Query OK, 0 rows affected (0.00 sec)
4、查看表结构
mysql> desc test4; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | int(11) | NO | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.01 sec)
5、查看表详细语法
mysql> show create table test4\G *************************** 1. row *************************** Table: test4 Create Table: CREATE TABLE `test4` ( `id` int(11) NOT NULL, `name` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `fk` (`id`,`name`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
6、修改表名
mysql> alter table test4 rename test004; Query OK, 0 rows affected (0.00 sec)
7、修改字段数据类型
mysql> alter table test1 modify name varchar(30); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
8、修改字段名
mysql> alter table test1 change name mingzi varchar(20); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
9、增加字段
mysql> alter table test1 add address varchar(60); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
10、将增加字段放在首位
mysql> alter table test1 add user int first; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0
11、将增加字段放在指定位置
mysql> alter table test1 add home int after id; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0
12、删除字段
mysql> alter table test1 drop mingzi; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
13、修改字段位置
mysql> alter table test1 modify address varchar(20) after id; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0
13、修改表引擎
mysql> alter table test1 engine=‘innodb‘; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
14、删除外键
mysql> alter table test004 drop foreign key fk; Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0
15、删除表
mysql> drop table test004; Query OK, 0 rows affected (0.00 sec)
16、创建数据库
mysql> create database test1; Query OK, 1 row affected (0.00 sec)
参考书籍:《MySQL入门很简单》
MySQL数据定义语句
标签: