SQL Mysql表結構操作

2022-06-25 21:12:13 字數 4155 閱讀 2828

本篇內容是關於 基本的資料庫操作,建表,表結構修改等內容;

學習本篇的基礎是知識追尋者以前發布的文章:

《sql-你真的了解什麼sql麼?》

《sql-小白最佳入門sql查詢一》

《sql-小白最佳入門sql查詢二》

《sql- 多年開發人員都不懂的插入與更新刪除操作注意點》

《sql-sql事物操作》

《sql-mysql資料型別》

知識追尋者(inheriting the spirit of open source, spreading technology knowledge;)

sql 對資料庫的操作分為如下三種型別,如果都學會這三種sql語言熟練對資料庫操作,說明你已經對資料庫登堂入室,如果再學會資料庫高階操作,說明你對資料庫就有一定的使用經驗,如果你還學會對資料庫進行優化,分表分庫,讀寫分離等操作,說明你使用資料庫已經到專家級別;

ddl:資料定義語言(data define language),即定義資料的結構。比如:create,drop,alter操作

dcl:資料控制語言(data control language),對許可權、事務等的控制。比如:grant(授權)revoke(取回許可權),commit,roolback等; 事物在上篇已經說明,不會在本篇提及;

資料庫的基本操作如下,也就是我們日常使用的操作

mysql -h 192.168.0.127 -p 3306 -u root -p root
檢視當前資料庫:

select database();
顯示使用者活動執行緒:

show processlist;
顯示系統變數:

show variables;
顯示當前時間,使用者,資料庫版本號

select now(), user(), version();
建立資料庫:create database[ if not exists] 資料庫名 [資料庫選項]

資料庫選項:

character set 字符集名稱

collate 排序規則名稱

示例:create database zszxz;

刪除資料庫: drop database [ if exists] 資料庫名;

drop  database zszxz;
資料庫表的日常操作如下

create [temporary] table[ if not exists] [庫名.]表名 ( 表的結構定義 ) [ 表選項]
其中 temporary 表示臨時表;中括號內容都表示可選,在正規的資料庫版本管理開發會經常使用到;

欄位的修飾如下 資料型別

表選項一般就是指定資料庫引擎和字符集:

engine=innodb default charset=utf8 comment='顧客表';
示例

create table if not exists `customer` (

`id` int(11) not null auto_increment comment '主鍵',

`customer_name` varchar(255) default null comment '顧客名稱',

`gender` varchar(255) default null comment '性別',

`telephone` varchar(255) default null comment '**號碼',

`register_time` timestamp null default null comment '註冊時間',

primary key (`id`)

) engine=innodb default charset=utf8 comment='顧客表';

檢視所有表

show tables
檢視指定資料庫的表

show tables from 資料庫名稱;

示例:show tables from zszxz;

刪除表

drop table[ if exists] 表名;

示例: drop table op;

清空表(清除資料)

truncate [table] 表名
複製表結構

create table 表名 like 要複製的表名;

示例: create table op like `order`;

複製表結構和資料

create table 表名 [as] select * from 要複製的表名;

示例: create table op as select * from `order`;

常見的alter操作如下:

增加一列(追加至末尾)

alter table [資料庫名.]表名 add [column] 字段 資料型別;

示例: alter table `order` add column `year` year ;

增加到第一列

alter table [資料庫名.]表名 add [column] 字段 資料型別 first;
增加一列到指定欄位名後

alter table [資料庫名.]表名 add [column] 字段 資料型別 after 另乙個字段;
修改欄位名的 資料型別

alter table [資料庫名.]表名稱 modify [column] 欄位名 新的資料型別;

示例: altert table `order` modify column `gender` tinyint;

修改表字段的資料型別,並且移動至第一列

alter table [資料庫名.]表名稱 modify [column] 欄位名 資料型別 first;
修改表字段的資料型別,並且移動至指定字段後面

alter table [資料庫名.]表名稱 modify [column] 欄位名 資料型別 after 另乙個欄位名;
修改表字段的名稱

alter table [資料庫名.]表名稱 change [column] 舊欄位名 新的欄位名 資料型別;
新增主鍵

alter table [資料庫名.]表名稱 add primary key(欄位名);

示例: altert table `order` add primary key(`id`)

新增唯一鍵

alter table [資料庫名.]表名稱 add unique [索引名] (欄位名)
新增索引

alter table [資料庫名.]表名稱 add index [索引名] (欄位名)
刪除一列

alter table [資料庫名.]表名稱 drop [column] 欄位名;

示例:altert table `order` drop column `gender`;

刪除索引

alter table [資料庫名.]表名稱 drop index 索引名
刪除主鍵

alter table [資料庫名.]表名稱 drop primary key
刪除外來鍵

alter table [資料庫名.]表名稱 drop foreign key 外來鍵
關注知識追尋者:

表結構操作

1 複製表結構及資料到新錶 create table 新錶select from 舊表 這種方法會將oldtable中所有的內容都拷貝過來,當然我們可以用delete from newtable 來刪除。不過這種方法的乙個最不好的地方就是新錶中沒有了舊表的primary key extra auto...

表結構操作

查詢使用者 scott 下所有的表名 select table name from all tables where owner scott 查詢表 emp所有欄位的資訊 select from all tab columns where table name emp 表中的索引列 select f...

sql 表結構操作

新建表 create table 表名 自動編號字段 int identity 1,1 primary key 欄位1 nvarchar 50 default 預設值 null 欄位2 ntext null 欄位3 datetime,欄位4 money null 欄位5 int default 0,...