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

mysql sql语句修改字段长度

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2020-6-15 01:00:02 | 显示全部楼层 |阅读模式
mysql sql语句修改字段长度
- s0 w' @9 C# j- Y
语法:
ALTER TABLE 表名 MODIFY COLUMN 字段名  数据类型(修改后的长度)
alter table 表名  modify column 字段名   数据类型(修改后的长度)
要修改Type参数
mysql> desc sl;
6 m! O# D! [' W. K1 f" T. e5 ~+-------+---------+------+-----+---------+-------+
0 ^! f3 i8 R/ v- \| Field | Type    | Null | Key | Default | Extra |7 L7 q2 ]( l! n9 @3 u( m, ^
+-------+---------+------+-----+---------+-------+: g8 L! `- L: c8 k0 c
| name  | char(1) | YES  |     | NULL    |       | : [) u% r3 b& C2 v/ f. ~& Z
| sex   | char(1) | YES  |     | NULL    |       | 8 R' h7 x6 z9 {/ i
| age   | int(3)  | NO   |     | NULL    |       | 4 x4 X+ H8 M6 T
+-------+---------+------+-----+---------+-------+- G$ p' r# m& S* S
3 rows in set (0.00 sec)
! ~9 m7 T4 Y& R) i8 S; l0 p
由char(1)  修改成char(10)

: @/ W5 x$ q6 p; r1 r/ [# A' {0 V
mysql> alter table sl modify column name char(10);; n" J0 F) J* A/ N8 U( `* m
Query OK, 4 rows affected (0.19 sec)
. C  h, t% ?+ V1 g& a0 J  d. }Records: 4  Duplicates: 0  Warnings: 0
mysql> alter table sl modify column sex varchar(5);
! D9 }6 _/ ~! N* X# ^/ @Query OK, 4 rows affected (0.22 sec), g  g5 Z3 l+ o0 x& t& `6 I5 a
Records: 4  Duplicates: 0  Warnings: 0
mysql> desc sl;
( G. |3 B! s) g% [; E+-------+------------+------+-----+---------+-------+
7 Z6 D4 X, M1 L( P: _) @| Field | Type       | Null | Key | Default | Extra |$ B. P! x  z( ]3 x' s! f! r) Y
+-------+------------+------+-----+---------+-------+. t4 c3 L4 i) P6 k  f* B2 M1 @
| name  | char(10)   | YES  |     | NULL    |       |
% s  K* M! m3 Z& c* d' V. J| sex   | varchar(5) | YES  |     | NULL    |       | / J* X( W4 I1 J, `( g, A% v
| age   | int(3)     | NO   |     | NULL    |       | " v0 `$ }+ X. l. ~  p+ j& \2 |
+-------+------------+------+-----+---------+-------+6 K+ h" k: e3 L9 L% d
3 rows in set (0.00 sec)
% I, Z, ?: X8 d" F! H7 C
把name变成varchar
mysql> alter table sl modify column name varchar(10);' _, w/ B2 y- S+ S# I" |! h
Query OK, 4 rows affected (0.07 sec)
2 Y  |' O3 M; _2 F# n2 o  f8 eRecords: 4  Duplicates: 0  Warnings: 0
mysql> desc sl;
7 d" k0 b# T: p1 j6 \/ Q, S+-------+-------------+------+-----+---------+-------+# b9 I& J: X1 _
| Field | Type        | Null | Key | Default | Extra |% |& Z* w3 ]- N4 ^2 K& r
+-------+-------------+------+-----+---------+-------+
/ P) X1 R6 Z/ E, h8 Y" s# O| name  | varchar(10) | YES  |     | NULL    |       |
2 ~+ D5 x  E* A| sex   | varchar(5)  | YES  |     | NULL    |       |
& h/ Q* ]. X( Z* n! D  }| age   | int(3)      | NO   |     | NULL    |       | 6 ?" d5 P( F* K+ X5 q) u- t: E
+-------+-------------+------+-----+---------+-------+& ], l! b6 H; R; e  M5 A1 i2 {
3 rows in set (0.00 sec)
' n' H8 c% n) x/ G

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2020-6-15 01:00:03 | 显示全部楼层
3.新增字段
* |  W8 k+ |& N3 g3 ]- Q; A语法:7 V! |) u( v) v; T/ p/ N' S

+ U+ X" x8 R7 b2 k' l. B; S7 ?: ]% {新增默认为空的字段
- n/ g6 ?' ^% F4 qALTER TABLE 表名 ADD COLUMN 字段名 字段类型 DEFAULT NULL; 6 v/ ^  ?& P  d% J3 V& f# C$ C
新增不为空的字段- X0 c) q3 w) O& J4 `, P
ALTER TABLE 表名ADD COLUMN 字段名 字段类型  NOT NULL;
6 h: i8 C/ y$ h
1 C. B! G4 _' |6 i例子:
4 L/ I% {  l3 `4 d/ I' |ALTER TABLE attence ADD COLUMN attence_name VARCHAR(20) DEFAULT NULL;
: ?  p0 _, [1 f' k& o( c
3 E) c* Q( w$ ^- lALTER TABLE attence ADD COLUMN age VARCHAR(20) NOT NULL;0 ]) T* d  o1 r. Z

2 b% \# b# z% R% V2 j
- o( F: y1 J* h# Z
3 X9 v$ m! B& F4.删除字段5 C/ U2 t& e( N* H* u5 |
语法:9 I1 j2 g! g: \5 _
# Y3 u3 r- a2 k! k
ALTER TABLE 表名 DROP COLUMN 字段名;3 ~% `" R9 R' J! S. _( G. H' e; Q6 ^

' l. v& x) s$ E1 O8 c! }; Y/ v例子:
4 Z: U: x% q) e2 j0 ?% H. U3 M0 _
3 v, Z  t  i2 {7 Q  _9 O6 rALTER TABLE attence DROP COLUMN age;% ]' d% O0 H# b- q; D3 l

8 @2 y1 B$ k# U/ q
+ w( J: o7 w$ y) R1 k( q
0 X/ n* |3 B6 H& H; U2 d0 H
% P. G; t/ J8 ?) t- w2 c, ]- b, J
, f0 n0 _; ~* b, Y8 U; ]8 |5.批量增加字段
  b  A' O8 l% P% |方法一
4 `" C+ k# S! Y( P可以使用事务
) L7 y$ B# v& ~$ S! a7 j3 i( F6 P# U& o1 {" z: g7 C" V5 r. _
语法:, G$ V: g3 z% _2 q
, Q/ z6 H, {- O8 @9 t4 E3 E
begin;                                           //事务开始
0 P; n- @! W2 f7 qalter table 表名  add 字段名  字段类型(长度);
' N* {1 N+ Z7 P' n4 malter table 表名 add 字段名  字段类型(长度);) ^7 T! I# p; u% j( f
alter table 表名 add 字段名  字段类型(长度);
) Y8 g$ n( l  balter table 表名 add 字段名  字段类型(长度);: Z3 S/ [7 [3 Z9 g7 B8 T
commit;    6 g9 \5 t: z3 v+ n% q+ l

# M% E& F  i5 @# t# D) y* ^例子:
  M0 b$ q! K6 M& ?# ^7 V4 `* {" `0 z8 ]# z; H- V, s
begin;                                           //事务开始. }- F+ Q. g8 K+ {  w6 Q( X, U! g
alter table em_day_data add f_day_house7 int(11);  q! |  e; B, j) K' ~; _0 ]( m
alter table em_day_data add f_day_house8 int(11);1 K* H6 N7 B' z- @! t
alter table em_day_data add f_day_house9 int(11);$ k9 S4 u, G1 g5 r1 D; [7 [
alter table em_day_data add f_day_house10 int(11);, a1 R6 y  z% F
commit;     
) y7 ]) @6 g5 }% m# W0 k+ s3 o  Q+ q  O  \' J# ^
方法二
' R4 \5 j) V7 D7 h0 K0 I4 ~' U' q5 ~$ P' W5 g+ N- Z- c
alter table 表名 add (字段1 类型(长度),字段2 类型(长度),字段3 类型(长度));; I: ]- s' f& G1 A2 S

  s. t# j8 |) x; Y8 Y7 a! M3 Aalter table em_day_data add (f_day_house11 int(11),f_day_house12 int(11),f_day_house13 int(11));
( N. s5 x% N! M. d0 Z- s" b2 e8 v- ~! K3 |( S
3 [$ E1 [1 p# [7 @! t
) }# _+ w! M% x) P2 h5 Y
1 b3 p. q3 C( P, w& h7 d  |

7 M- X2 `/ c- q7 ?$ b" n" |6.批量修改字段名称
3 o9 U5 Z0 Y: B4 k2 \: A语法:9 p6 r/ Y* F# C  y9 R

6 S- J% t& u, z- c' {6 s3 Dalter table 表 change 修改前字段名  修改后字段名称 int(11) not null,
3 ~7 K$ W& _% _& pchange 修改前字段名  修改后字段名称 int(11) not null,# O0 ?$ C  M9 u
change 修改前字段名  修改后字段名称 int(11) not null,
8 N: a1 U& e9 F( _5 }8 M( I. W7 fchange 修改前字段名  修改后字段名称 int(11) not null,, t! |- g- ?. X
change 修改前字段名  修改后字段名称 int(11) not null
) @6 f, {- e, I- p4 W: k
: @( T' W9 q# F5 J例子:( P' s+ K2 _3 |
% k3 Z( u7 s5 z& J2 c
alter table em_day_data change f_day_house11 f_day_hour11 int(11) not null,
8 g' }9 A5 G2 [change f_day_house12 f_day_hour12 int(11) not null,
" U& M( ^- B+ X7 ^) b- t- S5 w! vchange f_day_house13 f_day_hour13 int(11) not null,
# C0 A* U/ D0 H- Dchange f_day_house14 f_day_hour14 int(11) not null,
* R& S2 P: U, h6 wchange f_day_house15 f_day_hour15 int(11) not null,
' B1 |/ W  S7 ?: Ochange f_day_house16 f_day_hour16 int(11) not null,
4 K* H' j9 o7 @" [4 e; `3 \change f_day_house17 f_day_hour17 int(11) not null
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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