MySQL基础讲义——数据库与表空间

· 数据库

-- 创建数据库   
create database test1;   
-- 查看数据库   
show databases;   
-- 选择数据库   
use test1;   
-- 删除数据库   
drop database test1;  

· 表空间

表空间是什么?

图片名称

数据类型(共3大类):数值型、字符型、日期型
数值型:int整数型、float浮点型
字符型:char、varchar 注意:区别,填满和不填满
日期型:datetime “YYYY-MM-DD hh:mm:ss”

-- 创建表    
create table stu_inf(id int(10),stu_name varchar(20),stu_sex varchar(20),stu_score float(10));      
-- 查询表   
show tables;   
describe stu_inf;   
-- 修改表(修改表名称,添加变量,修改变量,删除变量)    
alter table stu_inf rename a1; #修改表名称   
alter table a1 add column class varchar(20); #添加变量   
alter table a1 change column class stu_class varchar(20); #修改变量    
alter table a1 drop column stu_class; #删除变量
-- 删除表    
drop table a1;

索引:主键索引(一张表里只能有一个)
   唯一索引(一张表里可能存在多个)
作用:提高后台查询效率(为什么?)

图片名称
-- 添加索引   
alter table stu_inf add primary key(id);   
alter table stu_inf add unique(stu_name);   
-- 查询索引   
show index from stu_inf;   
-- 修改索引(先删除后添加)   
-- 删除索引   
alter table stu_inf drop primary key; #删除主键索引   
alter table stu_inf drop index stu_name; #删除其他索引

· 单元测试

创建一个数据库 test_stu
进入数据库 test_stu
在数据库 test_stu 中建立四张表:
Student 学生表(id, S_name, S_age, S_sex)
Score 成绩表(S_id, C_name, score)
Teacher 教师表(id, S_id, C_name, T_name)

· 相关连接

MySQL基础讲义—— 数据库与表空间
MySQL基础讲义—— 约束与表管理
MySQL基础讲义—— SQL查询

分类: 数据库技术