maridb資料庫表及欄位增刪改
阿新 • • 發佈:2021-10-04
mariadb資料型別
mariadb資料型別分為數字、日期、時間以及字串值。
適用型別原則:夠用就行,儘量使用範圍小的,而不用大的
常用資料型別:
1、整數 int,bit #例如 年紀 適用於必須是整數的
2、小數 decimal # 例如 身高、收入 #decimal(5,2) 5位數保留小數點後兩位 decimal(7,1)7位數保留小數點後兩位
3、字串 char,varchar (可定義字元) #例如姓名 、班級、籍貫等等,也可以整數、小數等資料型別 但是不能函式運算
4、日期時間 date,time,datetime
5、列舉型別 enum #可指定的型別 例如 性別
約束:
主鍵primary key:物理上儲存的順序
非空not null:此欄位不能為空
唯一unique:此欄位不允許重複
預設default:當不填寫此值時會使用預設值,如果填寫則已填寫為準
外來鍵foreign key:對關係欄位進行約束,當為關係欄位填寫值時,會到關聯的表中查詢此值是否存在,如果存在則填寫成功,如果不存在則填寫失敗並丟擲異常
建立一張class表 (id,name,age)
MariaDB [testdb]> create table class(id int primary key not null auto_increment,name varchar(20), age tinyint unsigned); Query OK, 0 rows affected (0.00 sec)
在class表中插入資料 insert into
MariaDB [testdb]> insert into class(name,age) values('李志鋒',29); Query OK, 1 row affected (0.00 sec)
檢視class表 select
MariaDB [testdb]> select * from class; +----+-----------+------+ | id | name | age | +----+-----------+------+ | 1 | 李志鋒 | 29 | +----+-----------+------+ 1 row in set (0.00 sec)
查看錶結構 desc
MariaDB [testdb]> desc class; +-------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | +-------+---------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
修改表-新增欄位 --alter table 表名 add 列名 型別;
MariaDB [testdb]> alter table class add gender enum ('男','女','保密'); Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [testdb]> desc class; +--------+----------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+----------------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | | gender | enum('男','女','保密') | YES | | NULL | | +--------+----------------------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
修改表-修改表字段型別不重名版本 --alter table 表名 modify 列名 型別及約束;
MariaDB [testdb]> alter table class modify gender enum('男','女','保密') default'保密'; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [testdb]> desc class; +----------+----------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | | gender | enum('男','女','保密') | YES | | 保密 | | | birthday | datetime | YES | | NULL | | +----------+----------------------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec)
修改表-修改表字段名稱、型別--alter table 表名 change 原名 新名 型別及約束;
MariaDB [testdb]> alter table class change birthday birth date; Query OK, 1 row affected (0.02 sec) Records: 1 Duplicates: 0 Warnings: 0 MariaDB [testdb]> desc class; +--------+----------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+----------------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | | gender | enum('男','女','保密') | YES | | 保密 | | | birth | date | YES | | NULL | | +--------+----------------------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec)
修改表-刪除欄位 -- alter table 表名 drop 列名;
MariaDB [testdb]> alter table class drop gender; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [testdb]> desc class; +-------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | | birth | date | YES | | NULL | | +-------+---------------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
修改表內容
--update 表名 set 列1=值1, 列2=值2... where 條件;
例: 將class表中的綠帽子改成紅帽
MariaDB [testdb]> select * from class;
+----+-----------+------+-------+------+
| id | name | age | birth | high |
+----+-----------+------+-------+------+
| 1 | 李志鋒 | 29 | NULL | NULL |
| 2 | 綠帽子 | 19 | NULL | NULL |
| 3 | 王晶 | 29 | NULL | NULL |
+----+-----------+------+-------+------+
3 rows in set (0.00 sec)
MariaDB [testdb]> update class set name=('紅帽') where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [testdb]> select * from class;
+----+-----------+------+-------+------+
| id | name | age | birth | high |
+----+-----------+------+-------+------+
| 1 | 李志鋒 | 29 | NULL | NULL |
| 2 | 紅帽 | 19 | NULL | NULL |
| 3 | 王晶 | 29 | NULL | NULL |
+----+-----------+------+-------+------+
3 rows in set (0.00 sec)
刪除表內容
delete from 表名 where 條件
例:將class表中的紅帽刪除
MariaDB [testdb]> delete from class where name=('紅帽'); Query OK, 1 row affected (0.00 sec) MariaDB [testdb]> select * from class; +----+-----------+------+-------+------+ | id | name | age | birth | high | +----+-----------+------+-------+------+ | 1 | 李志鋒 | 29 | NULL | NULL | | 3 | 王晶 | 29 | NULL | NULL | +----+-----------+------+-------+------+ 2 rows in set (0.00 sec)