启动MySQL:net start mysql
连接服务器:mysql -h 地址 -P 端口 -u 用户名 -p 密码
CMD打开方式:mysql -u root -p
#查看已有数据库 show databases; #查看当前数据库 select database(); #显示当前时间、用户名、数据库版本 select now(), user(), version(); #查看当前数据库信息 show create database `数据库名`; #创建数据库 create database [if not exists] `数据库名` `数据库选项`; 数据库选项: charset set charset_name collate collation_name #删除数据库 drop database [if exists] `数据库名`; #修改数据库信息 alter database `库名` `选项信息`; #使用数据库 use `数据库名`; #查看数据库存储引擎 show engines;
#查看所有表 show tables; show table from `表名`; #查看表结构 #反单引(``)标注该关键字是表或字段 #查看表的结构语句 describe `表名`; #查看表的详细结构语句 show create table|desc|describe|explain `表名`|show columns from `表名`; show tables status [from db_name]; #创建表 create [temporary] table [if not exists] `[库名].表名`( `字段1` `列类型[属性][索引][注释]`, `字段2` `列类型[属性][索引][注释]` )charset =utf8; temporary:临时表,会话结束时自动消失 #删除表 drop table [if exists] `表名`; #清空表数据 truncate [table] `表名`; #修改表 #修改表本身的选项 alter table `表名` `表的选项`; #对表重命名 rename table `原表名` to `新表名`; rename table `原表名` to `库名.表名`; --可将表移动到另一个数据库 #修改表的字段结构 alter table `表名` `操作名`; #操作名 add [column] `字段名`; --增加字段 after `字段名`; --表示增加在该字段后面 first `字段名`; --表示增加在第一个 add primary key (`字段名); --创建主键 add unique [`索引名`] (`字段名`); --创建唯一索引 add index [`索引名`](`字段名`); --创建普通索引 drop [column] `字段名`; --删除字段 drop primary key; --删除主键 drop index `索引名`; --删除索引 drop foreign key `外键`; --删除外键 modify [column] `字段名` `字段属性`; --支持对字段属性进行修改,不能修改字段名(原属性需写上) change [column] `原字段名` `新字段名` `字段属性`; --支持对字段名修改 # 示例 ################################################################## #在邮箱后添加地址段 add address varchar(200) not null after Emial # 示例 ################################################################## #创建学生表 #comment为注释,default为默认值 create table if not exists student( stuNo int(4) primary key comment '...', stuName varchar(20) not null, stuSex char(2) not null, stuAge int(3) not null, stuAddress varchar(200) default '...', gradeId int(4) not null )CHARSET =utf8;
类型 | 说明 | 备注 |
---|---|---|
int | 标准整数 | 无 |
char[(M)] | 固定长度字符串 | 0<=M<=255 |
varchar[(M)] | 可变字符串 | 0<=M<=65535 |
datatime | YY-MM-DD hh:mm:ss | 23:59:59 |
#增加数据 # insert 与 replace 完全一样,可互换 insert into `数据库名.表名` (`字段名`) values (`值`); --若全插入,可省略字段名 insert into `表名` set `字段名`=`值`; #利用 local data 语句将数据装入数据库表中 local data local infile `地址、文件名` into table `表名`; #删除数据 delete from `表名`[`删除条件字句`]; --没有条件语句,将全部删除 #更改数据 update `表名` set `字段名`=`新值`; #查找数据 select `字段列表` from `表名`[`其他字句`];