【更改字段的长度】注意表名、字段名都不用加引号:数据库的操作
alter table Blog modify column Topic varchar(255);
【增加字段】
alter table Diary add Byp_uuid varchar(36);
alter table Warning_Wave2 add MaoMing text DEFAULT NULL;
alter table StormSurge2 add MaoMing text DEFAULT NULL;
【修改一个字段的名称】此时一定要重新指定该字段的类型
alter table Outlook CHANGE Content ContentSummary mediumtext;
【同一个字段多个or判断条件】会影响查询效率,可以改用in语句 ;
【删除数据库】
DROP DATABASE my_db1;
【创建数据库】
CREATE DATABASE IF NOT EXISTS my_db1;
【备份数据库】
mysqldump -u -p bypDB >/$path/bypDB20180225.sql
【导出单个数据表结构(不包含数据)】
mysqldump -h localhost -uroot -p123456 -d database table > dump.sql
【导入数据库】
常用source 命令 。进入mysql数据库控制台,如mysql -u root -p mysql>use 数据库
然后使用source命令,后面参数为脚本文件(如这里用到的.sql) :mysql>source /data/web/archive/DB_HYYB/BlogTableOnly.sql
【表改名】
ALTER TABLE Blog RENAME TO Outlook;
【替换字段内的内容】 将表Blog的字段Content的图片地址替换
update Blog set Content = replace(Content,'/BypResearch/BypSystems/BypWeb/assets/kindeditor/attached/','/archive/BypResearch/Attached/');
UPDATE Blog SET Category = 'BypResearch' WHERE Type = 'Tanngo';
UPDATE Person SET Address = 'Zhongshan 23', City = 'Nanjing' WHERE LastName = 'Wilson'更新某一记录多个字段;
【利用mysqldump 将一个表按条件导出数据】 参考来源
mysqldump -uroot -pdsideal -t dsideal_db t_resource_info --where="res_type=1 and group_id=1 and ts>2015122115005600474 ORDER BY TS DESC LIMIT 1" --triggers=false --replace > /usr/local/info.sql
说明:
-t:只导数据
--where:条件
--triggers=false:不导触发器
--replace:使用REPLACE INTO 取代INSERT INTO
【数据库启动】 ubuntu
重新启动:service mysqld restart
停止:service mysqld stop
启动:service mysqld start
============================
20190603
重新启动:service mysql restart
停止:service mysql stop
启动:service mysql start
状态:service mysql status
【数据库启动】 windows
停止:net stop mysql
启动:net start mysql
如果你没安装系统服务,也可在命令行模式定位到mysql下的bin目录里,输入:
(关闭)mysqladmin shutdown
(启动)mysqladmin start
PRI主键约束;UNI唯一约束;MUL可以重复。
假设有数据库表playlist163,将其中的over字段里key 设为MUL
mysql>alter table playlist163 add index(over);