MySQL数据定义语句主要是创建、修改、删除表,增加,修改,删除字段的操作
创建表:
CREATE TABLE 表名(属性名 数据类型 约束条件,
属性名 数据类型 约束条件,
属性名 数据类型 约束条件,
属性名 数据类型 ,
);
完整约束条件:
PRIMARY KEY 主键
FOREIGN KEY 外键
NOT NULL 非空
UNIQUE 唯一键
AUTO_INCREMENT 自增键(mysql特色)
DEFAULT 设置默认值
1、创建表test1
mysql> create table test1(id int not null primary key, -> name varchar(20) not null); Query OK, 0 rows affected (0.00 sec)
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入门很简单》
时间: 2024-10-13 23:08:48