开启服务:
net start mysql
连接认证语法:
mysql -h localhost -p 3306 -u root -p
关闭服务:
net stop mysql
创建数据库:
create database 数据库名;
创建带库选项的数据库:
create database 数据库名 charset gbk;
选择数据库:
use 数据库名;
查看数据库创建语句:
show create database 数据库名;
显示所有数据库:
show databases;
显示以my开头的全部数据库:
show databases like 'my%';
显示以bases为结尾的数据库:
show databases like '%bases';
修改数据库字符集:
alter databases 数据库名 charset gbk;
删除数据库:
drop database 数据库名;
创建表:
create table 表名(
字段名 int,
字段名 varchar(10)
);
创建带表选项的表:
create table 表名(
字段名 int,
字段名 varchar(10)
)charset gbk;
显示当前库的所有表:
show tables;
查看表结构:
desc 表名;
查看以a开头的所有表:
show tables like 'a%';
显示表创建语句:
show create table 表名;
复制表结构:
create table 表名2 like 表名1;
修改表的字符集:
alter table 表名 charset utf8;
修改表名:
rename table 原表名 to 新表名;
向表中添加新字段(放在最前):
alter table 表名 add 字段名 int[类型] first;
向表中添加name字段,放在id字段后:
alter table 表名 add name char(10) after id;
修改字段名,将age字段修改为id:
alter table 表名 change age id int;
修改name字段类型:
alter table 表名 modify name char(20);
删除字段:
alter table 表名 drop 字段名;
删除一个表:
drop table 表名;
删除多个表(不允许):
drop table 表1,表2;
向表中插入1条数据:
insert into 表名(id,name)values(1,"张三");
向表中插入多条数据:
insert into 表名 values(1,'张三'),(2,'李四');
查看表的全部信息:
select * from 表名;
查询学生表的姓名和年龄:
select name,nl from stu;
查询姓名为张三的学生信息:
select * from stu where name="张三";
删除姓名为张三的学生信息:
delete from stu where name="张三";
将张三的id修改为3:
update stu set id=3 where name="张三";
设置统一字符集:
set names gbk/utf8;
增加主键1:
create table 表名(
id int primary key,
name char(20)
)charset gbk;
增加主键2:
create table 表名(
id int,
name char(20),
primary key(id)
)charset gbk;
表后增加主键:
alter table 表名 add primary key(字段名);
删除主键:
alter table 表名 drop primary key;
添加自动增长:
create table 表名(
id int primary key auto_increment,
name char(10) not null
)charset gbk;
修改自动增长:
alter table 表名 auto_increment=值;
创建唯一键:
alter table 表名add unique key(字段名);
删除唯一键:
alter table 表名 drop index 唯一键名字;
查询表中全部数据:
select * from 表名;
查询表中部分字段:
select 字段列表 from 表名;
条件查询数据:
select 字段列表 * from 表名 where 字段名=值;
删除操作:
delete from 表名 【where条件】;
更新操作:
update 表名 set 字段名=新值 【where条件】;