目录
启动/关闭数据库
-
方法1:指令启动
以管理员身份允许cmd
启动 :net start mysql
关闭 :net stop mysql
- 方法2:鼠标启动
右键我的电脑 -> 管理 -> 服务 ->启动/关闭
数据库建立
-
建立数据库:
creat database 表名;
creat database studentsdb;
-
查询以存在的数据库:
show databases;
show databases;
-
修改数据库中的编码信息(默认utf-8):
alter database 库名 character set = gbk;
alter database studentsdb charater set = gbk;
-
删除数据库:
drop database 库名;
drop database studentsdb;
-
切换数据库:
use 库名;
use studentsdb;
表的建立
-
建表:
creat table 表名(
标题名 类型 约束条件
标题名 类型 约束条件
);
creat table student_info(
学号 char(4) not null primary key,
姓名 char(8) not null,
性别 char(2) null,
出生日期 date null,
家庭住址 varchar(50) null
);
-
查询该数据库中的所有表:
creat tables;
creat tables;
-
查询表结构
show columns from 表名;
show columns from student_info;
-
向表中插入数据
insert into 表名(标题名,标题名,标题名,标题名,标题名)
value(对应标题的数据,对应标题的数据~~~);
insert into student_info(学号,姓名,性别,出生日期,家庭住址)
value
('0001','张青平','男','2000-10-01','衡阳市东风路77号'),
('0002','刘东阳','男','1995-05-12','东阳市八一北路33号'),
('0003','马晓夏','女','2000-10-01','长岭市五一路763号'),
('0004','钱忠理','男','1994-09-23','滨海市洞庭大道279号'),
('0005','孙海洋','男','1995-04-03','长岛市解放路27号'),
('0006','郭小斌','男','1997-11-10','南山市红旗路113号'),
('0007','肖月玲','女','1996-12-07','东方市南京路11号'),
('0008','张玲珑','女','1997-12-24','滨江市新建路97号');
-
修改表
- 添加标题(add)
添加1个
alter table 表名 add 标题名 类型 约束;
alter table student_info add 爱好 varchar(50);
添加多个
alter table 表名
add 标题名 类型 约束,
add 标题名 类型 约束;
alter table student_info
add 爱好 varchar(50),
add 梦想 varchar(50);
-
修改表名(rename)
alter table 旧表名
rename to 新表名
alter table student_info rename to student_info1;
- 修改字段(标题)名 (change)
alter table 表名
change 旧标题 新标题 数据类型 约束;
alter table student_info change 梦想 理想 varcher(50);
- 修改字段/标题的数据类型和位置(modify)
alter table 表名
modify 表名 数据类型【想改为的类型】
alter table student_info modify 理想 varchar(10);
- 修改表的存储引擎(engine)
alter table 表名 engine 想改为的引擎;
alter table student_info engine MyISAM;
-
删除标题/字段(drop)
alter table 表名
drop 标题/字段名
alter table student_info
drop 理想,drop 爱好;
-
删除标题/字段的约束(drop constraint)
alter table 表名
drop constraint 标题/字段名;
alter table student_info drop constraint 姓名;
-
删除主码约束 (drop primary key)
alter table 表名
drop primary key;
alter table student_info drop primary key;
-
删除表:
drop table 表名;
drop table student_info;