1. 程式人生 > >mysql資料庫與表的建立

mysql資料庫與表的建立

一、mysql資料庫的建立

1、建立資料庫

         建立資料庫語句:create database [IF NOT EXISTS] db_name [create_specification]

2、選項說明:

         IF NOT EXISTS:如果要建立的資料庫存在,建立時沒有加此語句會報錯。

         create_specification:指明建立資料庫的屬性

                   Character set屬性指明此資料庫的預設字符集

                   Collate屬性指明此資料庫的預設排序規則

3、建立資料庫

#建立名為test的資料庫
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
#再次建立時會報錯
mysql> create database test;
ERROR 1007 (HY000): Can't create database 'test'; database exists
#加” IF NOT EXISTS”後建立時不會報錯,會有告警
mysql> create database if not exists test;
Query OK, 1 row affected, 1 warning (0.00 sec)
#切換到剛建立的資料庫
mysql> use test;
Database changed

4、刪除資料庫

         命令:drop database database_name;

    Drop database語句用來刪除資料庫操作,既刪除了資料庫也刪除了庫裡的所有表,同時也會刪除該資料庫所在資料夾下的相關檔案。

二、mysql資料庫中表的建立

         建立完資料庫後需要建立資料表,建立資料表的過程是規定資料列的屬性的過程,同時也是實時資料完整性約束的過程。在建立表示需要先進入到建立的資料庫中。

1、建立表的語法格式

(1)建立表語發一:

         CREATE TABLE <表名>

         (

           欄位名1,資料型別 [列級別約束條件] [預設值],

           欄位名2,資料型別 [列級別約束條件] [預設值],

          ……

           [表級別約束條件]

         );

(2)建立表的語法2:

         CREATE [TEMPORARY] TABLE [IF NOT EXISTS] <表名>

         (

           [(create_definition,……)]

           [table_options]

           [partition_options]

           [IGNORE | REPLACE]

           [AS] query_expression

         );

使用這種方法建立時各欄位的說明如下:

         IF NOT EXISTS:當此表名存在時,不執行建立語句,同時也不報錯。

         TEMPORARY:表示建立的表為臨時表,臨時表僅對本連結可見,另外的資料庫連結不可見,當本連結斷開時,臨時表也自動被drop掉

         IGNORE | REPLACE:Ignore和replace表示在插入資料的過程中如果新表中碰到違反唯一約束的情況下怎麼處理,ignore表示不插入,replace表示替換已有的資料,預設兩個關鍵詞都不寫則碰到違反的情況會報錯

(3)建立表的語法3(建立表的同時將現有的表結構複製過來):

         CREATE TABLE <表名>

         (

           LIKE 現有的表名

         );

2、建立表

(1)建立普通表

# 在test庫中建立一張學生表,有兩個欄位,sid為整數;sname為字串,長度為12
mysql> create table student(sid int,sname varchar(12));
Query OK, 0 rows affected (0.65 sec)
# 檢視剛建立的表結構
mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid   | int(11)     | YES  |     | NULL    |       |
| sname | varchar(12) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (3.11 sec)
#建立表時也可在不進入庫時建立
mysql> create table test.student1(id int,name varchar(12));   
Query OK, 0 rows affected (0.14 sec)

(2)建立臨時表

# 在test庫下建立臨時表test123
mysql> create temporary table test.test1234(name varchar(12),age int(3));
Query OK, 0 rows affected (0.05 sec)

         臨時表只對當前的連線有效,當斷開當前連線時,臨時表會被drop掉,再次連線mysql時找不到臨時表。

(3)依據已有的表的結構建立表

# 將mysql庫中的user表結構複製到新建的testuser_copy表中
mysql> create table testuser_copy like mysql.user;    
Query OK, 0 rows affected (0.03 sec)
# 在建立表testuser2_copy時將查詢結果插入表中
mysql> create table testuser_copy2 as select user,host from mysql.user;     
Query OK, 8 rows affected (0.22 sec)
Records: 8  Duplicates: 0  Warnings: 0
# 查詢表testcopy2_user表中的資料
mysql> select * from testuser_copy2;
+---------------+---------------+
| user          | host          |
+---------------+---------------+
| root          | 192.168.%.%   |
| liuyi         | 192.168.1.100 |
……

(4)建立表時設定屬性

         Data_type:設定欄位的型別,如整型(int),字元創(varchar)

         Not null/null:設定欄位能否為空

         Default:設定欄位的預設值

         auto_increment:表示欄位為整數或者浮點數型別的value+1遞增數值,value為當前表中該欄位最大的值,預設是從1開始遞增;一個表中只容許有一個自增欄位,且該欄位必須有key屬性,不能含有default屬性,且插入負值會被當成很大的正數

# 建立表student3,id不能為空
mysql> create table student3(id int not null,name varchar(10));  
Query OK, 0 rows affected (0.43 sec)
# 建立表student4,age預設值為25
mysql> create table student4(id int not null,name varchar(12),age int default 25);
Query OK, 0 rows affected (1.65 sec)
# 建立帶有auto_increment欄位的表
mysql> create table student5(id int key auto_increment,name varchar(12)); 
Query OK, 0 rows affected (0.06 sec)
# 給表student5的name欄位插入一條資料 
mysql> insert into student5(name) values('dayi');
Query OK, 1 row affected (0.00 sec)
# id欄位會自動插入
mysql> select * from student5;
+----+------+
| id | name |
+----+------+
|  1 | dayi |
+----+------+
1 row in set (0.00 sec)

(5)建立約束的資料表

         Primary key:表示該欄位為主鍵,主鍵欄位必須唯一,必須非空,一個表中只能有一個主鍵,主鍵可以包含一個或多個欄位

# 建立主鍵約束的資料表(也可使用create table teacher(id int,name varchar(12),primary key(id));格式建立)
mysql> create table teacher(id int primary key,name varchar(12));
Query OK, 0 rows affected (0.08 sec)
# 建立多個鍵聯合主鍵約束表
mysql> create table teacher3(id int,name varchar(12),deptid int,primary key(id,deptid));
Query OK, 0 rows affected (0.01 sec)

         外來鍵用來在兩個表的資料之間建立連線,外來鍵可以是一列或者多列,一個表可以有一個或者多個外來鍵,一個表的外來鍵可以為空值,若不為空值,則每一個外來鍵的值必須等於另一個表中主鍵的某個值;定義外來鍵後,不允許刪除在另一個表中具有關聯關係的行。

         Key/index:表示索引欄位

         Unique:要求該列唯一,允許為空,但只能出現一個空值,唯一約束可以確保一列或者幾列不出現重複值。

         Foreign key:表示該欄位為外來鍵欄位

         Constraint:表示為主鍵、唯一鍵、外來鍵等約束條件命名,如果沒有命名則MySQL會預設給一個

         Column_format:目前僅在ndb儲存引擎的表上有用,表示該欄位的儲存型別是fixed, dynamic或者是default

         Storage:目前也僅在ndb儲存引擎的表上有用

# 先建立一個有主鍵約束的表gender
mysql> create table  gender(gender_id int(11) not null,name varchar(12) default null, primary key(gender_id));
Query OK, 0 rows affected (0.01 sec)
# 建立表student6,讓它的外來鍵作為關聯到表gender的gender_id
mysql> create table student6(id int primary key auto_increment,name varchar(12) unique,gender int,constraint gend_id foreign key(gender) references gender(gender_id)); 
Query OK, 0 rows affected (0.06 sec)

(6)檢視建立的表

         查看錶結構:desc tablename

         查看錶結構詳細語句:show create table tablename

# 查看錶student6結構
mysql> desc student6;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| name   | varchar(12) | YES  | UNI | NULL    |                |
| gender | int(11)     | YES  | MUL | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
# 檢視建立表student6的詳細結構語句
mysql> show create table student6;
+----------+--------------------------------------------------------------------
| Table    | Create Table
+----------+---------------------------------------------------------------------------
| student6 | CREATE TABLE `student6` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(12) DEFAULT NULL,
  `gender` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`),
  KEY `gend_id` (`gender`),
  CONSTRAINT `gend_id` FOREIGN KEY (`gender`) REFERENCES `gender` (`gender_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+----------+----------------------------------------------------------------------------------
1 row in set (0.00 sec)

(7)建立表綜合操作

         建立一個名為course的資料庫

         在庫中建立students表,包含的欄位有sid(整型自增主鍵),sname(64位元組字串),gender(12位字串),dept_id(整型並外來鍵到dept表的id欄位)。

         在庫中建立dept表,包含的欄位有id(整型自增主鍵),dept_name(64位字串)

         在庫中建立course表,包含的欄位有id(整型自增主鍵),course_name(64位字串),teacher_id(整型並外來鍵到teacher表的id欄位)

         在庫中建立teacher表,包含的主要欄位為id(整型自增欄位主鍵),name(字串64位),dept_id(整型並外來鍵到dept表的id欄位)

# 建立並進入到庫course
mysql> create database course;
mysql> use course;
#先建立dept表
mysql> create table dept(id int primary key auto_increment,dept_name varchar(64));
#建立student表
mysql> create table student(sid int primary key auto_increment,sname varchar(64),gender varchar(12),dept_id int not null,constraint deptid foreign key(dept_id) references dept(id));
# 建立teacher表
mysql> create table teacher(id int primary key auto_increment,name varchar(64),dept_id int not null,constraint dept_id foreign key(dept_id) references dept(id));
# 建立course表
mysql> create table course(id int primary key auto_increment,course_name varchar(64),teacher_id int,constraint teacher_id foreign key(teacher_id) references teacher(id));

(8)重命名錶

表建立完成後,如果建立錯了可以重命名錶,Rename table能將表中的資料,索引,主鍵定義都自動轉換到新表下,但檢視和對原表分配的許可權不能自動轉換到新表,需要手動執行。

# 對錶重新命名
mysql> rename table student_backup to student02;
Query OK, 0 rows affected (0.01 sec)

(9)刪除表

         命令:drop table [IF EXISTS] 表1,表2……;

         在刪除表時可以同時刪除多個表,如果刪除被外來鍵關聯的父表時會報錯;要刪除被關聯的父表時應該先刪除與父表關聯的字表,在刪除父表。

# 刪除表student
mysql> drop table student;
Query OK, 0 rows affected (0.12 sec)