MySql 庫 表級操作 及 資料型別

2022-08-02 18:36:13 字數 2465 閱讀 9892

資料庫分類

關係型資料庫(sql):儲存方式固定,安全

非關係型資料庫(nosql):儲存方式比較靈活,儲存資料的效率比較高,不太安全

mysql是一種關係型資料庫管理系統(採用關係模型來組織管理資料的資料庫系統)

注意事項

大小寫:不嚴格區分,預設大寫為程式**,小寫為程式設計師寫的**

語句結束符:每個語句都以;或者\g結束

型別:強制資料型別,任何資料都有自己的資料型別

逗號:建立表的時候最後一行不需要逗號

進入與退出

進入:mysql –uusername -ppassword    mysql -uusername -p 回車再輸入密碼,密碼不可見

退出:exit

庫級操作

建立庫:       create database [if not exists] 庫名;    重複建立會報錯, 可以加上if not exists

刪除庫:       drop database [if exists] db_name;     如果不知道資料庫是否存在,記得加 if exists

使用資料庫:     use db_name;

顯示所有的庫:    show databases;

查詢當前使用的庫:  select database();

表級操作

顯示所有的表:    show tables;

建立表:       create table [if not exists] 表名(id int ,name varchar(20).....)  重複建立會報錯, 可以加上if not exists

顯示建立表的資訊:  show create table tb_name;

刪除表:       drop table tb_name

表中資料的操作

增(insert into values)

指定字段插入:       insert into tb_name(field_name)  values (field_values);

全欄位插入:           insert into tb_name values (all_values);

多條插入:         insert into tb_name(field_name) values (value_1), (value_2), …;

刪(delete from where)

注意:一定要寫where條件,不然會刪除表中全部資料

刪除表中所有資料:     delete  from  tb_name;

刪除表中滿足條件的資料:  delete  from  tb_name  where  conditions;

改(update set where)

注意:一定要寫where條件,不然會修改表中全部資料

修改所有資料:         update  tb_name  set field_1=value_1 

修改多個:           update  tb_name  set field_1=value_1, field_2=value_2 …  where conditions; 

修改滿足條件的資料:      update  tb_name  set field_1=value_1  where  conditions; 

查(select from where)

指定字段查詢:     select field_names from tb_name;

全欄位查詢:      select * from tb_name;

帶條件的查詢:       select field_names from tb_name where conditions; 

資料型別

數值型別

int            四個位元組(0,4294967295)

float(m,n)        單精度浮點型(4個位元組)

double(m,n)       雙精度浮點型,m總個數,d小數字(8位元組)

不常用:        tinyint、samllint、mediumint、bigint

字元型別

char(size)         儲存固定長度的字串(可包含字母、數值以及特殊字元)。在括號中指定字串的長度。最多255個字元

varchar(size)       儲存可變長度的字串(可包含字母、數值以及特殊字元)。在括號中指定字串的長度。最多255個字元。如果值的長度大於255,則被轉換為text型別

不常用:            tinytext/tinyblob、text/blob、longtext/longblob、enum(列舉)

時間日期型別

date:日期       格式:2019-04-16

time:時間       格式:09:32:43

datetime:日期時間   格式:2019-04-16 09:32:43

timestamp:自動儲存記錄修改的時間

year:存放年

MySql資料型別及建立庫操作

登陸mysql伺服器 mysql u username ppwd 檢視所有的資料庫 show databases 建立資料庫 create dadtabase dbname 使用 切換資料庫 use dbname 檢視正在使用的資料庫 select database 刪除資料庫 drop datab...

MySql資料庫之表操作及基本資料型別

1.建立表 create table 表名 列名1 資料型別1,列名2 資料型別2.charset 字符集 collate 校驗規則 engine 搜尋引擎 字符集和校驗規則還有搜尋引擎可以不顯示給出,mysql預設的是utf8字符集和utf8 general ci字元校驗規則和myisam搜尋引擎...

資料型別及資料表的操作

原則 選擇最合適,而不是最大的資料格式 整形浮點型 日期時間型 字元型開啟資料庫 use db name 檢視資料庫資訊 show database 建立資料表 create table if not exists table name column name data type,檢視資料表 sho...