易陆发现互联网技术论坛

 找回密码
 开始注册
查看: 3460|回复: 1
收起左侧

mysql sql语句修改字段长度

[复制链接]
发表于 2020-6-15 01:00:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?开始注册

x
mysql sql语句修改字段长度1 C' j0 ?2 m$ ?$ F/ x, b) _
语法:
ALTER TABLE 表名 MODIFY COLUMN 字段名  数据类型(修改后的长度)
alter table 表名  modify column 字段名   数据类型(修改后的长度)
要修改Type参数
mysql> desc sl;
( W6 E$ h$ @$ e; `; V+-------+---------+------+-----+---------+-------+
/ l2 j7 y. v* C) V7 }| Field | Type    | Null | Key | Default | Extra |
9 A7 J. e7 t8 I) z/ x, u& M+-------+---------+------+-----+---------+-------+
6 V/ b7 E2 Y! ]' L0 N0 e" i/ _| name  | char(1) | YES  |     | NULL    |       |
7 v/ ~& @7 D4 `/ {| sex   | char(1) | YES  |     | NULL    |       | 5 l: n" c5 y# o( C
| age   | int(3)  | NO   |     | NULL    |       | 5 }1 J9 A2 R/ ]3 x- t8 G
+-------+---------+------+-----+---------+-------+
( |' H5 P* w( w7 q  X3 rows in set (0.00 sec)

+ |% U# N- c; a  {9 \
由char(1)  修改成char(10)

- i: H) [- l6 w! a! [
mysql> alter table sl modify column name char(10);9 g( h) a! j' C8 ]
Query OK, 4 rows affected (0.19 sec)
, j. p+ H7 o/ W6 c7 [Records: 4  Duplicates: 0  Warnings: 0
mysql> alter table sl modify column sex varchar(5);6 a) O) ~" i7 h+ Q' H
Query OK, 4 rows affected (0.22 sec)( y& y' G: E& Q6 ^7 G8 H
Records: 4  Duplicates: 0  Warnings: 0
mysql> desc sl;
  ]# `7 H* K# q  j: @+-------+------------+------+-----+---------+-------+
; ^# G. E( Q9 t| Field | Type       | Null | Key | Default | Extra |9 n  M" B- `. L
+-------+------------+------+-----+---------+-------+
* \9 ]$ n& ^/ d9 y2 N) c| name  | char(10)   | YES  |     | NULL    |       |
& z) l" s: k) f4 S: ~2 M| sex   | varchar(5) | YES  |     | NULL    |       |
% g. d4 N7 ?: ^( ?. r8 Z0 y4 e| age   | int(3)     | NO   |     | NULL    |       | % Z7 }: g- u/ F9 g' ~8 }' c
+-------+------------+------+-----+---------+-------+
4 _& W# F" A) U* A% C; Y$ l$ \7 a3 rows in set (0.00 sec)

' U) \4 l3 l  n' P
把name变成varchar
mysql> alter table sl modify column name varchar(10);* }( F/ [0 U4 i
Query OK, 4 rows affected (0.07 sec)
  s4 B& _% l6 k( ^* H2 S. tRecords: 4  Duplicates: 0  Warnings: 0
mysql> desc sl;
' G- e' U1 R0 M: ~8 f+-------+-------------+------+-----+---------+-------+. w2 w1 w* S: h4 S8 `+ m
| Field | Type        | Null | Key | Default | Extra |
0 b9 b* W1 I0 y* v, L' l3 i" `/ W+-------+-------------+------+-----+---------+-------+, R' P: D1 ?" u/ r; ~6 \
| name  | varchar(10) | YES  |     | NULL    |       | 2 s  v# N! [" _% a" o* Z
| sex   | varchar(5)  | YES  |     | NULL    |       | * s1 |( @6 x5 d) w9 \
| age   | int(3)      | NO   |     | NULL    |       |
$ M9 x! Q& r  m7 v) q& t$ _5 Z+-------+-------------+------+-----+---------+-------+
: n# T& r7 y4 o5 a& I2 `4 z3 rows in set (0.00 sec)! J! x% j) E! R" g
 楼主| 发表于 2020-6-15 01:00:03 | 显示全部楼层
3.新增字段
$ ]4 E) g1 N3 r9 a+ o% a' O语法:
: o: F7 s- T  ^* A" J& o. k0 s( J$ [
新增默认为空的字段
& i& [1 K. [  U* `) YALTER TABLE 表名 ADD COLUMN 字段名 字段类型 DEFAULT NULL;
' C2 a: e0 s8 |% f$ g7 T1 `新增不为空的字段' i9 A% V  `. D/ y1 l! o
ALTER TABLE 表名ADD COLUMN 字段名 字段类型  NOT NULL;. U: [' @3 ^& J. ^

, D- i$ N( G& Y/ j, D例子:, I- o+ P8 D; \9 B, U1 ?# ?' X
ALTER TABLE attence ADD COLUMN attence_name VARCHAR(20) DEFAULT NULL; % C* w- D8 Y3 c: J% }' w) E5 C
7 }. o' }3 ^4 O
ALTER TABLE attence ADD COLUMN age VARCHAR(20) NOT NULL;& ?  _  d9 M" i/ q" |

5 O: x1 I/ y$ O( z2 q
2 z+ c# ~8 _4 P* c/ ]/ V4 O. w- j0 N7 N4 p# T+ c
4.删除字段# w/ x0 J, ]5 r/ _0 o! n  W/ P7 B
语法:" a1 Y1 G6 \; V7 L
3 }% [* O) z! m% L9 ~
ALTER TABLE 表名 DROP COLUMN 字段名;$ r) W2 m% V* f; ]

. ]1 L1 q& @5 w0 @6 W' ]例子:
/ Z8 ]! L$ K6 J6 g- e
  v+ l5 ?- i0 ?& N2 D4 v4 i+ GALTER TABLE attence DROP COLUMN age;8 @! R# k0 x5 x0 G4 t

* B- {5 G4 Z+ A# J: ^* j
8 }( Y9 q' E+ i/ g7 A% E, W8 e; V7 A6 r' ~% o3 ?! o* b0 \* f2 f

' G) J& x5 T8 u+ o6 i; f, ]- b
+ }9 p9 e% H: N9 l5.批量增加字段
" m$ ], U0 v& G方法一
' C) o, b) R3 C) d7 ?! Y4 r' y) o可以使用事务
: j/ y1 Z6 ^$ q/ N8 ]4 a( `% J" `) B* B$ ?) t
语法:
8 l. d8 n  Q; H) f6 v: H9 b
+ M, ^: u- f6 R- K; |2 H! mbegin;                                           //事务开始
5 X3 B' g, s) G$ J2 ]/ e7 zalter table 表名  add 字段名  字段类型(长度);8 \# d/ e; D+ |! ~& B1 z
alter table 表名 add 字段名  字段类型(长度);
! g5 O* f0 B" Aalter table 表名 add 字段名  字段类型(长度);1 E" q$ c: k& `! Z6 E$ k6 x
alter table 表名 add 字段名  字段类型(长度);
$ W3 A2 [& w1 P, }commit;    3 ]  s5 w7 I9 t

( u2 F, m1 C9 v. a例子: , u* Y  o1 g  s0 [$ h' A- Y
$ E. ^0 \1 j1 K
begin;                                           //事务开始
4 x" ?: e6 k. T! T/ q0 U; s5 ralter table em_day_data add f_day_house7 int(11);7 N$ ]' r( X! A' t2 C
alter table em_day_data add f_day_house8 int(11);. e7 [- A0 D3 ]9 B( v( I
alter table em_day_data add f_day_house9 int(11);) `0 K$ i+ W0 N- v7 q, o" U
alter table em_day_data add f_day_house10 int(11);
" M) I  z5 ^$ e1 Y/ {, ^5 \- A# ^commit;     . F2 k2 E* z0 M2 _3 k7 k5 p

7 v% x5 j  `- N" R方法二
0 v' r, [" s# K7 Y5 X: q
8 l7 l5 j- C- c& D3 ~/ Halter table 表名 add (字段1 类型(长度),字段2 类型(长度),字段3 类型(长度));3 J& C/ a% }' @8 Z& D1 K7 f9 `

' x8 R6 O" O- N  B2 r" e  |9 ?5 ^alter table em_day_data add (f_day_house11 int(11),f_day_house12 int(11),f_day_house13 int(11));5 Q4 F+ F1 l6 K0 F. T) ]# H7 |

6 i# ^8 W3 a, | ' P5 p4 l5 C! ?0 E# Z5 R
0 m& C1 ]  j- I; Q: r

6 c/ L( B0 S% |
/ x1 n4 b" Y& `7 \' ]! A& y6.批量修改字段名称
, n! z% `- b. A6 G. L; O语法:
' K2 h, B& E- \  ?+ {4 b
7 [" V- x, j5 W- Y) l% v" \  aalter table 表 change 修改前字段名  修改后字段名称 int(11) not null,
+ g* b; W& ~/ jchange 修改前字段名  修改后字段名称 int(11) not null,8 n- r& l0 U, y9 I1 V6 Q% }
change 修改前字段名  修改后字段名称 int(11) not null,
6 ?( X, V: K9 qchange 修改前字段名  修改后字段名称 int(11) not null,
4 e% y, V; F7 f( [, ~  Y6 Kchange 修改前字段名  修改后字段名称 int(11) not null2 D$ n! a: I& ~' n) s
, v* w) C& I4 I
例子:
; W: _" d" `0 D3 z, [( i- Q. Z6 G
alter table em_day_data change f_day_house11 f_day_hour11 int(11) not null,
; C# U1 k; X# r6 W* G; E& @* }( [change f_day_house12 f_day_hour12 int(11) not null,9 l0 t9 n2 L1 V' \- ]
change f_day_house13 f_day_hour13 int(11) not null,
* \" h- a$ z) s2 C+ J  Vchange f_day_house14 f_day_hour14 int(11) not null,5 i4 l2 P) C8 m& a% i9 U  w
change f_day_house15 f_day_hour15 int(11) not null,9 T- J/ p# |) v
change f_day_house16 f_day_hour16 int(11) not null,1 z! F+ |% C9 ]8 {# J- m7 E
change f_day_house17 f_day_hour17 int(11) not null
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

北京云银创陇科技有限公司以云计算运维,代码开发

QQ|返回首页|Archiver|小黑屋|易陆发现技术论坛 点击这里给我发消息

GMT+8, 2026-4-8 13:45 , Processed in 0.093942 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

© 2012-2025 Discuz! Team.

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