库中数据表操作
1、创建表
(1)简写形式
create table 表名 (
字段名 数据类型,
字段名 数据类型,
字段名 数据类型
);
注意:如果不遇到; 则代表命令未结束,即使换行,也是同一个命令。
(2)完整语法:
create table 表名(
字段名 数据类型 *约束* ,
字段名 数据类型 *约束* ,
字段名 数据类型 *约束*
) *default charater 字符集名称*;
注意:**部分可以省去。
(3)常见问题:
问题1: ERROR 1046 (3D000): No database selected
(没有选择仓库,需要使用)
解决: use 仓库名;
问题2: You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'table(tbnamevarchar(20),tbtype varchar(20))' at line 1
解决: 找 near 关键词,查看语句中的错误位置
问题3:如何在黑窗口输入中文
解决: set names gbk
2、表约束
-
非空约束(not null)
非空,即字段下不能存入null值(空值即null)**给字段设置非空约束,限定字段下不能有null** 1、 空格不属于空 ' ' 2、 留白符不属于空 '' 3、 0不属于空 4、 'null' 不属于 null create table 表名 ( 字段名 类型 not null, 字段名 类型 not null, 字段名 类型 , 字段名 类型 ); 例如: create table stu1( id int, name varchar(20) not null, sex varchar(1) not null );
-
唯一约束(unique)
限定字段下不能存入重复值(null 除外)。方式一:(列级约束):即直接在字段后设置 unique create table 表名 ( 字段名 类型 unique, 字段名 类型 unique, 字段名 类型 ); 方式二:表级约束(即所有字段设置完成之后,再设置唯一约束) create table 表名 ( 字段名1 类型 , 字段名2 类型 , 字段名3 类型, unique(字段名1), unique(字段2) ); 表级约束可以设置组合唯一键 create table 表名 ( 字段名1 类型 , 字段名2 类型 , 字段名3 类型, unique(字段名1,字段名2) );
-
主键约束(primary key)
限定字段之下值不能重复,且不能为null.一张表中最多只能有一个主键约束。 方式一:列级约束 create table 表名( 字段1 类型 primary key, 字段2 类型, 字段3 类型 not null unique ); 方式二:表级约束 create table 表名( 字段1 类型 , 字段2 类型, 字段3 类型 not null unique, primary key(字段1) ); 组合主键形式 create table 表名( 字段1 类型 , 字段2 类型, 字段3 类型 not null unique, primary key(字段1,字段2) );
-
外键约束(foreign key)
1、建立不同表之间的数据关联关系。 2、在从表中设计关系字段, 3、该关系字段必须关联主表中的主键字段或唯一约束字段。 方式一:(列级约束,语法可以执行,但是在mysql中失效) create table 表名( 字段名 类型, 字段名 类型, 关系字段名 类型 references 主表名(字段名), 字段名 类型 ) 方式二: 表级约束 create table 表名( 字段名 类型, 字段名 类型, 关系字段名 类型, 字段名 类型, foreign key(关系字段名) references 主表名(字段名) )
-
检查约束(check)
设置字段下的值的筛选条件。在mysql中失效。 -
默认值(缺省约束 default)
如果添加数据时未给字段添加内容,则字段使用默认值填充。方式: create table 表名 ( 字段名 类型 约束 default 默认值, 字段名 类型 约束 default 默认值, 字段名 类型 约束 )
-
自增属性(自增约束 auto_increment)
1、设置字段下的值,进行自动增长。 2、配合主键 或 唯一键约束使用。 3、通常情况下会设置主键约束字段为自动增长(主键自增) 方式: create table 表名 ( 字段名 类型 primary key auto_increment, 字段名 类型, 字段名 类型 )
-
备注说明(comment)
给表中字段设置解释说明create table 表名 ( 字段 类型 约束 comment '字段说明', 字段 类型 约束 comment '字段说明', 字段 类型 约束 comment '字段说明' )
3、查询表结构
(1)查询表的结构信息
desc 表名;
(2)查询表的创建语句
show create table 表名;
3、删除表
drop table 表名;
4、修改表结构
(1)修改表名
rename table 旧表名 to 新表名;
alter table 旧表名 rename 新表名;
(2)修改字段名(可以修改字段名时顺便修改字段类型)
alter table 表名 change 旧字段名 新字段名 类型;
(3)修改字段类型
alter table 表名 modify 字段名 新类型;
(4)添加非空约束
alter table 表名 modify 字段 类型 not null;
(5)删除字段的非空约束
alter table 表名 modify 字段名 类型 null;
(6)添加唯一约束
alter table 表名 add unique(字段名);
(7)添加主键约束
alter table 表名 add primary key(字段名);
(8)添加外键
alter table 表名 add foreign key(字段名) references 主表名(字段名)
(9)删除主键约束
alter table 表名 drop primary key;
(10)查询表中约束的名称
select * from information_schema.table_constraints where table_name='表名';
(11)删除唯一约束
alter table 表名 drop index 约束名称;
(12)删除外键约束
alter table 表名 drop foreign key 约束名称;
(13)添加默认值
alter table 表名 modify 字段名 类型 default 默认值;
(14)删除默认值
alter table 表名 modify 字段名 类型 default null;
(15)添加字段
alter table 表名 add 字段名 类型 约束;
(16)删除字段
alter table 表名 drop column 字段名;
alter table 表名 drop 字段名;
change和modify区别:
change既可以修改列的名称也可以修改列的类型
modify只能修改列的类型