1.數據庫創建,查詢,刪除
(1)創建一個test數據庫
CREATE DATABASE test?;
CREATE DATABASE IF NOT EXISTS test;
# default character set :默認字符集
CREATE DATABASE IF NOT EXISTS test?default character set UTF8;
# default collate:默認排序規格
# utf8_general_ci:不區分大小寫
# utf8_general_cs:區分大小寫?? ??? ??? ?
CREATE DATABASE IF NOT EXISTS tkjy default character set UTF8 default collate utf8_general_ci; ?
(2)切換數據庫
use test;
(3)查詢數據庫
show databases;?
# 如果有很多數據庫,模糊查詢某個數據庫
show databases like '%test%';
# 查詢創建數據庫的語句
show create database test;
# 更新數據庫選項信息(操作需要謹慎)
alter database test?character set gbk;
(4)刪除數據庫
drop database test;
drop database if exists test;
(5)使用mysqladmin工具創建、刪除數據庫
mysqladmin create test? -uroot -p123456
mysqladmin drop test? -uroot -p123456
2.mysql約束對應的五大關鍵詞
NOT NULL:?? ??? ?如果在列上定義了 not null,那么當插入數據時,必須為列提供數據
UNIQUE:?? ??? ???當定義了唯一約束后,該列值是不能重復的,但是可以為null
Primary Key:?? ?用于唯一的標識表行的數據,當定義主鍵約東后,該列不但不能重復而且不能為NULL。
Foreign Key:?? ?用于定義主表和從表之間的關系,外鍵約束要定義在從表上,主要則必須具有主鍵約束或是 uniques約束,當定義外鍵約束后,要求外鍵列數據必須在主表的主鍵列存在或是為NULL
CHECK:?? ??? ???用于強制行數據必須滿足的條件
3.創建表,修改表,刪除表等操作
(1)在test數據庫下面創建一張student學生表
use test;
create table if not exists student(id int(5) unsigned auto_increment primary key comment '學生表主鍵',name varchar(20) not null comment '學生姓名',age tinyint ?not null comment '學生年齡',admission_time datetime comment '入學時間',gender enum('男','女','保密') comment '學生性別',student_id int(10) UNIQUE comment '學生編號'
) engine=innodb ?default charset=utf8 ?comment '學生表';# auto_increment :?? ??? ??? ?主鍵自增(可選操作)
# engine :?? ? ? ? ?? ??? ??? 表使用存儲引擎(可選操作)
# comment :?? ? ? ? ?? ? ?? ? 注釋(可選操作)
# default charset :?? ??? ? 表的字符集(可選操作)
(2)使用like 關鍵字通過舊表創建新表 ( 包括舊表的 結構 + 信息 + 索引 )
create table <table_name> like <old_table_name>;
create table a like student;
(3)使用as 關鍵字通過舊表創建新表 ( 包括舊表的 結構 + 信息 )
create table <table_name> as select * from <old_table_name>;
create table b as select * from a;
(4)查看表、表結構、表創建語句
show tables;
show tables from test;?
show tables like '%stud%';
desc student;
show create table student \G;
(5)刪除表
drop table student;
(6)重命名表
rename table student to new_student;
(7)截斷表
truncate table new_student;
(8)修改表結構
alter table <table_name> add/drop/modify/change
# 增加列、增加主鍵
alter table new_student add student_from varchar(10) not null;
alter table new_student add (phone int unique not null,email varchar(20));
alter table new_student add primary key (id);
# 刪除列、刪除主鍵約束、刪除自增的主鍵約束
alter table new_student drop email;
alter table new_student drop primary key;
alter table new_student change id id int;?
alter table new_student drop primary key;
# 重命名列
alter table new_student change student_from st_from varchar(10);
# 修改表字段屬性
alter table new_student modify st_from varchar(15) unique ;
# 修改字符集,有數據不能改
alter table new_student character set gbk;
alter table new_student character set utf8;
(9)在test數據庫創建包含外鍵的員工表(YG)和工資表(gz)
# 創建工資表
create table if not exists test.gz(id int(5) primary key comment '工資表主鍵',salary int(7) not null comment '薪水',job varchar(10) ?not null comment '工作崗位',department varchar(5) not null comment '工作部門'
)?
engine=innodb ?default charset=utf8 ?comment '員工表';
# 工資表插入數據
insert into test.gz values (1,10000,'銷售','銷售部');
insert into test.gz values (2,15000,'Oracle DBA','技術部');
insert into test.gz values (3,20000,'mysql DBA','技術部');
insert into test.gz values (4,18000,'java','研發中心');
insert into test.gz values (5,30000,'C++','研發中心');
insert into test.gz values (6,16000,'python','研發中心');
commit;
# 查看工資
select * from test.gz;
# 創建員工表
create table if not exists test.yg(id int(5) unsigned auto_increment primary key comment '員工表主鍵',name varchar(7) not null comment '員工姓名',age tinyint ?not null comment '員工年齡',entry_time year comment '入職時間',gender enum('男','女','保密') comment '員工性別',gz_id int(5) not null,foreign key (gz_id) references tkjy.gz (id)
)?engine=innodb ?default charset=utf8 ?comment '員工表';
# 外鍵必須是主表的主鍵或者唯一鍵,如果是另外一張表主鍵的話,該表主鍵不允許帶有auto_increment 自增長屬性。
# 主表記錄刪除時 on delete cascade / 更新時的動作 on update cascade?
# 創建表以后再增加外鍵也可以
?alter table tkjy.yg add foreign key (gz_id) references test.gz (id);
#插入數據測試(在主表間鍵值內成功)
insert into test.yg values (1,'春野櫻',18,2015,'女',1);
insert into test.yg values (2,'漩渦鳴人',18,2016,'男',2);
insert into test.yg values (3,'宇智波佐助',18,2017,'男',2);
insert into test.yg values (4,'第一代火影',65,1970,'男',3);
insert into test.yg values (5,'第二代火影',60,1975,'男',4);
insert into test.yg values (6,'第三代火影',38,2000,'男',5);
insert into test.yg values (7,'第四代火影',38,2000,'男',6);
commit;
select * from test.yg;
#插入數據測試(在主表間鍵值外失敗)
insert into tkjy.yg values (8,'宇智波斑',28,2020,'男',7);
外鍵總結:mysql數據庫不建議使用外鍵、會極大影響數據庫運行性能(并發訪問)。