-- 啟動數(shù)據(jù)庫服務(wù)
net start mysql57;
net stop mysql57;
-- 創(chuàng)建數(shù)據(jù)庫
create database mydb(數(shù)據(jù)庫的名字)
create database mydb charset gbk;//創(chuàng)建數(shù)據(jù)庫并指定字符集
-- 顯示數(shù)據(jù)庫
show databases //顯示全部數(shù)據(jù)庫
匹配模式:
show databases like _d;//匹配當(dāng)前位置單子字符
show databases like %d;//匹配指定位置多個字符
show create database mydb;//顯示數(shù)據(jù)庫創(chuàng)建語句
use mydb;(數(shù)據(jù)庫的名字)//選擇數(shù)據(jù)庫
alter database mydb charset gbk;//修改數(shù)據(jù)庫字符集
drop database mydb;(數(shù)據(jù)庫的名字)//刪除數(shù)據(jù)庫
-- 表
-- 創(chuàng)建表 兩種寫法
use mydb;
create table class(name varchar(10));//class 表名 name 字段名
或者create table mydb.class(name varchar(10));
create database if not exists mydb;//如果不存在創(chuàng)建 存在就不創(chuàng)建了 相當(dāng)于容錯
-- 復(fù)制已有的表
create table 新表名 like 表名 //只能復(fù)制結(jié)構(gòu) 不能復(fù)制數(shù)據(jù)
create table new_emp select *from emp;//不僅能復(fù)制結(jié)構(gòu)也能復(fù)制數(shù)據(jù)
-- 顯示所有表
show tables;
-- 顯示表結(jié)構(gòu)
desc emp(表名);
describe emp;
show columns from emp;
-- 顯示表創(chuàng)建語句
show create table class(表名);
-- 更改表的字符集
alter table class charset gbk;
-- 修改表的名字
rename table emp to new_emp;
-- 更改表 新增一個字段
alter table emp add deptno int;//在emp表中加入deptno字段
-- 更改表中的字段名
alter table emp change deptno deptno1 int;//把表emp中字段deptno 改為deptno1
-- 更改表中的字段的類型
alter table emp modify deptno TINYINT;// 將表emp的字段deptno字段類型改了
-- 刪除表中的字段
alter table emp drop deptno;//將emp 表中的字段deptno刪掉
-- 刪除表
drop table emp;//刪除表emp
drop table emp,emp1;//刪除多張表
-- 向emp表中添加數(shù)據(jù)
insert into emp vaules();
insert into emp vaules(20,30);//插入數(shù)據(jù)時沒有指定表中的具體字段慨蓝,值列表需要和表結(jié)構(gòu)相對應(yīng)
insert into emp(age,deptno) values(5,40);//插入數(shù)據(jù)時指定表中的具體字段(1個或多個),值列表需要和表定義的字段相對應(yīng)贾漏,而不用和表結(jié)構(gòu)對應(yīng)
-- 查詢表中的數(shù)據(jù)
select *from emp;
select age from emp;//查看表中某一具體字段
-- 修改表中的數(shù)據(jù)
update emp set age=100 where deptno=1;//update...set....
-- 刪除表中的某個字段所對應(yīng)的行數(shù)據(jù)
delete from deptno where age=10;
-- 查看字符集
show variables like '%character%';
-- 更改字符集
set character_set_server =utf8;