mysql 表相关操作

mysql 表的相关操作

创建表

表的操作需要依赖于库在创建表的时候需要先指定数据库

指定数据库

use 数据库名;

$ use mydatabase;

mysql> use mydatabase;
Database changed
mysql>

创建数据表

格式

create table [if not exists] 表名(
     字段名 数据类型,
     字段名 数据类型
     )[库选项];

注意事项 创建时必须指定数据库 字段与字段直接以符号逗号隔开 ( , )最后一个字段不加符号

$  create table if not exists mytabale(
     id int,
     name varchar(12)
     )charset utf8;

mysql> create table if not exists mytabale(
    -> id int,
    -> name varchar(12)
    -> )charset utf8;
Query OK, 0 rows affected (0.07 sec)

第二种方式 

create table [if not exists] 数据库名.表名(
     字段名 数据类型,
     字段名 数据类型
     )[库选项];

这里不演示  这样可以直接指定创建到那个数据库中 

查看数据表

查看所有表

show tables;

$ show tables;

mysql> show tables;
+----------------------+
| Tables_in_mydatabase |
+----------------------+
| mytabale             |
+----------------------+
1 row in set (0.00 sec)

模糊查询

show table like 'my%';

show table like 'mytab_e';

$ show tables like  'my%';

mysql> show tables like  'my%';
+----------------------------+
| Tables_in_mydatabase (my%) |
+----------------------------+
| mytabale                   |
+----------------------------+
1 row in set (0.00 sec)
$ show tables like 'mytaba_e';

mysql> show tables like 'mytaba_e';
+---------------------------------+
| Tables_in_mydatabase (mytaba_e) |
+---------------------------------+
| mytabale                        |
+---------------------------------+
1 row in set (0.00 sec)

查看表结构

desc/ describe/show columns from 表名;

$ desc mytabale;

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

修改表

修改表可以分为两个部分 

1表本身的一些数据 如表选项

2表内部的一些属性比如字段

表本身修改

修改表名

格式 rename table 旧表名 to 新表名;

rename table mytabale to mytable;

$ rename table mytabale to mytable;

mysql> rename table mytabale to mytable;
Query OK, 0 rows affected (0.02 sec)

修改表选项 

alter table 表名 charset utf8;

$ alter table mytable charset utf8;

mysql> alter table mytable charset utf8;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

新增字段

字段可修改 新增 重命名  删除

新增字段

完整格式

alter table 表名 [column] 字段名  数据类型 [列属性] [位置]

位置

  first :第一个位置 

after :在哪个字段之后  after 字段名 ; 默认的在最后一个字段之后

alter table mytable add age varchar(20);

$ alter table mytable add age varchar(20);

mysql> alter table mytable add age varchar(20);
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

//查看下表中字段已经添加

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

如果没有加 数据类型 (varchar(20))会报以下错误;

mysql> alter table mytable add age; --没有加数据类型
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

修改字段 

alter table 表名 modify  字段名  数据类型  [列属性] [位置]

alter table table modify age int after id;

$ alter table mytable modify age int after id;

mysql> alter table mytable modify age int after id;
Query OK, 0 rows affected (0.13 sec)
Records: 0  Duplicates: 0  Warnings: 0

//查看age的数据 类型 位置已经发生改变了

mysql> desc mytable;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
| name  | varchar(12) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

重命名字段

alert table 表名 change 旧字段名 新字段名 数据类型 [属性] [位置];

演示:alter table mytable change age gender int;

$ alter table mytable change age gender int;

mysql> alter table mytable change age gender int;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

-- 查看 age已经发生改变 数据类型也可在此修改
mysql> desc mytable;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | YES  |     | NULL    |       |
| gender | int(11)     | YES  |     | NULL    |       |
| name   | varchar(12) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

删除字段

在删除字段之前先插入一条无用的字段

alter table 表名 drop 字段名;

演示 alter table mytable drop sex;

$ alter table mytable drop sex;
-- 执行前后的变化 sex被删除了

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

mysql> alter table mytable drop sex;
Query OK, 0 rows affected (0.10 sec)
Records: 0  Duplicates: 0  Warnings: 0


mysql> desc mytable;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | YES  |     | NULL    |       |
| gender | int(11)     | YES  |     | NULL    |       |
| name   | varchar(12) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql>

注意: 如果表中以有数据执行删除字段操作那么表中该列的数据会被清空(删除不可逆)

删除表

 删除表前先准备一张无用表

drop table 表名 ,表名1,表名2;-- 可以同时删除多张数据表;

演示  drop table class;

$ drop table class;

mysql> show tables;
+----------------------+
| Tables_in_mydatabase |
+----------------------+
| class                |
| mytable              |
+----------------------+
2 rows in set (0.00 sec)

mysql> drop table class;
Query OK, 0 rows affected (0.02 sec)

mysql> show tables;
+----------------------+
| Tables_in_mydatabase |
+----------------------+
| mytable              |
+----------------------+
1 row in set (0.00 sec)

注意 删除不可逆 ;

数据操作

新增数据

insert into 表名 values (值1,值2,值3)[,(值列表二)];可以同时添加多条数据与表中字段一一对应;

insert into 表名(字段列表)values(值1,值2,值3)[,(值列表二)]; 与字段列表一一对应;

演示

$ insert into mytable values(1,1,'大聖');

mysql> insert into mytable values(1,1,'大聖');
Query OK, 1 row affected (0.00 sec)

注意 name 字段是varchar类型的所以要使用(“”) 数据库中单引号与双引号等价

如果没使用不正确的类型添加会出以下错误

mysql> insert into mytable values(1,1,大聖);
ERROR 1054 (42S22): Unknown column '大聖' in 'field list'

演示

$ insert into mytable(id,name) values(1,'大圣');

mysql> insert into mytable(id,name) values(1,'大圣');
Query OK, 1 row affected (0.01 sec)

查看数据

select */字段名 from 表名 [where 条件];

演示

$ select * from mytable;

mysql> select * from mytable;
+------+--------+------+
| id   | gender | name |
+------+--------+------+
|    1 |      1 | 大聖 |
|    1 |   NULL | 大圣 |
+------+--------+------+
2 rows in set (0.00 sec)

演示

$ select name from mytable;

mysql> select name from mytable;
+------+
| name |
+------+
| 大聖 |
| 大圣 |
+------+
2 rows in set (0.00 sec)

更新数据

update 表名 set 字段=值[where 条件]

演示

$ update mytable set name = '孙悟空' where gender=1;

mysql> update mytable set name = '孙悟空' where gender=1;
Query OK, 1 row affected (0.00 sec)

mysql> select * from mytable;
+------+--------+--------+
| id   | gender | name   |
+------+--------+--------+
|    1 |      1 | 孙悟空 |
|    1 |   NULL | 大圣   |
+------+--------+--------+
2 rows in set (0.00 sec)

注意where不加会修改全部数据

删除数据

delete from 表名 [where 条件];

演示

$ delete from mytable where gender =1;


mysql> delete from mytable where gender =1;
Query OK, 1 row affected (0.00 sec)

mysql> select * from mytable;
+------+--------+------+
| id   | gender | name |
+------+--------+------+
|    1 |   NULL | 大圣 |
+------+--------+------+
1 row in set (0.00 sec)

Web乱码问题

动态网站由三部分构成: 浏览器, web服务器,数据库服务器, 三个部分都有自己的字符集(中文), 数据需要在三个部分之间来回传递: 很容易产生乱码.

 

如何解决乱码问题: 统一编码(三码合一)

 

但是事实上不可能: 浏览器是用户管理(根本不可能控制).

但是必须要解决这些问题: 主要靠web端代码来转换

猜你喜欢

转载自blog.csdn.net/weixin_39434788/article/details/84571136