創建數據庫
create database|schema [if not exists] db_name [character set= ] [collate= ]
注:schema可以理解為方案或者數據庫,與database一個意義
例創建一個students數據庫,且默認字符集為‘gbk’、默認排序為'gbk_chinese_ci';
create schema if not exists students character set 'gbk' collate 'gbk_chinese_ci';
刪除數據庫
drop {database|schema} [if exists] db_name;
創建表的方式
直接創建一個空表
從其他表中查詢出數據,并以此創建一個有數據的表
以其他表為模板創建一個空表
create table [if not exists] tb.name (字段名稱1 字段定義 約束 索引,字段名稱2 字段定義 約束 索引)
例:
mysql> create table tb1 (id int unsigned not null auto_increment primary key,name char(20) not null,age tinyint not null) engine='engine_name';
主鍵還可以如下單獨定義
mysql> create table tb1 (id int unsigned not null auto_increment ,name char(20) not null,age tinyint not null,primary key(id));
也可以將多個字段一起作為主鍵
mysql> create table tb1 (id int unsigned not null auto_increment ,name char(20) not null,age tinyint not null,primary key(id,name));
注:任何一個auto_increment字段都必須定義為主鍵
將id定義為主鍵,name定義為唯一鍵,age定義為索引
mysql> create table tb1 (id int unsigned not null auto_increment ,name char(20) not null,age tinyint not null,primary key(id),unique key (name) index(age));
mysql> show tables ;
+--------------------+
| Tables_in_students |
+--------------------+
| tb1 |
+--------------------+
1 row in set (0.00 sec)
mysql> select id,name,age from tb1;
鍵是一種特殊的索引,其數值不能相同,而索引允許出現相同值,稱鍵為約束,屬于B+樹索引結構
索引類型有兩種
BTREE索引
HASH索引
每個字段都支持B樹索引,但不一定支持HASH索引
mysql> create table corses (cid tinyint unsigned not null auto_increment ,course varchar(50) not null,primary key(cid));
Query OK, 0 rows affected (0.15 sec)
mysql> show table status like 'corses'\G
*************************** 1. row ***************************
Name: corses
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: 1
Create_time: 2017-02-12 10:45:11
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
注:創建表時如果未指定字符集,排序規則等,則從數據庫繼承;而數據庫創建時指定或者采取默認
向表中插入及查看數據 insert into 和select
例:
mysql> insert into corses (course) values ('kuihuabaodian'),('jiuyingzhenjing'),('rulaishezhang'); ##向corses表的courses字段插入數據
Query OK, 3 rows affected (0.06 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from corses; #查看
+-----+-----------------+
| cid | course |
+-----+-----------------+
| 1 | kuihuabaodian |
| 2 | jiuyingzhenjing |
| 3 | rulaishezhang |
+-----+-----------------+
3 rows in set (0.00 sec)
show index from tb_name; 顯示表的索引
例:
mysql> show index from corses\G
*************************** 1. row ***************************
Table: corses 表名
Non_unique: 0 是否為唯一主鍵;0表示是唯一鍵,1表示不是唯一鍵
Key_name: PRIMARY 鍵名
Seq_in_index: 1 這個表的第1個索引,一個表中可以第一多個索引
Column_name: cid 索引在哪個字段上(cid)
Collation: A 排序規則
Cardinality: 3
Sub_part: NULL 索引長度
Packed: NULL
Null:
Index_type: BTREE 索引類型
Comment:
Index_comment:
1 row in set (0.00 sec)
從以張表中查找出數據并建立一個新表
mysql> create table testcourses select * from corses where cid<2;
Query OK, 1 row affected (0.12 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from testcourses;
+-----+---------------+
| cid | course |
+-----+---------------+
| 1 | kuihuabaodian |
+-----+---------------+
1 row in set (0.00 sec)
mysql> create table testcourses select * from corses where cid<3;
ERROR 1050 (42S01): Table 'testcourses' already exists
mysql> desc courses;
ERROR 1146 (42S02): Table 'students.courses' doesn't exist
mysql> desc testcourses;
+--------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| cid | tinyint(3) unsigned | NO | | 0 |
| course | varchar(50) | NO | | NULL |
+--------+---------------------+------+-----+---------+-------+
2 rows in set (0.05 sec)
mysql> desc corses;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra
+--------+---------------------+------+-----+---------+----------------+
| cid | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| course | varchar(50) | NO | | NULL |
+--------+---------------------+------+-----+---------+----------------
用dessc查看兩張表的結構,可以看出不一樣
當從一張表中復制數據時某些字段會消失
以一個表為模板創建一個結構一樣的空表
mysql> create table test like corses;
Query OK, 0 rows affected (0.16 sec)
mysql> desc test;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| cid | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| course | varchar(50) | NO | | NULL |
+--------+---------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> desc corses;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| cid | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| course | varchar(50) | NO | | NULL |
+--------+---------------------+------+-----+---------+----------------+
desc顯示兩個表的格式相同
注:
所以要想復制一張表中的數據,最好先依據原表創建一個完全相同的空表,在用insert命令導入原表的數據
修改表
alter table tb_name(添加,刪除,修改字段,修改索引,改表名,修改表屬性)
例
mysql> alter table test add unique key(course);
為test表添加一個唯一鍵(course)
mysql> alter table test change course Course varchar(50) not null;
將course字段(屬性)改為Course 且為varchar(50) 不能為空
新增一個日期字段
例:
mysql> alter table test add starttime date default '2017-2-12';
mysql> desc test;
+-----------+---------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+------------+----------------+
| cid | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| Course | varchar(50) | NO | UNI | NULL | |
| starttime | date | YES | | 2017-02-12 | |
+-----------+---------------------+------+-----+------------+----------------
修改表名test為mytest
mysql> alter table test rename mytest;
刪除表
mysql> create table hehe (sid int unsigned not null auto_increment primary key,name varchar(30),cid int not null);
Query OK, 0 rows affected (0.09 sec)
mysql> insert into hehe (name,cid) values ('jiamian',2),('zxl',1);
mysql> select * from hehe;
+-----+---------+-----+
| sid | name | cid |
+-----+---------+-----+
| 1 | jiamian | 2 |
| 2 | zxl | 1 |
+-----+---------+-----+
2 rows in set (0.00 sec)
mysql> select * from Courses;
ERROR 1146 (42S02): Table 'students.Courses' doesn't exist
mysql> select * from corses;
+-----+-----------------+
| cid | course |
+-----+-----------------+
| 1 | kuihuabaodian |
| 2 | jiuyingzhenjing |
| 3 | rulaishezhang |
+-----+-----------------+
3 rows in set (0.00 sec)
做兩表的條件顯示
mysql> select name,course from hehe,corses where hehe.cid=corses.cid;
+---------+-----------------+
| name | course |
+---------+-----------------+
| zxl | kuihuabaodian |
| jiamian | jiuyingzhenjing |
+---------+-----------------+
2 rows in set (0.01 sec)
添加外鍵約束
外鍵約束只能添加在支持事物的存儲引擎上,且存儲引擎要一樣
外鍵約束的關聯的兩個字段類型要一樣
mysql> alter table corses engine=innodb; 修改引擎
mysql> alter table hehe modify cid tinyint unsigned not null; 修改字段類型一樣
mysql>alter table hehe add foreign key foreign_cid (cid) references corses (cid);
將hehe表的cid字段與corses表的cid字段關聯一起建立一個外鍵約束,外鍵名稱為foreign_cid
mysql> create table test1 (cid int unsigned not null auto_increment primary key,name varchar(50) not null,sid char not null);
mysql> insert into test1 (cid,name,sid) values (1,'zxl','A'),(2,'jiamian','B'),(3,'fade','C');
mysql> create table test2 (cid int unsigned not null auto_increment primary key,name varchar(50));
mysql> insert into test2 (cid,name) values (1,'hehe'),(2,'haha');
mysql> alter table test1 add foreign key foreign_cid (cid) references courses(cid);
索引:可以創建,查看,刪除,不可以修改
create index index_name on tb_name(字段)using BTREE/HASH;
且可以 (字段(length)desc|asc )
length表示索引長度、占的字符數;
asc表示創建索引后按照升序排,desc表示按照降序排
mysql> create index name_on_student on test1(name) using BTREE;
在test1表的name字段創建索引,并且為BTREE索引
mysql> show index from test1\G
*************************** 1. row ***************************
Table: test1
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: cid
Collation: A
Cardinality: 3
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: test1
Non_uniqu
Key_name: name_on_student
Seq_in_index: 1
Column_name: name
Collation: A
Cardinality: 3
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment
mysql> drop index name_on_student ontest1;
在test1表的name字段創建一個長度為5,且降序排列的BTREE索引
mysql> create index name_on_student on test1(name(5) desc) using BTREE;
mysql> show index from test1\G
*************************** 1. row ***************************
Table: test1
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: cid
Collation: A
Cardinality:
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: test1
Non_unique: 1
Key_name: name_on_student 索引名稱
Seq_in_index: 1 這個表的第一個索引
Column_name: name 索引所在的字段(name)
Collation: A
Cardinality: 3
Sub_part: 5 索引長度為5
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。