mysql直接对表操作_MySQL对表的操作

这篇博客详细介绍了MySQL数据库的常用操作,包括创建表、查看表、修改表结构、插入数据、解决插入时的编码问题、更新表数据、删除记录以及各种查询操作。内容涵盖DML和DDL语句,对数据库管理进行了全面解析。
摘要由CSDN通过智能技术生成

对表的整体操作

创建表

use mydb1;

create table employee(id int, name varchar(40),sex  varchar(2),birthday date,entry_date date,job varchar(40),salary decimal(8,2),resume text);

查看表

show tables;

show create table employee;

desc employee;

修改表

alter table employee add image blob;

alter table employee modify sex varchar(4);

alter table employee drop sex;

alter table employee character  utf8;

alter table employee change column name xingming varchar(40);

rename table employee to emp;

删除表

Drop table emp;

对表的细操作

插入数据:

Insert into employee

(id,name,sex,birthday,entry_date,job,salary,resume) values

(1, 'john', 'a', '1990-10-01', '2014-10-1', 'ceo',10000, 'hello');

insert into employee values(1, 'john', 'a','1999-09-09','1999-09-09','bbb',90,'aaaaa');

插入遇到的问题

ERROR 1300 (HY000): Invalid utf8 character string: '\xA1\xAFaaa\xA1\xAF'

解决办法:引号问题’ ’这种是GB2312 , '  '这种是utf-8

ERROR 1366 (HY000): Incorrect string value: '\xC4\xD0' for column 'sex' at row 1

解决办法:show variables like 'chara%';

set character_set_client=gb2312;

284e82a8629cfee43b8279e528a5f56f.png

该处查看会显示乱码

解决方法:set character_set_results=gb2312;

修改表数据:

update employee set salary=10;

update employee set salary=10000 where name='john';

update employee set salary=salary+1000 where name='aaa';

删除表的记录

delete from employee where sex='null';

delete from employee;  //删除表的内容,但不删除定义,不释放空间

truncate table employee;//删除内容、释放空间,但不删除定义

Drop table emp;       //删除表的内容、定义和释放空间

查询表数据

select * from student;

条件查询

select * from student where name='王五';

select name,english from student;

select distinct english from student;

select name,(chinese+english+math) from student;

select name,(chinese+english+math)+10 from student;

select name as 姓名,(chinese+english+math)+10 as 总分 from student;  //别名

select name 姓名,(chinese+english+math)+10  总分 from student;

select * from student where english>'90';

select name from student where (chinese+english+math)>200;

select name from student where (chinese+english+math)> '200'  //这里用单引号也是可以的

select name from student where english>80 and english<90;

select name from student where English between 80 and 90;  //这句跟上面那句是一样的

select * from student where math in(89,90,91);

模糊查询

select * from student where name like '李_'; (2个字符)

select * from student where name like '李%';  (2个字符以上)

排顺查询

select name,math from student order by math;

select name 姓名,(chinese+english+math) 总分 from student order by (chinese+english+math) desc;

select name 姓名,(chinese+english+math) 总分 from student order by 总分 desc;

select * from student where name like '李%' order by (chinese+english+math) desc;

统计

select count(name) from student;

select count(*) from student;

select count(*) from student where (chinese+english+math)>250;

求和

select sum(chinese),sum(english),sum(math) from student;

select sum(chinese+english+math) from student;

求平均

select avg(chinese) from student;

select avg(chinese+math+english) from student;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值