插入<记录>INSERT
语法:insert into 表名values (字段值1,字段值2, 字段值3);
同时插入多条记录
insert into 表名 values(2,‘lis’,24),(3,‘wange’,26);
分开插入表记录
insert into 表名 (id,name)values(4,‘hangl’);
查询表中记录select
select * from 表名; # *号表示表中所有的字段
当表中记录比较多时可以使用\G查看
select * from student\G
只查询表中某个字段或某些字段的内容
select 字段名 from 表名;
查看别的数据库的表或者不在本数据库上进行查看
语法:SELECT 字段 FROM 数据库名.表名;
删除表中的记录
语法:
DELETE FROM table_name WHERE some_column=some_value;
删除students表中id为3的行
delete from students where id=3;
删除age为空的行
delete from students where age is null;
更新记录
语法:
UPDATE表名 SET 字段名1=value1,字段名2=value2,…
WHERE 字段名=value;
例:把表中id为2的记录age更新为25:
update students set age=25 where id=2;
把表中所有的id都更新为2
update students set id=2;
注:不加where,把所有字段值更改
同时更新多个字段的值,请使用逗号隔开:
update students set id=1,name=‘zhangsan’ where age=21;
SQL条件查询语句
去重复查询distinct:
例:select distinct name,age from 表名;
使用AND和OR进行多条件查询
查询表中id>3和age>25的记录:
select id,name,age from students where id>3 and age>25;
查询表中id>3 或者 age>25的记录:
select id,name,age from students where id>3 or age>25;
区分大小写查询binary
select * from students where binary name=‘kill’;
select * from students where binary name=‘KILL’;
MYSQL查询排序
语法:select字段1,字段2 from 表名order by 字段名;
默认为升序asc
select id from students order by id asc;
降序desc
select id from students order by id desc;
关于MYSQL命令帮助
help show;
Mysql的数据类型
数据类型是数据的一种属性,其可以决定数据的存储格式,有效范围和相应的限制。mysql的数据类型包括整数类型,浮点数类型,日期和时间类型,字符串类型和二进制类型。
作用:
1、使系统能够根据数据类型来操作数据。
2、预防数据运算时出错。
3、更有效的利用空间。数据分类,可以使用最少的存储来存放数据,同时提高性能。
数据类型解释
MySQL支持多种类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型。
1.数值类型
2.日期和时间类型
3.字符串类型
测试取值范围
mysql> create table kdata ( fti tinyint,fsi smallint,fmi mediumint ,fi int, fbi bigint);
mysql> insert into kdata values (123456789,123456789,123456789,123456789,123456789);
ERROR 1264 (22003): Out of range value for column ‘fti’ at row 1
错误1264(22003):第1行“fti”列的值超出范围,对于不符合的数据报错
insert into kdata values (123,12345,1234567,123456789,123456789);
可以插入
插入错误的值:
create table kdata2 (age int) ;
insert into kdata2 values (‘hello’ );
ERROR 1366 (HY000): Incorrect integer value: ‘hello’ for column ‘age’ at row 1