MySQL基本语句及理解

#查询:
use zhaopin;
desc information;#显示表结构
#show create table information;#显示创建表的sql语句
#select 字段名 from information where 条件;
select 职位,工资 from information where 职位!='其他';


#修改表:alter,rename
use zhaopin;
#rename table imformation to information; 对表进行重命名
alter database zhaopin character set utf8;
#alter table 表名 add 字段名 数据类型; 添加字段
alter table information add 年终奖 int;
#alter table 表名 drop 被删除的字段名 删除字段
alter table information drop 年终奖;

#对表中的字段重命名:alter table 表名 change 旧字段名 新字段名 数据类型
use zhaopin;
alter table information change 注释 备注 text; #同时可以更该表字段的数据类型,保持字段名不变即可


#改变表的内容:
#update 表名 set 字段名=balabala where 条件
use zhaopin;
update information set 职位='不确定' where 职位='其他'; #查找null字段用is,不能用=, 而是 where 职位 is null

数据备份与还原

#第一种通过命令:
#mysqldump -uroot -p密码 需要备份的数据库名> e:\备份后的sql脚本名 通过命令行备份
mysqldump -uroot -proot zhaopin>e:\zhaopin_back.sql
#还原:首先进入到mysql环境--->创建一个库--->在库下还原数据库
#使用命令 source 备份的数据库脚本

#第二种通过图形化界面

#------------------------------------------------------------------------------------------------------
#属性:
#默认:default '默认值'
#非空:not null 在数据操作时必须有值
#自动增长:auto_increment ,尽量作用在int类型上
#主键:primary key,不能够重复,一张表只有一个字段可以作为主键
#unique:唯一键,被unique修饰不能重复
#表字段注释:comment‘注释内容’,注释字段含义的属性,创建表时该属性放在最后

#两种删除方式:
#1.delete 用法:delete from 表名 where 条件
delete from information where 1=1; #删除整张表,但效率较慢,需要一条一条扫描删除,当主键被设置自动增长时,不会清除原有的增长记录,例如,删掉id为3的记录时,下一条记录的id为4
#2.truncate table 表名 #删除整个表
truncate table information; #删除效率较高,会删除原有的增长记录,删除整张表后,从1重新开始

猜你喜欢

转载自www.cnblogs.com/zhanghuachuan/p/11674396.html