mariadb mysql 添加 修改 删除 表字段 sql语句 示例

小豆苗 1年前 ⋅ 6966 阅读

 

一、添加表的字段:
1. 规则:alter table 表名 add 字段名 字段的类型
示例1:alter table table1 add newcol varchar(10) not Null;
2. 主键自动增长:
示例2:alter table table1 add id int unsigned not Null auto_increment primary key
3. 指定的一个字段后面添加一个字段:
示例:alter table newexam add address varchar(110) after stu_id;


二、修改表的字段:
1. 修改字段类型:ALTER TABLE 表名 MODIFY COLUMN 字段名 字段类型定义
示例:ALTER TABLE chatter_users MODIFY COLUMN ip VARCHAR(50);
2. 修改字段名:alter table 表名 change 原字段名 新字段名 字段的类型
示例:alter table student change oldcolumn newcolumn char(10) not null


三、删除表的字段:
1. 规则:alter table 表名 drop column 字段名
示例:alter table `user` drop column name

四、表的重命名:
1. 规则:alter table 原表名 rename 现表名
示例:alter table table1 rename table2;
2. 重命名后再调整字段的位置
示例:ALTER TABLE user CHANGE uId uId int not null default 0 AFTER cID

五、删除表的数据:
1. 删除整个表
示例:drop table table1;
2. 删除指定范围内的数据
规则:delete from 表名 where (条件) id 不是从1开始
示例:delete from table1 where id > 100;

如有不对的地方,欢迎大家提出指正意见。


全部评论: 0

    我有话说: