表操作

alter mysql 设置字段编码规则 mysql设置字段类型_mysql

添加字段类型 add after first

alter table 表名 add 字段 类型;
方法:添加新字段 类型
 mysql> alter table g3 add age int(3); -------添加一个字段方法:添加 多个字段 类型
 mysql> alter table g3 add (jiaxiang varchar(10),hunyin enum(‘yihun’,‘weihun’));方法:把添加的字段放到某个字段后面 after
 alter table 表名 add 添加的字段(和类型) after name;方法;把添加的字段放在第一个 first
 alter table 表名 add 添加的字段(和类型) first;

案例:添加一个字段

mysql> insert into g3(id,name) values(4,‘xtian’); 添加一个字段类型
 Query OK, 1 row affected (0.01 sec)
 mysql> select * from g3;
 ±-----±------±----+
 | id | name | sex |
 ±-----±------±----+
 | 1 | xtian | nan |
 | 2 | xli | nan |
 | 4 | xtian | nan |
 ±-----±------±----+
 3 rows in set (0.00 sec)

alter mysql 设置字段编码规则 mysql设置字段类型_数据库_02


案例:添加多个字段类型

mysql> alter table g3 add (jiaxiang varchar(10),hunyin enum(‘yihun’,‘weihun’));
 Query OK, 0 rows affected (0.03 sec)
 Records: 0 Duplicates: 0 Warnings: 0
 mysql> select * from g3;
 ±-----±------±----±-----±---------±-------+
 | id | name | sex | age | jiaxiang | hunyin |
 ±-----±------±----±-----±---------±-------+
 | 1 | xtian | nan | NULL | NULL | NULL |
 | 2 | xli | nan | NULL | NULL | NULL |
 | 4 | xtian | nan | NULL | NULL | NULL |
 ±-----±------±----±-----±---------±-------+
 3 rows in set (0.00 sec)

alter mysql 设置字段编码规则 mysql设置字段类型_字段_03


案例:把添加的字段放到某个字段后面

mysql> alter table g3 add shengri float(5,3) after name; 方法
 Query OK, 0 rows affected (0.05 sec)
 Records: 0 Duplicates: 0 Warnings: 0
 mysql> select * from g3;
 ±-----±------±--------±----±-----±---------±-------+
 | id | name | shengri | sex | age | jiaxiang | hunyin |
 ±-----±------±--------±----±-----±---------±-------+
 | 1 | xtian | NULL | nan | NULL | NULL | NULL |
 | 2 | xli | NULL | nan | NULL | NULL | NULL |
 | 4 | xtian | NULL | nan | NULL | NULL | NULL |
 ±-----±------±--------±----±-----±---------±-------+
 3 rows in set (0.00 sec)

alter mysql 设置字段编码规则 mysql设置字段类型_数据库_04


案例;把添加的字段放在第一个

mysql> alter table g3 add ip int(3) first; 方法
 Query OK, 0 rows affected (0.04 sec)
 Records: 0 Duplicates: 0 Warnings: 0mysql> select * from g3;
 ±-----±-----±------±--------±----±-----±---------±-------+
 | ip | id | name | shengri | sex | age | jiaxiang | hunyin |
 ±-----±-----±------±--------±----±-----±---------±-------+
 | NULL | 1 | xtian | NULL | nan | NULL | NULL | NULL |
 | NULL | 2 | xli | NULL | nan | NULL | NULL | NULL |
 | NULL | 4 | xtian | NULL | nan | NULL | NULL | NULL |
 ±-----±-----±------±--------±----±-----±---------±-------+
 3 rows in set (0.00 sec)

alter mysql 设置字段编码规则 mysql设置字段类型_数据库_05

2.修改字段和类型

1.修改名称、数据类型、类型
方法一:修改名称类型

alter table 表名 change 旧字段 新字段 类型; #change修改字段名称,类型,约束,顺序

方法二:修改并改变位置

mysql> alter table t3 change max maxs int(15) after id; #修改字段名称与修饰并更换了位置

案例:修改并改变位置

mysql> alter table g3 change ip diqu varchar(4) after id; 方法
 Query OK, 3 rows affected (0.01 sec)
 Records: 3 Duplicates: 0 Warnings: 0mysql> desc g3;
 ±---------±-----------------------±-----±----±--------±------+
 | Field | Type | Null | Key | Default | Extra |
 ±---------±-----------------------±-----±----±--------±------+
 | id | int(5) | YES | | NULL | |
 | diqu | varchar(4) | YES | | NULL | |
 | name | varchar(6) | YES | | NULL | |
 | shengri | float(5,3) | YES | | NULL | |
 | sex | enum(‘nan’,‘nv’) | NO | | nan | |
 | age | int(3) | YES | | NULL | |
 | jiaxiang | varchar(10) | YES | | NULL | |
 | hunyin | enum(‘yihun’,‘weihun’) | YES | | NULL | |
 ±---------±-----------------------±-----±----±--------±------+
 8 rows in set (0.00 sec)

alter mysql 设置字段编码规则 mysql设置字段类型_sql_06


2.修改字段类型,约束,顺序 第二种方法

alter table 表名 modify 字段 类型; #modify 不能修改字段名称
 mysql> alter table t3 modify maxs int(20) after math; #修改类型并更换位置

方法:删除字段

mysql> alter table g3 drop hunyin; 方法
 Query OK, 0 rows affected (0.03 sec)
 Records: 0 Duplicates: 0 Warnings: 0mysql> select * from g3;
 ±-----±-----±------±--------±----±-----±---------+
 | id | diqu | name | shengri | sex | age | jiaxiang |
 ±-----±-----±------±--------±----±-----±---------+
 | 1 | NULL | xtian | NULL | nan | NULL | NULL |
 | 2 | NULL | xli | NULL | nan | NULL | NULL |
 | 4 | NULL | xtian | NULL | nan | NULL | NULL |
 ±-----±-----±------±--------±----±-----±---------+
 3 rows in set (0.00 sec)

案例:测试

alter mysql 设置字段编码规则 mysql设置字段类型_字段_07

插入数据

字符串必须引号引起来
记录与表头相对应,表头与字段用逗号隔开。

1.添加一条记录

insert into 表名(字段1,字段2,字段3,字段4) values(1,“tom”,“m”,90);
 mysql> insert into t3(id,name,sex,age) values(1,“tom”,“m”,18);
 Query OK, 1 row affected (0.00 sec)


注:添加的记录与表头要对应,

2.添加多条记录

mysql> insert into t3(id,name,sex,age) values(2,“jack”,“m”,19),(3,“xiaoli”,“f”,20);
 Query OK, 2 rows affected (0.34 sec)

案例:测试

mysql> insert into table g3(id,name,sex) values(5,‘xiaom’,‘nan’); 添加一条

alter mysql 设置字段编码规则 mysql设置字段类型_mysql_08

mysql> insert into g3(id,name,sex) values(6,‘xiaoh’,‘nan’),(7,‘xiaol’,‘nan’); 添加多条

alter mysql 设置字段编码规则 mysql设置字段类型_sql_09


3.用set添加记录

mysql> insert into t3 set id=4,name=“zhangsan”,sex=“m”,age=21;
 Query OK, 1 row affected (0.00 sec)

alter mysql 设置字段编码规则 mysql设置字段类型_字段_10

更新记录

4.更新记录

update 表名 set 修改的字段 where 给谁修改;
 update g3 set name=‘lan’ where id=8;
 案例:
 mysql> update g3 set name=‘lan’ where id=8;
 Query OK, 1 row affected (0.00 sec)
 Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from g3;

案例;测试案例

alter mysql 设置字段编码规则 mysql设置字段类型_字段_11

5.删除记录

1.删除单条记录

mysql> delete from t3 where id=6; #删除那个记录,等于几会删除那个整条记录
 Query OK, 1 row affected (0.35 sec)

案例:测试

alter mysql 设置字段编码规则 mysql设置字段类型_sql_12


2.删除所有记录

mysql> delete from t3;

alter mysql 设置字段编码规则 mysql设置字段类型_sql_13