MySQL数据库入门(表操作、表约束)
#语法:
# CREATE TABLE 表名(
# 字段名1 数据类型 [unsigned|auto_increment|not null| comment ''],
# 字段名1 数据类型 comment '对该字段的描述',
# .
#创建表 #语法: # CREATE TABLE 表名( # 字段名1 数据类型 [unsigned|auto_increment|not null| comment ''], # 字段名1 数据类型 comment '对该字段的描述', # . # . # . #); #eg: CREATE TABLE first ( id INT, age TINYINT, name VARCHAR(20) ); CREATE TABLE FRUIT( R_ID INT comment 'id', R_NAME VARCHAR(10) comment '水果的名字' ); #删除表 #语法 alter table [if exists] 表名; DROP TABLE if exists fruit; #修改表结构 #增加字段 #语法一: alter table 表名 modify 字段名 数据类型; #eg: alter table first modify name char(12) not null; #查询表结构MySQL 创建数据表,通过这个可以查看表的结构 desc first; #语法二: alter table 表名 change 原字段名 新新字段名 数据类型; alter table first change name name varchar(20) not null default '灰太狼'; #通过查询创建表结构,并且复制数据。 #这种方式仅仅只是复制了表结构,不会将被赋值表的主键、索引、 # Extra(auto_increment,字符集编码及排序)、注释、分区等属性 以及触发器、外键等。 #create table 表名 as select * from 表名; create table ff as select id, name from first; #通过查询创建表结构,不复制数据。同上不会赋值主键、索引等。 create table ff1 as select * from first where 1=2; #约束 #MySQL数据库的约束分为4种, #1.主键约束 #2.外键约束 #3.唯一键约束 #4.非空约束 #MySQL数据库不支持检查约束,但是提供了check的代替方式 #mysql数据库约束名默认与字段名相同。通过select keys from 表名 或者 show index from 表名;查看 ; #主键约束:主键约束列不允许重复,不允许为空值,每个表只允许有一个字段是主键。 #主键可以是单个字段,也可以是多个字段组成联合主键。 #建表时创建主键 #单个字段主键 create table test( id int primary key comment '主键字段', name varchar(32), age tinyint ); #联合主键 create table test2( id int comment '主键字段', name varchar(20), age tinyint, primary key(id, name) ); #该已有的表中字段添加主键 #语法:alter table 表名 add primary key(字段1,字段2...); #方式一 alter table test2 add primary key (id,name); alter table test2 add primary key (id); #方式二 alter table test2 add constraint primary key (id); #删除主键 #语法:alter table 表名 drop primary key; alter table test2 drop primary key ; #唯一键约束: #指定的table的列或者列组合不能重复,保证数据的唯一性。 #唯一约束不允许出现重复值,但是可以有多个null。 #同一个表可以有多个唯一约束,多个列组合的约束。 #创建表时,用户名,密码不能重复 create table temp( id int not null , name varchar(12), password varchar(12), constraint un_temp_name_pwd unique (name, password) #简写 unique(字段1,字段2...) ); #给已有的表添加唯一键约束 #语法:alter table 表名 add unqiue(字段名1,字段名2...); #方式一 alter table test add unique (name); #方式二 alter table test add constraint unique (name); #修改唯一键约束 alter table temp modify name varchar(12) unique ; #删除唯一键约束 #语法: alter table 表名 drop index 约束名; alter table temp drop index un_temp_name_pwd; #查看表中约束的名字 show keys from temp; show create table temp; show index from test2; #外键约束 #外键约束是保证一个或两个表之间的参照完整性,外键是构建于一个表的两个字段或者是两个表的两个字段之间的参照关系。 #在建表时 #添加外键 create table stu( sid int primary key , sname varchar(20) ); create table socre1( sid int, score double, constraint fk_stu_score1_sid foreign key (sid) references stu(sid) ); #给已经存在的表添加外键约束 #语法:alter table 外键表名 add constraint 外键名 foreign key(外键字段名) #refenences 主表名(主键名); create table stu1( sid int primary key , sname varchar(20) ); create table socre2( sid int, score double ); alter table socre2 add constraint fk_socre1_stu1_sid foreign key(sid) references stu1(sid); show create table socre1; #非空约束 not null #建表时 直接添加在字段后面。 create table socre2( sid int, score double not null ); #给已有的表中字段添加非空字段 #语法:alter table 表名 change 新字段名 新字段名 数据类型 约束; alter table socre2 change score score double not null; show create table socre2; #检查约束 # mysql暂不支持,但提供了代替check的方式 #enum(值1,值2,值3) 单选 #set(值1,值2,值3) 多线 create table test4( id int primary key auto_increment, sex varchar(50) check(sex in ('男','女','女博士')) ); #check关键字虽然可以正常写入,但并没有效果。 create table test5( id int primary key auto_increment, sex varchar(50) #enum('男','女','女博士') ); alter table test5 add sex2 enum('男','女','女博士'); insert into test5 values(default, "女", "女博士"); #[01000][1265] Data truncated for column 'sex2' at row 1 insert into test5 values(default, "女", "女人"); create table test6( id int primary key auto_increment, sex varchar(50), sex2 set('男','女','女博士') ); insert into test6 values(default,'男','男,女'); select * from test6; (编辑:航空爱好网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |