mysql練習之一:資料表的基本操作
已經學習了MySQL的各種操作,例如建立表格、新增各種限制、產看表結構、以及修改和刪除表。給出一個實戰演練,全面複習一下資料表的基本操作基礎。
案例:建立資料庫company,依照下面兩個表給出的表結構在company資料庫中建立兩個資料表offices和employees,按照操作程序完成資料表的基本操作。
(免費學習推薦:mysql影片教學)
操作过程如下:
(1):登入MySQL。
mysql -h localhost -u root -p
開啟windows命令列,輸入登入使用者名稱和密碼:
C:\Users\Hudie>mysql -h localhost -u root -p Enter password: ********Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 19Server version: 8.0.16 MySQL Community Server - GPL Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>_
登入成功,可以輸入SQL語句來操作。
(2):建立資料庫company。
create database company;
mysql> create database company;Query OK, 1 row affected (0.06 sec)
建立成功後,在company資料庫中建立資料表,必須先選擇該資料庫。 SQL語句如下:
mysql> use company;Database changed
(3):建立表格offices。
create table offices
mysql> create table offices -> ( -> officeCode int(10) not null unique, -> city varchar(50) not null, -> address varchar(50) not null, -> country varchar(50) not null, -> postalCode varchar(15) not null, -> primary key (officeCode) -> );Query OK, 0 rows affected (0.14 sec)mysql> show tables;+-------------------+| Tables_in_company |+-------------------+| offices |+-------------------+1 row in set (0.00 sec)
(4):建立表格enployees。
create table employees
mysql> create table employees -> ( -> employeeNumber int(11) not null primary key auto_increment, -> lastNamee varchar(50) not null, -> firstName varchar(50) not null, -> mobile varchar(25) not null, -> officeCode int (10) not null, -> jobTitle varchar(50) not null, -> birth datetime, -> noth varchar(25), -> sex varchar(5), -> constraint office_fk foreign key(officeCode) references offices(officeCode) -> );Query OK, 0 rows affected (0.14 sec)mysql> show tables;+-------------------+| Tables_in_company |+-------------------+| employees || offices |+-------------------+2 rows in set (0.01 sec)
建立成功,查看兩個表的結構:
mysql> desc offices;+------------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+------------+-------------+------+-----+---------+-------+| officeCode | int(10) | NO | PRI | NULL | || city | varchar(50) | NO | | NULL | || address | varchar(50) | NO | | NULL | || country | varchar(50) | NO | | NULL | || postalCode | varchar(15) | NO | | NULL | |+------------+-------------+------+-----+---------+-------+5 rows in set (0.06 sec)mysql> desc employees;+----------------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------------+-------------+------+-----+---------+----------------+| employeeNumber | int(11) | NO | PRI | NULL | auto_increment || lastNamee | varchar(50) | NO | | NULL | || firstName | varchar(50) | NO | | NULL | || mobile | varchar(25) | NO | | NULL | || officeCode | int(10) | NO | MUL | NULL | || jobTitle | varchar(50) | NO | | NULL | || birth | datetime | YES | | NULL | || noth | varchar(25) | YES | | NULL | || sex | varchar(5) | YES | | NULL | |+----------------+-------------+------+-----+---------+----------------+9 rows in set (0.01 sec)
(5):將表格employees的mobile欄位修改到officeCode欄位後面。
alter table employees modify mobile varchar(25) after officeCode;
mysql> alter table employees modify mobile varchar(25) after officeCode;Query OK, 0 rows affected (0.18 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc employees;+----------------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------------+-------------+------+-----+---------+----------------+| employeeNumber | int(11) | NO | PRI | NULL | auto_increment || lastNamee | varchar(50) | NO | | NULL | || firstName | varchar(50) | NO | | NULL | || officeCode | int(10) | NO | MUL | NULL | || mobile | varchar(25) | YES | | NULL | || jobTitle | varchar(50) | NO | | NULL | || birth | datetime | YES | | NULL | || noth | varchar(25) | YES | | NULL | || sex | varchar(5) | YES | | NULL | |+----------------+-------------+------+-----+---------+----------------+9 rows in set (0.01 sec)
(6):將表格employees的birth欄位改名為employee_birth。
alter table employees change birth employee_birth datetime;
mysql> alter table employees change birth employee_birth datetime;Query OK, 0 rows affected (0.03 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc employees;+----------------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------------+-------------+------+-----+---------+----------------+| employeeNumber | int(11) | NO | PRI | NULL | auto_increment || lastNamee | varchar(50) | NO | | NULL | || firstName | varchar(50) | NO | | NULL | || officeCode | int(10) | NO | MUL | NULL | || mobile | varchar(25) | YES | | NULL | || jobTitle | varchar(50) | NO | | NULL | || employee_birth | datetime | YES | | NULL | || noth | varchar(25) | YES | | NULL | || sex | varchar(5) | YES | | NULL | |+----------------+-------------+------+-----+---------+----------------+9 rows in set (0.00 sec)
(7):修改sex字段,設定資料類型為char(1),非空約束。
alter table employees modify sex char(1) not null;
mysql> alter table employees modify sex char(1) not null;Query OK, 0 rows affected (0.20 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc employees;+----------------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------------+-------------+------+-----+---------+----------------+| employeeNumber | int(11) | NO | PRI | NULL | auto_increment || lastNamee | varchar(50) | NO | | NULL | || firstName | varchar(50) | NO | | NULL | || officeCode | int(10) | NO | MUL | NULL | || mobile | varchar(25) | YES | | NULL | || jobTitle | varchar(50) | NO | | NULL | || employee_birth | datetime | YES | | NULL | || noth | varchar(25) | YES | | NULL | || sex | char(1) | NO | | NULL | |+----------------+-------------+------+-----+---------+----------------+9 rows in set (0.01 sec)
(8):刪除欄位noth。
alter table employees drop noth;
mysql> alter table employees drop noth;Query OK, 0 rows affected (0.15 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc employees;+----------------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------------+-------------+------+-----+---------+----------------+| employeeNumber | int(11) | NO | PRI | NULL | auto_increment || lastNamee | varchar(50) | NO | | NULL | || firstName | varchar(50) | NO | | NULL | || officeCode | int(10) | NO | MUL | NULL | || mobile | varchar(25) | YES | | NULL | || jobTitle | varchar(50) | NO | | NULL | || employee_birth | datetime | YES | | NULL | || sex | char(1) | NO | | NULL | |+----------------+-------------+------+-----+---------+----------------+8 rows in set (0.01 sec)
(9):增加欄位名稱favoriate_activity,資料型別為varchar(100)
alter table employees add favoriate_activity varchar(100);
mysql> alter table employees add favoriate_activity varchar(100);Query OK, 0 rows affected (0.09 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc employees;+--------------------+--------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+--------------------+--------------+------+-----+---------+----------------+| employeeNumber | int(11) | NO | PRI | NULL | auto_increment || lastNamee | varchar(50) | NO | | NULL | || firstName | varchar(50) | NO | | NULL | || officeCode | int(10) | NO | MUL | NULL | || mobile | varchar(25) | YES | | NULL | || jobTitle | varchar(50) | NO | | NULL | || employee_birth | datetime | YES | | NULL | || sex | char(1) | NO | | NULL | || favoriate_activity | varchar(100) | YES | | NULL | |+--------------------+--------------+------+-----+---------+----------------+9 rows in set (0.00 sec)
(10 ):刪除主表offices
①刪除表的外鍵限制:alter table employees drop foreign key office_fk;
②刪除表offices:drop table offices;
mysql> alter table employees drop foreign key office_fk;Query OK, 0 rows affected (0.03 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> drop table offices;Query OK, 0 rows affected (0.03 sec)mysql> show tables;+-------------------+| Tables_in_company |+-------------------+| employees |+-------------------+1 row in set (0.06 sec)
(11):修改表格employees儲存引擎為MyISAM。
alter table employees ENGINE=MyISAM;
mysql> alter table employees ENGINE=MyISAM;Query OK, 0 rows affected (0.17 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> show create table employees \G*************************** 1. row *************************** Table: employeesCreate Table: CREATE TABLE `employees` ( `employeeNumber` int(11) NOT NULL AUTO_INCREMENT, `lastNamee` varchar(50) NOT NULL, `firstName` varchar(50) NOT NULL, `officeCode` int(10) NOT NULL, `mobile` varchar(25) DEFAULT NULL, `jobTitle` varchar(50) NOT NULL, `employee_birth` datetime DEFAULT NULL, `sex` char(1) NOT NULL, `favoriate_activity` varchar(100) DEFAULT NULL, PRIMARY KEY (`employeeNumber`), KEY `office_fk` (`officeCode`)) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.00 sec)
(12)將表employees名稱修改為employees_info。
alter table employees rename employees_info;
mysql> alter table employees rename employees_info;Query OK, 0 rows affected (0.07 sec)mysql> show tables;+-------------------+| Tables_in_company |+-------------------+| employees_info |+-------------------+1 row in set (0.00 sec)
相關免費學習推薦:#mysql資料庫##(影片)
以上是mysql練習之一:資料表的基本操作的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MySQL是一種常見的關聯式資料庫,是許多網站和應用程式的核心元件。隨著資料量越來越大,如何優化MySQL的效能就變得尤為重要。其中一個關鍵領域是資料表的壓縮,在本文中我們將介紹MySQL中的資料表壓縮技術。壓縮表和非壓縮表MySQL中有兩種類型的資料表:壓縮表和非壓縮表。非壓縮表是MySQL預設的表類型,它使用固定長度的行格式,對資料進行儲存。這意味著數據

MySQL是一款非常受歡迎的開源關係型資料庫管理系統,它支援完整的DDL(資料定義語言)操作。 DDL是用來定義和管理資料庫中各種資料物件的語言,包括資料表、檢視和索引等。對於資料庫管理員和開發人員而言,熟練MySQL中資料表DDL操作技術非常重要。本文將詳細介紹MySQL中資料表DDL操作的技術與方法,並提供實際操作範例。一、建立資料表建立資料表是DDL中

MySQL修改資料表:1.先檢視資料庫中所有的表,代碼為:「SHOW TABLES;」;2、修改表名,代碼為:「ALTER TABLE 舊表名 RENAME [TO] 新表名;」。 3.檢查表名是否修改成功,代碼為:“SHOW TABLES;”

MySQL是一種開源關係型資料庫管理系統,它的基本功能在資料庫設計、資料儲存和管理方面非常優秀。在MySQL中,資料表是資料儲存的最基本單元。在實際應用中,資料表的重載是一種非常常見的操作技巧,它可以幫助我們提高資料庫的運作效率,並提升系統的穩定性。本文將從MySQL中資料表重載的概念、原理和實務應用等方面詳細介紹這項操作技巧。一、什麼是資料表重載資料表重載是

利用MySQL的AVG函數計算資料表中數字列的平均值方法簡介:MySQL是一種開源的關聯式資料庫管理系統,擁有豐富的內建函數來處理和計算資料。其中,AVG函數是用來計算數字列的平均值的函數。本文將介紹如何使用AVG函數來計算MySQL資料表中數字列的平均值,並提供相關的程式碼範例。一、建立範例資料表首先,我們需要建立一個範例資料表來進行示範。假設我們有一個名為

如何利用thinkorm實作資料表之間的關聯查詢引言:在進行資料庫開發中,常常會碰到需要在多個資料表之間進行關聯查詢的情況。利用thinkorm這個優秀的資料庫ORM框架,可以輕鬆實現資料表的關聯查詢,提高開發效率。本文將介紹如何利用thinkorm實作資料表之間的關聯查詢,並提供程式碼範例幫助讀者更好地理解。一、基本概念在進行關聯查詢之前,首先需要了解th

如何實現MySQL底層最佳化:資料表的水平和垂直分割策略,需要具體程式碼範例引言:在大型應用場景下,MySQL資料庫經常面臨著大量資料的儲存和查詢壓力。為了解決這個問題,MySQL提供了資料表的分割策略,包括水平分割(HorizontalPartitioning)和垂直分割(VerticalPartitioning)。本文將介紹如何實作MySQL底層最佳化,重

MySQL中如何使用MAX函數來找到資料表中最大的數值引言:在MySQL中,我們經常需要對資料表進行各種查詢和分析,其中包括找出資料表中的最大數值。使用MAX函數可以輕鬆找到資料表中的最大值,並且在進一步處理資料時非常有用。本文將介紹如何使用MAX函數來找出資料表中最大的數值,並給出對應的程式碼範例。一、MAX函數簡介MAX函數是MySQL中的一個聚合函數,用
