數(shù)據(jù)庫(kù)操作捎废、數(shù)據(jù)表操作、字段操作總結(jié)
################################ 數(shù)據(jù)庫(kù) #################################
-- 查看用戶(hù)可以使用的數(shù)據(jù)庫(kù)拓春,mysql是必須的狸演,它記錄用戶(hù)訪(fǎng)問(wèn)權(quán)限
show databases;
-- 創(chuàng)建數(shù)據(jù)庫(kù)
create database mydatabase default CHARACTER set utf8;
-- 刪除數(shù)據(jù)庫(kù)
drop database mydatabase;
-- 查看指定的數(shù)據(jù)庫(kù)
show create database mydatabase;
-- 選擇使用的數(shù)據(jù)庫(kù)
use mydatabase;
-- 查看引擎
show ENGINES;
################################ 數(shù)據(jù)表 #################################
-- 創(chuàng)建數(shù)據(jù)表 db_1
create table db_1(
id int(11) primary key auto_increment,
name varchar(25),
sex BOOLEAN,
salary float
);
-- 刪除表
drop table if exists db_1;
-- 查看該數(shù)據(jù)庫(kù)所有的數(shù)據(jù)表
show tables;
-- 查看指定表詳細(xì)創(chuàng)建結(jié)構(gòu)
show create table db_1;
-- 主表
create table tb_dept_1(
id int(11) primary key,
name varchar(22) not null,
location varchar(50)
);
-- 從表
create table db_5(
id int(11) primary key,
name varchar(25),
deptId int(11),
salary float ,
constraint fk_emp_dept1 foreign key(deptId) references tb_dept_1(id)
);
-- 查看表基本結(jié)構(gòu)
desc db_5;
-- 修改表名
alter table db_5 rename tb_new;
-- 刪除表
drop table if exists tb_new;
drop table tb_new;
################################ 數(shù)據(jù)字段 #################################
-- 修改字段數(shù)據(jù)類(lèi)型
alter table tb_new modify name varchar(50);
-- 修改字段名
alter table tb_new change name deptname varchar(35);
-- 添加字段
alter table tb_new add createtime datetime ;
-- 刪除字段
alter table tb_new drop createtime;
-- 修改字段排序位置
alter table tb_new modify salary float first;
alter table tb_new modify salary float after deptId;
-- 更改表的存儲(chǔ)引擎
alter table db_1 engine=MyISAM;
-- 刪除表的外鍵約束(表中有數(shù)據(jù)也可以刪除索引)
alter table tb_new drop foreign key fk_emp_dept1;
思維導(dǎo)圖總結(jié):