数据表的基本操作

数据表的增删书改查

  1 # 数据表的增删改查
  2 
  3 # 并设置数据表的存储引擎alter
  4 create database test_review;
  5 use test_review;
  6 
  7 # 创建数据表 设置主键约束性字段
  8 
  9 # 但字段主键 只有一个主键 primary key
 10 create table review(
 11 	id int primary key auto_increment,
 12     name varchar(10),
 13     score int(10)
 14 )engine = InnoDB;
 15 
 16 # 多字段主键
 17 create table review_1(
 18 	id int(10),
 19     name varchar(10),
 20     score int(10),
 21 	primary key(name, id)
 22 )engine = InnoDB;
 23 
 24 # 创建外键约束的字段
 25 create table review_2(
 26 	id int(10) primary key auto_increment, # 自动递增约束
 27     name varchar(10) not null,  # 非空约束
 28     score int(10)
 29 )engine = InnoDB;
 30 
 31 create table review_3(
 32 	id int(10) primary key auto_increment, # 自动递增约束
 33     name varchar(10) not null,  # 非空约束
 34     score int(10) unique,  # 唯一名称约束
 35     course varchar(10) default "默认"  # 默认值约束
 36 )engine = InnoDB;
 37 
 38 # 查看数据表
 39 show tables;
 40 
 41 # 查看数据表的基本结构
 42 desc review;
 43 
 44 # 查看表的次详细结构 查看表的存储引擎, 字段, 编码
 45 show create table review;
 46 
 47 # 4.3 修改数据表
 48 # 4.31 修改表名 alter
 49 alter table review rename review_00;
 50 
 51 # 4.32 修改数据表字段类型
 52 alter table review_3 modify course char(10);
 53 
 54 # 4.33 修改字段名
 55 alter table review_3 change course courses varchar(10);
 56 alter table review_3 change courses courses char(10); # 同时修改字段名和类型
 57 
 58 # 4.34添加字段
 59 
 60 alter table review_3 add teacher varchar(10) default "默认";  # 添加字段, 具有约束性默认值
 61 
 62 # 在表的第一列添加字段
 63 alter table review_3 add test_first01 int  not null first;
 64 
 65 # 在表的指定列后面添加字段
 66 alter table review_3 add test_behind int unique after courses;
 67 
 68 # 4.35 删除字段
 69 alter table review_3 drop test_first;
 70 
 71 # 4.36 修改字段的排列位置
 72 alter table review_3 modify;
 73 
 74 show databases;
 75 use test_review;
 76 show tables;
 77 
 78 # 4.37 修改表的存储引擎
 79 alter table review_3 engine= myisam;
 80 
 81 # 4.38 删除表的外键约束 解除主表和从表之间的关联
 82 # 外键约束 关联表之间的的字段在创建表的时候创建
 83 alter table 表名 drop foreign key 外键约束名
 84 
 85 # 4.4 删除表
 86 # 4.41 删除没有被关联的表
 87 drop table if exists 表名1,  表名2...
 88 
 89 
 90 # 4.42 删除被其他表关联的主表
 91 # 直接删除主表的时候会报错因为 会影响表之间的完整性, 因此需要关联的子表,再删除父表
 92 # 1 删除表之间大的关联键
 93 # 2 删除主表
 94 drop table 表名
 95 
 96 
 97 
 98 
 99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
posted @ 2019-05-20 21:10  binyang  阅读(186)  评论(0编辑  收藏  举报