Mysql數據庫基礎(三) 數據表基本操作

#"xxx":表示數據表名
#數據表屬於數據庫,創建表之前先創建數據庫,並切換到指定數據庫,然後再創建數據庫

#創建數據表
create TABLE xxx (字段名,數據類型,字段名, 數據類型);
eg.
mysql> create table test (id INT(11), name VARCHAR(25));
Query OK, 0 rows affected (0.02 sec)

#展示當前數據庫中有哪些表
show tables;
eg.
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| test              |
+-------------------+

#查看數據表結構
 #1.describe(desc)語句可以查看錶的字段信息,包括:字段名、數據類型、是否爲主鍵、是否有默認值
 格式:
	describe(desc) xxx;
 eg.
 mysql> desc test1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(25) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

mysql> describe test1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(25) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

NULL:表示該列是否可以存儲NULL值
KEY:表示該列是否已編制索引。PRI表示是該列是主鍵的一部分;
							UNI表示該列是UNIQUE索引的一部分;
							MUL表示在列中某個定值允許出現多次
Default:表示該列是否有默認值,有的話顯示該值
Extra: 表示可以獲取的與給定列有關的附加信息,eg。AUTO_INCREMENT等

#查看錶詳細數據結構,格式:
show create table xxx(xxx\G)
#"\G":使顯示結果更加直觀
eg.

mysql> show create table test1\G;
*************************** 1. row ***************************
       Table: test1
Create Table: CREATE TABLE `test1` (
  `id` int(11) NOT NULL,
  `name` varchar(25) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

mysql> show create table test1;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                   |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------+
| test1 | CREATE TABLE `test1` (
  `id` int(11) NOT NULL,
  `name` varchar(25) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------+

#修改表名
格式:alter table old_xxx rename new_xxx;
eg.
mysql> alter table test1 rename test5;

#修改字段數據類型
格式:alter table xxx modify 字段名 數據類型;
eg.
mysql> desc test;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(25) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

mysql> alter table test modify name varchar(30);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(30) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

#修改字段名,同時可修改數據類型
格式:alter table xxx change 舊字段名 新字段名 數據類型;
eg.
mysql> desc test;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(30) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

mysql> alter table test change  name username varchar(30);         
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| username | varchar(30) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

#添加字段
格式:alter table xxx add 新字段名 數據類型 
	 約束條件(first | after 已存在字段名)#可以選擇性添加
eg.
mysql> desc test;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| username | varchar(30) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
1.添加無完整性約束條件的字段
mysql> alter table test add sex varchar(1);
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| username | varchar(30) | YES  |     | NULL    |       |
| sex      | varchar(1)  | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

2、有約束條件
eg.mysql> alter table test add sex varchar(1) not null;#表示新加的字段不能爲空

3、在表的第一列添加一個字段
eg.mysql> alter table test add sex varchar(1) first;

4、在表的指定列後添加一個字段
eg.mysql> alter table test add sex varchar(1) after id;#在id列之後添加性別列

#刪除字段
格式:alter table xxx drop 字段名;
eg.
mysql> alter table test drop sex;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| username | varchar(30) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

#修改字段的排列位置
格式:alter table xxx modify 字段1 數據類型 first|after 字段名2
"字段1":要修改位置的字段,"數據類型": 字段1的數據類型
"first": 將字段1修改爲表的第一個字段
"after 字段2": 將字段1插入到字段2後面
eg.
mysql> desc test;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| username | varchar(30) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

mysql> alter table test modify id int(11) after username;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(30) | YES  |     | NULL    |       |
| id       | int(11)     | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

mysql> desc test;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(30) | YES  |     | NULL    |       |
| id       | int(11)     | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

mysql> alter table test modify id int(11) first;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| username | varchar(30) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

#刪除數據表
1.刪除沒有被關聯的表
#可以同時刪除多個表,用","分割
"if exists":判斷表是否存在,表不存在會警告,但可以繼續執行不會結束
格式: drop table if exists 表名;
eg.
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| test              |
| test2             |
| test5             |
+-------------------+

mysql> drop table if exists test2,tset6,test5;
Query OK, 0 rows affected, 1 warning (0.04 sec)

mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| test              |
+-------------------+



#主鍵約束
主鍵約束要求主鍵的數據唯一,且不允許爲空,能唯一地標識表中的一條記錄,
結合外鍵來定義不同表之間的關係,加快數據查詢速度
 #1.單字段主鍵
 (1)在定義時指定主鍵,格式
	字段名 數據類型 PRIMARY KEY
 eg.
	mysql> create table test1(id int(11) primary key, name VARCHAR(25));   
 (2)在定義完所有列之後指定主鍵,格式
	PRIMARY KEY (字段名)
 eg.
	mysql> create table test2(id int(11), name VARCHAR(25), primary key(id));
 #2.多字段聯合主鍵,格式
	primary key(字段名,字段名)
 eg.
	mysql> create table test2(id int(11), name VARCHAR(25), primary key(id,name));

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章