学习了MySQL很长时间了 一直对字段的操作知之甚少,今天在网上查找了一些资料在这总结一下,不对的地方还希望大神们改正。
首先 进入MySQL的dos窗口输入密码
1 创建数据库
create database ddlTest;
2 进入数据库
use ddlTest;
3 创建表并查看表的字段的属性
create table t_test(
id int(11) not null default ‘0’,
username varchar(10) not null,
sex char(1) not null check(sex = '0' or sex = '1'),
)
desc t_test;查看表的字段属性
4 开始对表的字段进行修改
-------add
4.1 增加字段并为之添加主键
alter table t_test add 新字段 数据类型 约束条件 primary key;
------change
4.2 修改表字段的属性和重命名
alter table t_test change id user_id 数据类型;
alter table t_test change id id 数据类型;
5 重命名表
alter table t_test rename t;