1.創建數據庫
mysql> show databases;
mysql> create database ranzai;
mysql> drop database ranzai;
mysql> select database();
mysql> use ranazi;
--------------------------------------
2.查看數據引擎:
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
3.查看錶
mysql> show tables;
Empty set (0.00 sec)//是個空表
數據表的數據類型
4.DDL 數據表
5.數據表操作:
1)主要操作:創建、修改、刪除;
2)定義信息:表名、字段名、字段類型;
6.MySQL的建表語法
Create [TEMPORARY] TABLE [IF NOT EXISIT]table_name[(create_definition,..)][table options][select_statemennt]
1)TEMPORARY:表示創建臨時表,在當前會話結束後自動消失;
2)IF NOT EXISIT:在建表之前,先判斷表是否存在,只有在表不存在的時候創建;
3)create_definition:建表語句的關鍵部分,用於定義表中各列的屬性;
4)table_option:表的配置選項,如表的默認存儲殷勤、字符集;
5)select_statemennt:通過select 語句建表
7.創建表
mysql> create table ran(
-> id int primary key,
-> name varchar (30),
-> phone varchar(11)
-> );
Query OK, 0 rows affected (0.28 sec)
8.更改表的結構
1)添加字段
mysql> desc ran;//查看原表ran的數據結構
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| phone | varchar(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> alter table ran add sex char(1);//對已經存在的表ran添加sex字段,數據類型爲char,限制爲1位
Query OK, 0 rows affected (0.35 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc ran;//查看新增字段後的表ran的數據結構
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| phone | varchar(11) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+//新增了sex字段,數據類型爲char(1)
4 rows in set (0.01 sec)
2)修改字段數據類型
alter table [表名] modify 字段 數據類型(位數);
mysql> alter table ran modify sex char(2);
3)刪除字段
alter table [表名] drop column 字段;
mysql> alter table ran drop column sex;
4)刪除表
drop table [表名];
mysql> drop table ran2;