mysql的教學 MySQL的簡單實用 手把手教學

2021-10-22 09:55:27 字數 2191 閱讀 1899

mysql的使用

1.登陸資料庫

開啟terminal

在終端根檔案目錄下輸入/usr/local/mysql/bin/mysql -u root -p

接著輸入管理員密碼

2.顯示所有的資料庫

輸入:mysql> show databases;

(不要忘記加 『 ; 』 ,在mysql中分號為一句語句的結束)

顯示結果:

| database |

| information_schema |

| mysql |

| performance_schema |

| studb |

| sys |

5 rows in set (0.06 sec)

3.使用資料庫

在上面的顯示的資料庫中,使用studb資料庫,輸入:

mysql> use studb;

若沒有建立過資料庫,則可以使用以下命令來建立資料庫:

mysql> create database studb;

4.展示資料庫中所有的表

輸入:mysql> show tables;

顯示結果:

| tables_in_studb |

| student |

1 row in set (0.00 sec)

可以看到資料庫有一張表,若沒有建立表,則可以使用下面的命令建立:

mysql> create table student(

->stu_id int primary key not null,

->stu_name char(25) not null,

->stu_email char(25) null,

->stu_address char(50) null);

5.列印表的結構

輸入:mysql> desc student;

顯示結果:

| field | type | null | key | default | extra |

| stu_id | int | no | pri | null | |

| stu_name | char(25) | no | | null | |

| stu_email | char(25) | yes | | null | |

| stu_address | char(50) | yes | | null | |

4 rows in set (0.00 sec)

6.向表中插入資料

輸入:mysql> insert into student(stu_id, stu_name, stu_email, stu_address)

-> values(2020100116,'張三','[email protected]','beijin');

7.檢視表中資料

輸入:mysql> select *

-> from student;

顯示結果:

| stu_id | stu_name | stu_email | stu_address |

| 2020100116 | 張三 | [email protected] | beijin |

1 row in set (0.00 sec)

8.修改表中的資料

輸入:mysql> update student

-> set stu_email = '[email protected]'

-> where stu_id = 2020100116;

更新成功後,檢視更新結果:

mysql> select stu_id, stu_name, stu_email

-> from student

-> where stu_id = 2020100116;

顯示結果:

| stu_id | stu_name | stu_email |

| 2020100116 | 張三 | [email protected] |

1 row in set (0.00 sec)

9.刪除表中的資料

輸入:mysql> delete from student where stu_id = 2020100116;

------------恢復內容結束------------

mysql約束的操作 MySQL中的約束簡單使用

資料庫約束是為了保證資料的完整性而實現的一套機制,它具體的根據各個不同的資料庫的實現而有不同的工具.一般來說有以下幾種實現方式 1 檢查約束 通過在定義資料庫表裡,在字段級或者是在表級加入的檢查約束,使其滿足特定的要求.比如以下的表定義 crate table student id serial,n...

mysql簡書 mysql使用

mysql對大小寫不敏感 1.使用者管理 使用者的建立和授權 mysql 8.0.11 版本之後建立使用者方法如下 create user laowang localhost identified by 123456 或grant usage on to user01 localhost ident...

mysql簡單例子 mysql 儲存過程的簡單例子

mysql 儲存過程的簡單例子 定義新的語句分隔符 delimiter create procedure pd016 begin 本儲存過程主要實現以下操作 1.查詢遊戲表的資料 2.根據遊戲表資料去獲取型別表的資訊 建立乙個臨時表 create temporary table if not exi...