欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mysql表的簡單操作(基本技能)

 更新時(shí)間:2025年04月02日 17:21:04   作者:貓咪-9527  
在數(shù)據(jù)庫中,表的操作主要包括表的創(chuàng)建、查看、修改、刪除等,了解如何操作這些表是數(shù)據(jù)庫管理和開發(fā)的基本技能,本文給大家介紹Mysql表的簡單操作,感興趣的朋友一起看看吧

在數(shù)據(jù)庫中,表的操作主要包括表的創(chuàng)建、查看、修改、刪除等。了解如何操作這些表是數(shù)據(jù)庫管理和開發(fā)的基本技能。

3.1 創(chuàng)建表

創(chuàng)建表時(shí),指定表的結(jié)構(gòu)、數(shù)據(jù)類型、字符集、校驗(yàn)規(guī)則和存儲引擎等。以下是創(chuàng)建表的詳細(xì)語法:

語法:

create table table_name (
    field1 datatype [constraint],
    field2 datatype [constraint],
    ...
) [character set charset_name] [collate collation_name] [engine storage_engine];
  • field: 表示列名(字段名)
  • datatype: 列的數(shù)據(jù)類型(如 int、varchar、date 等)
  • constraint: 可選的約束條件(如 primary key、not null、unique 等)
  • character set: 字符集(可選,如果未指定,使用數(shù)據(jù)庫的默認(rèn)字符集)
  • collate: 校驗(yàn)規(guī)則(可選,如果未指定,使用數(shù)據(jù)庫的默認(rèn)校驗(yàn)規(guī)則)
  • engine: 存儲引擎(如 innodb、myisam 等)

注:括號[]里面可以省略,自定義寫或者不寫。

 查看表的創(chuàng)建信息:

show create table user;

其中被圈起來的就是我們沒有寫,默認(rèn)被添加的。

示例:

create table user (
    id int primary key, 
    name varchar(20) not null comment '用戶名', 
    password char(32) not null comment '密碼是32位的md5值', 
    birthday date comment '生日'
) character set utf8 collate utf8_general_ci engine=myisam;

作用:

  • 創(chuàng)建一個(gè)名為 users 的表,包含 id、namepasswordbirthday 字段。
  • 設(shè)置字符集為 utf8,校驗(yàn)規(guī)則為 utf8_general_ci,存儲引擎使用 myisam。

說明

  • primary key:定義主鍵,確保該字段唯一且不為空。
  • not null:字段不能為空。
  • comment:給字段加注釋,便于理解字段用途。

存儲引擎說明

不同存儲引擎具有不同的特性:

  • myisam:適用于讀取較多的場景,存儲和索引分離。
  • innodb:支持事務(wù),行級鎖,外鍵約束,適用于高并發(fā)寫操作。

 3.2 查看表結(jié)構(gòu)

語法:

desc 表名;

或者

show columns from table_name;
  • table_name: 表的名稱

使用規(guī)則:

  • describe: 顯示表的字段信息,包括字段名、數(shù)據(jù)類型、是否允許 null 值、鍵的類型(如 primary key、index 等)及其他信息。
  • show columns: 和 describe 類似,但輸出格式可能略有不同。

示例:

describe user;

3.3 修改表

1. 添加字段

語法:

alter table table_name add column column_name datatype 
[constraint] [after existing_column];

注:mysql支持分段書寫指令 

  • table_name: 表的名稱
  • column_name: 新增字段的名稱
  • datatype: 字段的數(shù)據(jù)類型
  • constraint: 可選約束,指定列的規(guī)則,如 not null、default
  • existing_column: 如果指定,將字段添加到該字段之后

使用規(guī)則:

  • add column: 添加新字段時(shí),可以指定新字段的默認(rèn)值、約束條件以及字段的位置(是否放在已有字段后面)。

示例:

alter table user add assets varchar(100) comment '圖片路徑' after birthday;

作用:

users 表中添加名為 assets 的字段,用于保存圖片路徑,數(shù)據(jù)類型為 varchar(100),并放置在 birthday 字段后。

2. 修改字段

語法:

alter table table_name modify column column_name datatype [constraint];
  • column_name: 要修改的字段名稱
  • datatype: 新的數(shù)據(jù)類型
  • constraint: 可選約束

使用規(guī)則:

  • modify column: 修改現(xiàn)有字段的類型或約束條件??梢愿淖償?shù)據(jù)類型、長度、是否允許 null 等。
  • 修改字段時(shí),字段位置不能更改。

示例:

alter table users modify name varchar(60) not null;

作用:

  • 修改 users 表中 name 字段的長度為 60,并設(shè)置為 not null。 

3. 刪除字段

語法:

alter table table_name drop column column_name;
  • column_name: 要?jiǎng)h除的字段名稱

使用規(guī)則:

  • drop column: 刪除字段時(shí)會丟失該字段的數(shù)據(jù),操作前請確保備份。

示例:

drop table user drop asserts;

作用:

  • 刪除 user 表中的 asserts 字段。

4.修改字段名

語法:

alter table table_name change column old_column_name new_column_name datatype [constraint];
  • table_name: 表的名稱
  • old_column_name: 要修改的字段的原名稱
  • new_column_name: 修改后的字段名稱
  • datatype: 字段的數(shù)據(jù)類型(必須重新指定)
  • constraint: 可選的約束(如 not null、unique 等)

使用規(guī)則:

  • change column: 用于修改字段名稱,同時(shí)可以修改字段的數(shù)據(jù)類型和約束。修改時(shí),必須提供字段的完整數(shù)據(jù)類型,即使只是修改名稱,也需要指定數(shù)據(jù)類型。
  • 注意:在修改字段名時(shí),不能省略數(shù)據(jù)類型和原字段名稱。

示例:

alter table users change column name xingming varchar(60) not null;

作用:

  • users 表中的 name 字段重命名為 xingming,并將數(shù)據(jù)類型修改為 varchar(60),且字段不能為空(not null)。

5. 修改表名

語法:

alter table old_table_name rename to new_table_name;
  • old_table_name: 原表名
  • new_table_name: 新表名

使用規(guī)則:

  • rename to: 修改表名時(shí),要保證新名稱沒有與現(xiàn)有表重名。

6.刪除表 

drop table [表名];

3.4 實(shí)踐案例:修改表

假設(shè)我們有一個(gè) users 表,包含以下字段:id、name、passwordbirthday

1. 插入數(shù)據(jù)

insert into users (id, name, password, birthday) 
values (1, 'a', 'b', '1982-01-04'),
       (2, 'b', 'c', '1984-01-04');

2. 查看表結(jié)構(gòu)

describe users;

3. 添加字段:添加 assets 字段,保存圖片路徑:

alter table users add assets varchar(100) comment '圖片路徑' after birthday;

4. 修改字段:修改 name 字段的長度:

alter table users modify name varchar(60);

5. 刪除字段:刪除 password 字段:

alter table users drop password;

6. 修改表名:將表名 users 改為 employee

alter table users rename to employee;

7. 最終查看表結(jié)構(gòu)

describe employee;

注: 

  • 創(chuàng)建表:創(chuàng)建表時(shí),確保字段類型、約束條件、字符集和存儲引擎的選擇合適。
  • 修改表:通過 alter table 命令,可以添加、刪除、修改字段,甚至修改表名。
  • 刪除表:使用 drop table 命令刪除表,操作時(shí)要格外小心。

注意:所有的修改表結(jié)構(gòu)的操作都會影響數(shù)據(jù)完整性,因此在進(jìn)行這些操作時(shí),務(wù)必先做好備份。

到此這篇關(guān)于Mysql表的簡單操作的文章就介紹到這了,更多相關(guān)Mysql表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論