MySQL數(shù)據(jù)庫之數(shù)據(jù)表操作
一、創(chuàng)建數(shù)據(jù)表
1、基本語法
create table 表名 ( 字段名 字段類型 [字段屬性], 字段名 字段類型 [字段屬性], ... ) [表選項];
需要注意:表需要放在對應的數(shù)據(jù)庫下面
2、創(chuàng)建方式一
-- 先選擇數(shù)據(jù)庫 use mydatabase; -- 創(chuàng)建數(shù)據(jù)表 create table user( name varchar(10) );
3、創(chuàng)建方式二
-- 直接將數(shù)據(jù)表掛到數(shù)據(jù)庫下 create table mydatabase.user( name varchar(10) );
4、表選項
- Engine 存儲引擎
- Charset 字符集
- Collate 校對集
指定表的字符集
create table user( name varchar(10) ) charset utf8;
5、復制已有表結(jié)構
create table 表名 like 表名; -- eg 從test數(shù)據(jù)庫復制表 create table user like test.user;
二、顯示數(shù)據(jù)表
-- 顯示所有表 mysql> show tables; +----------------------+ | Tables_in_mydatabase | +----------------------+ | t_author | | user | +----------------------+ -- 顯示匹配表 mysql> show tables like '%author'; +--------------------------------+ | Tables_in_mydatabase (%author) | +--------------------------------+ | t_author | +--------------------------------+
三、顯示表結(jié)構
基本語法:
desc 表名(常用); describe 表名; show columns from 表名;
示例:
mysql> desc user; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 1 row in set (0.00 sec) mysql> describe user; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 1 row in set (0.01 sec) mysql> show columns from user; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 1 row in set (0.01 sec)
字段含義:
Field 字段名 Type 字段類型 Null 是否允許為空 Key 索引 Default 默認值 Extra 額外的屬性
四、顯示表創(chuàng)建語句
基本語法:
show create table 表名;
示例:
mysql> show create table user; +-------+----------------+ | Table | Create Table | +-------+----------------+ | user | CREATE TABLE `user` ( `name` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci | +-------+----------------+ 1 row in set (0.00 sec)
語句結(jié)束符:
;
\g
效果一樣,字段在上,數(shù)據(jù)在下\G
字段在左,數(shù)據(jù)在右
mysql> show create table user\G *************************** 1. row *************************** Table: user Create Table: CREATE TABLE `user` ( `name` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci 1 row in set (0.00 sec)
五、設置表屬性
- engine
- charset
- collate
-- 基本語法 alter table 表名 表選項 [=] 值; -- eg 修改表的字符集 alter table user charset gbk;
如果數(shù)據(jù)表已經(jīng)有數(shù)據(jù),不要輕易修改表屬性
六、修改表結(jié)構
1、修改表名
--基本語法 rename table 舊表名 to 新表名; -- eg: rename table user to tb_user;
2、新增字段
-- 基本語法 alter table 表名 add [column] 字段名 字段類型 [字段屬性] [位置first/after 字段名]; mysql> desc user; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ -- 給學生表新增age字段,默認加到表的最后 mysql> alter table tb_user add age int; mysql> desc tb_user; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ -- 在最前面增加一個id字段 mysql> alter table tb_user add id int first; mysql> desc tb_user; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
字段位置:
first 放在最前名 alter 字段名 放在某個字段后面
3、修改字段名
-- 基本語法 alter table 表名 change 舊字段名 新字段名 字段類型 [字段屬性] [新位置] -- 將age字段修改為old mysql> alter table tb_user change age old int; mysql> desc tb_user; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(10) | YES | | NULL | | | old | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
4、修改字段屬性
-- 基本語法 alter table 表名 modify 字段名 新字段類型 [新字段屬性] [新位置] -- eg 將name的長度由10修改為20 mysql> alter table tb_user modify name varchar(20); mysql> desc tb_user; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(20) | YES | | NULL | | | old | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
5、刪除字段
-- 基本語法 alter table 表名 drop 字段名 -- eg 刪除old字段 alter table tb_user drop old;
七、刪除表結(jié)構
-- 基本語法, 可以同時刪除多個表 drop table 表名 [, 表名...]; -- eg: 刪除 tb_user表 mysql> drop table tb_user; mysql> show tables; +----------------------+ | Tables_in_mydatabase | +----------------------+ | t_author | +----------------------+
到此這篇關于MySQL數(shù)據(jù)庫之數(shù)據(jù)表操作的文章就介紹到這了,更多相關MySQL數(shù)據(jù)表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決MySQL報錯Error 3948 (42000): Loading loc
在執(zhí)行MySQL項目過程中意外出現(xiàn)的報錯,之前也沒有遇到過,報錯信息如下,Error 3948 (42000): Loading local data is disabled; this must be enabled on both the client an,本文小編就給大家介紹一下解決報錯的方法,需要的朋友可以參考下2023-09-09MySQL如何導入csv格式數(shù)據(jù)文件解決方案
本文將詳細介紹MySQL如何導入csv格式數(shù)據(jù)文件并提供詳細解決方案,需要了解的朋友可以參考下2012-11-11Mysql啟動與數(shù)據(jù)庫的創(chuàng)建方法[圖文]
Mysql啟動與數(shù)據(jù)庫的創(chuàng)建方法,mysql數(shù)據(jù)庫入門2012-03-03MySQL中的datediff()方法和timestampdiff()方法的應用示例小結(jié)
在MySQL中,DATEDIFF()函數(shù)和TIMESTAMPDIFF()函數(shù)用于計算日期和時間之間的差異,TIMESTAMPDIFF()函數(shù)返回的結(jié)果是整數(shù),但你可以通過在計算過程中使用適當?shù)某▉慝@得所需的小數(shù)部分,本文介紹MySQL中的datediff()方法和timestampdiff()方法的應用,感興趣的朋友一起看看吧2023-12-12