有库才有表,有表才能插(数据操作)——表操作

1:查看表:

show tables;

2:创建表:

create table [if not exists] 表名(
列名1 类型 [限制],
列名2 类型 [限制],
...
列名n 类型 [限制] #最后⼀列没有逗号
)** [engine=myisam | innodb][ default charset=utf8];
primary key 主键 不允许有重复值,不允许为空
auto_increment 自增长,只对int型主键起作用
#复合主键
mysql> create table grade(
sid int ,
cid int,
score float,
primary key(sid,cid));

#字段限制
primary key 不允许空值,唯⼀
not null ⾮空
unique 唯⼀
default 缺省,默认值

例如
课程表:

mysql> create table course
    -> (
    -> cno varchar(5) comment '课程号',
    -> cname varchar(5) not null comment '课程名',
    -> tno varchar(10) not null comment '教师编号',
    -> primary key (cno)
    -> );
**3.**删除表
drop table 表名;
**4.**复制表结构
create table 表名 like 其他表名
**5.**查看表结构
desc 表名;
**6.**查看建表语句
show create table 表名;
**7.**修改表
#修改字段类型
 alter table 表名 modify 字段名 类型 [限制] #增加字段
 alter table 表名 add [column] 字段名 类型 [限制];
 
#删除字段
 alter table 表名 drop [column] 字段名;
修改字段名和类型
 alter table 表名 change [column] 旧字段名 新字段名 类型 [限制];
 
#修改表名
 alter table 表名 rename 新表名
 alter table 表名 [engine=myisam] [default charset=utf8];
 
 # 可以通过first、after指定插⼊位置
 alter table student add sno varchar(3) not null after sid; //在sid列后插⼊
 alter table student add sid int primary key auto_increment first;//在第⼀列插⼊
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值