找回密码
 注册
查看: 3464|回复: 1

mysql sql语句修改字段长度

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2020-6-15 01:00:02 | 显示全部楼层 |阅读模式
mysql sql语句修改字段长度
+ k7 w' V0 `) W6 i
语法:
ALTER TABLE 表名 MODIFY COLUMN 字段名  数据类型(修改后的长度)
alter table 表名  modify column 字段名   数据类型(修改后的长度)
要修改Type参数
mysql> desc sl;3 ^! q. B; P% `  N0 ]
+-------+---------+------+-----+---------+-------+
) N& @7 l; y1 j| Field | Type    | Null | Key | Default | Extra |, a( r) j* t2 M6 C
+-------+---------+------+-----+---------+-------+6 |$ ]% C- t; ?& L  P
| name  | char(1) | YES  |     | NULL    |       | % [- S$ r3 n5 `) \3 y
| sex   | char(1) | YES  |     | NULL    |       |
2 z) e8 I7 d4 V$ \| age   | int(3)  | NO   |     | NULL    |       | 3 k6 O. s, H* r  G; e) S/ D
+-------+---------+------+-----+---------+-------+) {8 ~/ p  G  K( Y! Q9 U
3 rows in set (0.00 sec)
: a9 r0 w% p$ V( `' s9 b0 P! y
由char(1)  修改成char(10)
( O3 Q8 Z9 o4 D0 V; G( |
mysql> alter table sl modify column name char(10);( `  }4 `+ o. Z7 E$ t+ F0 x+ d& u
Query OK, 4 rows affected (0.19 sec)" t- C3 }# R9 A
Records: 4  Duplicates: 0  Warnings: 0
mysql> alter table sl modify column sex varchar(5);
6 _8 S/ i5 C! E$ g: EQuery OK, 4 rows affected (0.22 sec)
2 p; b! l5 b. V7 z. Q; E) uRecords: 4  Duplicates: 0  Warnings: 0
mysql> desc sl;
+ i7 [0 S2 e+ L+ n: g' X3 h* A' \$ _# G+-------+------------+------+-----+---------+-------+( M5 b( y: S" |1 m+ G  t
| Field | Type       | Null | Key | Default | Extra |( e. `. Y0 H$ _6 J* G9 S/ U& x
+-------+------------+------+-----+---------+-------+
  |6 S( X% ~& A9 C| name  | char(10)   | YES  |     | NULL    |       | : d- z6 N9 p, t$ d5 R6 j7 C- T
| sex   | varchar(5) | YES  |     | NULL    |       | 2 N7 h: H5 Z$ X9 e) y  w
| age   | int(3)     | NO   |     | NULL    |       |
. h: C& N1 I" t# e1 F+-------+------------+------+-----+---------+-------+
& F) ]+ a- d+ L3 rows in set (0.00 sec)

  w+ R, _0 o8 w/ e5 u# B7 y
把name变成varchar
mysql> alter table sl modify column name varchar(10);3 w( i0 q6 `- ?3 x- f  _1 n: m
Query OK, 4 rows affected (0.07 sec)
" b. q1 ?, ?) J9 v% H1 h6 c0 oRecords: 4  Duplicates: 0  Warnings: 0
mysql> desc sl;
$ b, I/ u9 T" w! k! B2 {; z+-------+-------------+------+-----+---------+-------+) b" s& H, A6 `; T/ T, c3 A, c3 ^
| Field | Type        | Null | Key | Default | Extra |  P4 W, v, |) l3 I9 d3 I9 r. r$ C
+-------+-------------+------+-----+---------+-------+1 r* G5 M0 G7 `( I+ I; H9 m
| name  | varchar(10) | YES  |     | NULL    |       | , M$ M7 C' d. C0 M7 @( u- p2 W2 q
| sex   | varchar(5)  | YES  |     | NULL    |       | - ^. N( i& ~% ^+ J( r
| age   | int(3)      | NO   |     | NULL    |       | # N4 s4 `+ X, d9 a, ~
+-------+-------------+------+-----+---------+-------+, W! O+ ]* _! z# G' q
3 rows in set (0.00 sec)/ ^& C  \& g. @1 R

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2020-6-15 01:00:03 | 显示全部楼层
3.新增字段
& v7 Z( p3 Q6 C7 Q# P语法:( t4 g, j- \9 q/ g' L' G+ F9 l7 ^
# k2 X  W. ^9 G$ m( F- U* |- K* `
新增默认为空的字段' u0 J" _. o# a( ]2 a% d6 X+ h  r
ALTER TABLE 表名 ADD COLUMN 字段名 字段类型 DEFAULT NULL;
; X1 h' z% ?3 @; C; w新增不为空的字段
2 I% Q1 p( ^2 x& Y; J% ~( _ALTER TABLE 表名ADD COLUMN 字段名 字段类型  NOT NULL;
0 r, B; K) [9 {2 O
4 E7 [3 `/ H& d# X; `例子:+ h% W3 O' R+ s9 M: V6 [8 _
ALTER TABLE attence ADD COLUMN attence_name VARCHAR(20) DEFAULT NULL;
7 n' x4 Y1 K% Y$ S7 \* m4 W+ k: a/ ?4 N- B, L
ALTER TABLE attence ADD COLUMN age VARCHAR(20) NOT NULL;
7 q% V$ K" i- K" v# |' S1 D+ T* M  w1 ]- J
. y; @1 B' s& }3 Z* n3 m+ }

' w6 l* S4 c0 W5 f/ A0 p4.删除字段9 o. H. N! A) y2 n, \& [3 ?
语法:1 N& W+ a, ]' Q3 V8 O* _7 o( [; o

: A! G% p9 K# V- U# mALTER TABLE 表名 DROP COLUMN 字段名;
; F# L1 k1 {, Q  f" ?( E1 E) P7 _' t/ q& U8 n  `+ f& ^
例子:, j0 |, l6 \( ~4 k7 W: _

# w" V: s- v# T4 D1 _3 qALTER TABLE attence DROP COLUMN age;3 O! F* v6 d  p9 a

2 f  m% v5 h" A; D; @- r2 B, R2 ^8 \& j $ l7 N8 t5 ~3 @, H0 R
1 c( X9 S! r( x+ h& D
. P  o4 I$ b9 W& u( g2 U4 Q$ C
* @( @! Q8 P+ ?8 m% j
5.批量增加字段. j* E. M6 a0 c; z) h
方法一$ q) K* Q: q. a, o0 }6 G
可以使用事务
3 H- R3 X" `- c9 z( W9 F
6 k8 q6 Z1 {7 N) F: S- ]语法:
1 o" }8 Y2 A: P/ J! o. E
( e5 i- i9 X5 }# B* Bbegin;                                           //事务开始# D, x& [+ @& c# f; g0 [% H
alter table 表名  add 字段名  字段类型(长度);
9 |1 M9 p( s* @) h: A, w$ l" @1 Aalter table 表名 add 字段名  字段类型(长度);# w, `$ \% b& r/ D( M/ `  E
alter table 表名 add 字段名  字段类型(长度);
9 Z7 Y0 ^. j, X) Ealter table 表名 add 字段名  字段类型(长度);0 y/ ?% d9 h' I/ K6 ^5 {
commit;    ) N8 `- `+ V9 f# e9 e

* w* U; q  g4 X) p" h+ c8 ?% X( U例子:
6 U: _- |* F8 s  K* ~( ]: `- N5 u" S; f
begin;                                           //事务开始
* X8 S. a7 d; o0 Halter table em_day_data add f_day_house7 int(11);5 o) _) F/ H* u* r" g
alter table em_day_data add f_day_house8 int(11);
0 B& Q$ t' W- e+ }7 `. ialter table em_day_data add f_day_house9 int(11);
" \6 p6 Q+ w6 Dalter table em_day_data add f_day_house10 int(11);7 u# `. J( M  b2 `
commit;     
7 N7 G" b3 N! a2 Z' h) e8 V4 _7 k1 n# u" |3 K
方法二
: j3 o4 E. k. v1 N
' P9 Q) e5 Z/ V' u. qalter table 表名 add (字段1 类型(长度),字段2 类型(长度),字段3 类型(长度));
1 D* C' a2 A# \. l" {
1 l) F/ \# }# I$ Ualter table em_day_data add (f_day_house11 int(11),f_day_house12 int(11),f_day_house13 int(11));4 C& D- U0 N# E' f$ l/ W

( w1 C/ M/ v4 z7 W' t9 | 0 o1 p6 D. R+ ]  A/ i
3 U8 B3 e! x- j9 L/ _" M* }5 }

& I$ z% o: N* N! A& ~, P; T6 N: i7 A) s) j0 ?/ G+ j; M( T, `
6.批量修改字段名称4 ~& s# O8 I7 L+ I, O) W
语法:. D$ h0 w2 t2 i" r

% C9 l+ M+ w7 ^, o* valter table 表 change 修改前字段名  修改后字段名称 int(11) not null,
. u# G3 h) s* w) H( ^* j' T# ?change 修改前字段名  修改后字段名称 int(11) not null,
1 `* J- U4 U, N* ^change 修改前字段名  修改后字段名称 int(11) not null,
  i1 X5 D9 |; F; F$ mchange 修改前字段名  修改后字段名称 int(11) not null,& ~# w3 K, V' ]0 q  j- l
change 修改前字段名  修改后字段名称 int(11) not null
# a* O8 j" _1 I5 E9 Y
5 C+ a- E6 z( g( D' g: P6 m例子:
; I) X( F( H; {1 Z
% N. M7 h% g8 T4 e/ M5 @# Y4 E3 Valter table em_day_data change f_day_house11 f_day_hour11 int(11) not null,% J& u% n- _1 K+ F3 S( @# `
change f_day_house12 f_day_hour12 int(11) not null,* G/ q$ z1 ~7 k7 L; `: u" L
change f_day_house13 f_day_hour13 int(11) not null,
6 W; I5 ~& L% @+ K8 X% M+ W- ?change f_day_house14 f_day_hour14 int(11) not null,
/ m' n, T. r7 _" [change f_day_house15 f_day_hour15 int(11) not null,& _8 n2 ?" Z( g- ^3 Y2 I! f( Q
change f_day_house16 f_day_hour16 int(11) not null,
8 S) t/ a) c1 B% i# jchange f_day_house17 f_day_hour17 int(11) not null
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-12 01:29 , Processed in 0.019673 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表