mysql SQL语法

增删改查

insert into table
(col1,clo2,col3)
values
(str1,str2,str3),
(str1,str2,str3);

delete form table
where Expression

update table
set
col1 = update,
col2 = update
where Expression;

select col1,col2,col3 form table
where Expression;

where 条件判断

  • in:效果等同于or
  • select id form tableTable where name in ('张三',‘李四’);

group by 分组

order by 对结果进行排序

distinct关键字 不同的。如果查询结果有重复的,可以使用distinc关键字使重复结果只出现一次。

select distinc company from tableTable;

删除表

drop table table;

列类型

Tinyint:占据空间1字节,存储范围-128-127或0-255
0代表正数,1代表负数
0 000 0000 ->0
0 111 1111->127
1 000 0000->0
1 111 1111->-127
可以看到,0有两种表达方式,0 000 0000和1 000 0000.
二进制补码
负数=绝对值-128
例如1 000 0000 = -128
1 111 1111 = -1
也就是说,负数的0变成了-1,0只有一种表达方式0 000 0000
创建表的时候怎么区别有符号数和无符号数呢?

create table student (
age tinyint not null default 0
)engine myisam charset utf8;

上述语句默认为有符号数
int系列不加特殊声明时,默认为有符号数
特殊声明:unsigned
给表student增加一列score

alter table student add score tinyint unsigned not null default 0;

特殊声明:(M) zerofill
M——宽度; zerofill——补0

alter table studnet add num smallint(5) zerofill not null default 0;

smallint:占据空间2个字节,储存范围0-65535或-32768到32767

mediunint:
3个字节

int:
4个字节,存储范围0到42亿

bigint:
8个字节

float(M,D)
M“精度”,代表小数的总位数;D“标度”,代表小数点后的位数。
如果M<=24,占据4个字节,否则占用8个字节
浮点(float)和定点(decimal)的区别:定点把整数部分和小数部分分开存储,比浮点类型精确。float会损失精度。

alter table student add
num2 float(6,2) not null default 0;

范围为-9999.99到9999.99
decimal(M,D):
同float
123456789.123456789
decimal占据8个字节,它将整数和小数分开,每9个数用4个字节存储,一共8个字节。

char
定长字符串。char(4)
速度上,定长更快。
不够的部分用空格填充,取出时删去空格。
如果你存储的字符本身有空格,取出的时候会被删去,注意!
char(M)和varchar(M)限制的是字符,而不是字节。

varchar
变长字符串。
在数据前面用一个字节或两个字节表示数据的长度。
MySQL要求一个行定义长度不能超过65535个字节,不包括text、blob等大字段类型,varchar长度受此长度限制,和其他非大字段加起来不能超过65535个字节.
使用gbk字符类型,用两个字节表示一个字符,varchar最大为65535/2=32766个字符。
使用utf8字符类型,用三个字节表示一个字符,varchar最大为65535/3=21845个字符。

mysql没有uniqueidentifier数据类型
如果要使用uniqueidentifier,考虑使用sqlserver

text
储存较大的文本,搜索速度稍慢。
建表时不需要设置默认值。

create tabel student
(name text);

blob:
二进制类型,用来存储图像、音频等二进制信息。
blob的意义在于防止因为字符集的问题导致的资源损坏。
例如,一张图片有0XFF字节,该字节在ascii码中认为非法,在入库时就会被过滤掉。当你取出时,图片就已经被损坏了。

date
时间类型。范围为1000-01-01到9999-12-31

create table time
(time date not null default '0000-00-00');

time:
时间类型。

datetime
日期时间类型。

timestamp:
时间戳。

create table test
(stime timestamp default current_timestamp);

默认取系统当前时间。

year
年份。范围从1901到2155年,加上0000年。占据一个字节,0-255.

创建表

create table table(
id int not null auto_increment,        //auto_increment一般用于主键
name varchar(30) not null default '',
primary key(id)
)engine=InnoDB default charset=utf8;

修改表

增加列

alter table testTable add id int not null default 0;

在指定列后添加新列:

alter table testTable add id int not null default 0 after name;

将列新建在最前面:

alter table testTable add num int not null default 0 first;

删除列

alter table testTable drop id;

修改列
相同数据类型,只增加宽度,例如从char(1)到char(4)。
修改列类型:

alter table testTable modify id bigint not null default 0;

修改列名+列类型:

alter table testTable change id newid smallint not null default 0;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值