show create table goods; 查看建表语句 |
#下表为例 |
表名为: user |
+ ----+-------+--------+---------+--------+-------+ |
| id | sname | gender | company | salary | fanbu | |
+ ----+-------+--------+---------+--------+-------+ |
| 1 | zs | 男 | 啊啊| 3000 | 37 | |
| 2 | ww | 女 | 版本| 3500 | 69 | |
| 3 | wf | 男 | 出错| 3800 | 68 | |
| 4 | ff | 男 | 打的| 5800 | 57 | |
| 5 | af | 女 | 嗯嗯| 2800 | 31 | |
| 6 | aaf | 妖 | 方法| 6900 | 25 | |
+ ----+-------+--------+---------+--------+-------+ |
#############基本增删改查 |
#添加一条或者多条数据13570540021 |
insert into user (sname,gender,company) values ( 'zs' , 'gender' , 'company' ) |
insert into user (sname,gender,company) values ( 'zs' , 'gender' , 'company' ), |
( 'zs' , 'gender' , 'content' ); |
#删除一条或多条数据 |
delete from user where id=2 |
#修改一条数据 |
update user set sname= 'zs' ,gender= '男' where id=2 |
#查询一条或者多条 |
select * from user ; |
select * from user where id>3 |
select id, name from user where id>3 |
#条件查询 |
select * from human order by salary desc limit 2 倒叙查询salary字段查询2条 |
#####################表结构 列类型 |
数值三大类型### 带符号 无符号(unsigned) |
int : -2147483648 2147483647 4294967295 占的字节越多,存储范围越大 |
tinyint: -128 127 255(年龄,如果有符号就不能存储128岁只能127岁) |
smallint : -32768 32767 65535 |
#zerofill zero是0,fill是填充 |
#例如学号(不能为负,学号一般位数相同,即使不同,00013,01235,即不够位数,用0填充 |
alter table user add num smallint (5) zerofill not null default 0; |
#浮点数 float (M,D) m代表总位数(精度) d代表小数点负数(标度) |
float (6,2) -9999.99 9999.99 |
gongzi float (6,2) |
#还有一种叫定点 decimal (9.2),定点是把整数部分,和小数部分,分开存储,比 float 精确 |
decimal (9.2) not null default 0.00 |
decimal 所占字节123456789.987654321 |
#############字符类型### |
char : 定长类型(固定的) 速度上定长要快些 |
char (2) charset utf8 能存两个utf8字符 比如 '中国' |
varchar : 不定长类型 |
text: 文本类型 |
blob: 二进制类型 用来存储图形,音频等二进制信息 |
//意义:二进制,0-255都有可能出现 |
//blob在于防止因为字符集的问题,导致信息丢失 |
//比如一张图片中有0xFF字节,这个在ascii字符集中认为非法,在入库的时候被过滤了 |
###########日期时间类型##### |
#日期 date 型 存储 年-月-日 能存1000-01-01到9999-12-31日 |
birthday date not null default '0000-00-00' |
#时间 |
alter table tiqi add sign time not null default '00:00:00' ; |
#日期时间类型 datatime |
alter table tiqi add login datetime not null default '0000-00-00 00:00:00' ; |
###########################建表 |
create table member( |
id int unsigned auto_increment primary key , |
username char (20) not null default '' , |
grend char (1) not null default '' , |
weight tinyint unsigned not null default 0, |
birth date not null default '0000:00:00' , |
salary decimal (8,2) not null default 0.00, |
lastlogin int unsigned not null default 0 |
)engine myisam charset utf8; |
)engine myisam/innodb(引擎) charset utf8(字符集) |
#在数据表里增加一咧 |
#新加的列都在最后 |
alter table 表名 add 字段名 字段类型 字段参数 字段声明 |
alter table class add 字段名 int unsigned not null ; |
alter table user add age tinyint(1) unsigned not null default '' ; |
#指定加在谁的后面( after ) |
alter table 表名 add 字段名 列类型 列参数 列声明 after |
alter table test add birth date not null default '0000:00:00' after weght; |
#把列加在第一位( first ) |
alter table test add sb int not null defalt 0 first ; |
#删除列 |
alter table 表名 drop 列名称 |
#修改列类型( modify ) |
alter table 表名 modify 列名称 列类型 列参数 列声明 |
alter table test modify sb char (4) not null default '' ; |
#修改列名及列类型(change) |
alter table 表名 change 旧列名 新列名 新类型 新参数 |
初级程序员
by: 云代码会员 发表于:2023-03-20 09:01:18 顶(0) | 踩(0) 回复
回复评论