1、MySQL 修改字段类型或长度:

1、MySQL 修改字段类型或长度:

数据库中address表 city字段是varchar(30)

修改类型可以用(谨慎修改类型,可能会导致原有数据出错)

mysql> alter table address modify column city char(30);

修改长度可以用(修改长度,要保证不短与已有数据,以保证原有数据不出错)

mysql> alter table address modify column city varchar(50);

2、mySQL:两表更新(用一个表更新另一个表)的SQL语句

用一个表中的字段去更新另外一个表中的字段, MySQL 中有相应的 update 语句来支持,不过这个 update 语法有些特殊。看一个例子就明白了。

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
create table student
(
   student_id    int          not null
  ,student_name  varchar(30)  not null
  ,city_code     varchar(10null
  ,city_name     varchar(50null
);
create table city
(
   code varchar(10) not null
  ,name varchar(50) not null
);
insert into student values(1, 'john', '001', null);
insert into student values(2, 'nick', '002', null);
 
insert into city values('001', 'beijing');
insert into city values('002', 'shanghai');
insert into city values('003', 'shenzhen');
有两个表:student & city,现在需要取出 city.name 来更新 student.city_name。两表关联条件是 student.city_code=city.code。
 
update student s, city c
   set s.city_name = c.name
 where s.city_code = c.code;
也可以试下面的相关子查询:
 
update student s set city_name = (select name from city where code = s.city_code);

3、MySQL中的表中增加删除字段

1增加两个字段:

复制代码
mysql> create table id_name(id int,name varchar(20));
Query OK, 0 rows affected (0.13 sec)


mysql> alter table id_name add age int,add address varchar(11);
Query OK, 0 rows affected (0.13 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc id_name;

+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | YES  |     | NULL    |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| age     | int(11)     | YES  |     | NULL    |       |
| address | varchar(11) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

2.删除两个字段
mysql> alter table id_name drop column age,drop column address;
Query OK, 0 rows affected (0.14 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc id_name;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

3.插入
mysql> insert into id_name values (1,'qustdjx');
Query OK, 1 row affected (0.00 sec)
4.查询看一下
mysql> alter table id_name add age int,add address varchar(11);
Query OK, 1 row affected (0.07 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from id_name;
+------+---------+------+---------+
| id   | name    | age  | address |
+------+---------+------+---------+
|    1 | qustdjx | NULL | NULL    |
+------+---------+------+---------+
1 row in set (0.00 sec)
5.新增字段并插入
mysql> insert into id_name values(2,'qust',14,'山东');
Query OK, 1 row affected (0.00 sec)

mysql> select * from id_name;
+------+---------+------+---------+
| id   | name    | age  | address |
+------+---------+------+---------+
|    1 | qustdjx | NULL | NULL    |
|    2 | qust    |   14 | 山东    |
+------+---------+------+---------+
2 rows in set (0.00 sec) 
复制代码

1.增加一个字段
alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; //增加一个字段,默认为空
alter table user add COLUMN new2 VARCHAR(20) NOT NULL;  //增加一个字段,默认不能为空  www.2cto.com  
 
2.删除一个字段
alter table user DROP COLUMN new2;   //删除一个字段
 
3.修改一个字段
alter table user MODIFY new1 VARCHAR(10);  //修改一个字段的类型
 
alter table user CHANGE new1 new4 int;  //修改一个字段的名称,此时一定要重新指定该字段的类型


猜你喜欢

转载自blog.csdn.net/shujuelin/article/details/79793484