mysql 基础命令语句_MySQL 常用命令语句

#---------------------------

#----cmd命令行连接MySql---------

cd  C:\Program Files\MySQL\MySQL Server 5.5\bin

# 启动mysql服务器

net  start  mysql

# 关闭mysql服务器

net  stop  mysql

# 进入mysql命令行

mysql  -h  localhost  -u  root  -p

或mysql -u root -p

#---------------------------

#----MySql用户管理---------

#修改密码:首先在DOS 下进入mysql安装路径的bin目录下,然后键入以下命令:

mysqladmin -uroot -p123 password 456;

#增加用户

#格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by '密码'

/*

如,增加一个用户user1密码为password1,让其可以在本机上登录, 并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入mysql,然后键入以下命令:

grant select,insert,update,delete on *.* to user1@localhost Identified by "password1";

如果希望该用户能够在任何机器上登陆mysql,则将localhost改为"%"。

如果你不想user1有密码,可以再打一个命令将密码去掉。

grant select,insert,update,delete on mydb.* to user1@localhost identified by "";

*/

grant all privileges on wpj1105.* to sunxiao@localhost identified by '123'; #all privileges 所有权限

#----------------------------

#-----MySql数据库操作基础-----

# 创建数据库

create  database   namage  default  character  set  utf8  collate  utf8_general_ci;

# 如果数据库存在删除

drop  database  if  exists  manage;

# 进入数据库

use  manage;

# 删除数据库

drop  manage;

# 查看表的结构

desc  class;

# 查看表内数据

select  *   from  class;

# 创建班级表并添加字段:

create  table  class(

id  int(10)  not  null  auto_increment,

name  varchar(30)  not  null  default  " noname",

add_time  datetime  no t null,

primary  key(id)

)

ENGINE = INNODB  charset=utf8;

# 1、向表内添加2条数据:如果 add_time 字段为datetime

insert into class(name,add_time) values ("一年级","2018-08-31 15:33");

insert into class(name,add_time) values ("二年级","2018-08-31 15:33");

# 2、向表内添加2条数据:如果 add_time 字段为timestamp

insert into class(name) values ("一年级");

insert into class(name) values ("二年级");

# 创建学生表并添加字段:

CREATE table student(

id  int(10)  not  null  primary  key  auto_increment  unique,    #  unique唯一性,不可重复

name  varchar(30)  not  null  default  "noname "   comment   "名称",

age  int(10)  not  null  default  0  comment   "年龄",

birthday  datetime  not  null  comment  "生日",

class_id  int(10) ,

foreign  key(class_id)  references  class(id)

);

# 向表内添加4条数据:

insert into student(name,age,birthday,class_id) values ("卢宇蒙",23,"1996-07-11",1);

insert into student(name,age,birthday,class_id) values ("王志敏",23,"1996-08-12",1);

insert into student(name,age,birthday,class_id) values ("赵广正",23,"1996-09-13",2);

insert into student(name,age,birthday,class_id) values ("古川",23,"1996-10-14",2);

# 创建分数表并添加字段: decimal(5,2) 5是有效长度,2是小数点后2位

create  table  course(

id  int (10)  not  null  primary  key  auto_increment,

name  varchar(30)  not  null ,

score  DECIMAL(5,2)  not  null,

class_id  int(10)  not  null,

stu_id  int (10)  not  null,

foreign  key(class_id)  references  class(id),

foreign  key (stu_id)  references  student(id)

);

# 向表内添加5条数据:

insert into course(name,score,class_id,stu_id) values ("数学",90.6,1,1);

insert into course(name,score,class_id,stu_id) values ("语文","135.44",1,5);

insert into course(name,score,class_id,stu_id) values ("英语","100",2,3);

insert into course(name,score,class_id,stu_id) values ("政治","98",1,2);

insert into course(name,score,class_id,stu_id) values ("历史","89.92",2,4);

完成后如图所示:

# 查找三张表里所有的数据:

SELECT * FROM student;

SELECT * FROM class;

SELECT * FROM course;

# 查询student表中id=1的name名

select  name  from  student  where  id=1;

# 查询student表中name=“王志敏”的数据

select * from student where name = "王志敏";

# 查询student表中年龄大于15的数据

select id,name from student where age>"15";

and且;

# 查询student表中年龄大于15并且小于30的数据

select * from student where age>"15" and age

or 或;

# 查询student表中年龄大于15或小于30的数据

select * from student where age>"15" and age

between 之间;

# 查询student表中年龄在15和30之间的数据

select   *  from  student  where  age > "15"  between  age > "30";

in 包含

# 查询指定集合内的数据

select  *  from  student  where  id  in  (1,3,5);

排序

id升序 :  select  *  from  student  order  by  id  asc;

id降序 :  select  *  from  student  order  by  id  desc;

id 最大值: select  max(id)  from  student;

生日最小值:select   min(birth)  from  student;

id平均值: select  avg(id)  as  '求平均'  from  student;

统计数据:   select  count(*)  from  student

名字统计:   select  count(name)  from  student;(如果为空不统计)

id的和:       select  sum(id)  from  student

查询第 i 条以后的 j 条数据(不包括第i条):select  *  from  student  limit  2,5; #从第3条开始的5条数据(3-8)

# 修改id=2的age为66

update student set age=66 where id=2;

# 修改id=2的name和age

update student set name = "haha", birth = "1999-01-01" where id=2;

# 修改表名

alter table student rename to stu;

# 修改某个字段的名字

alter table stu change name names varchar(30);(修改字段name名为names)

# 修改表的默认值

alter table stu alter text set default 'system';

# 删除默认值

alter table stu alter text drop default;

# 增加列

alter table stu add (text char(10));

# 删除主键

alter table stu drop primary key;

# 删除表内所有数据但是不删除表

delete * from student;

# 删除一列

alter table student drop column birth;

# 添加一列

alter table student add column haha varchar(30);

# 删除一行

delete from student where id=6;

# 添加一行

insert into student(name,age,birth,class_id) values ("姜奕迪",18,"2000-11-11")

#---------------------------

#----cmd命令行连接MySql---------

cd C:\mysql\bin

# 启动mysql服务器

net start mysql

# 关闭mysql服务器

net stop mysql

# 进入mysql命令行

mysql -h localhost -u root -p

#---------------------------

#----MySql用户管理---------

#修改密码:首先在DOS 下进入mysql安装路径的bin目录下,然后键入以下命令:

mysqladmin -uroot -p123 password 456;

#增加用户

#格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by '密码'

/*

如,增加一个用户user1密码为password1,让其可以在本机上登录, 并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入mysql,然后键入以下命令:

grant select,insert,update,delete on *.* to user1@localhost Identified by "password1";

如果希望该用户能够在任何机器上登陆mysql,则将localhost改为"%"。

如果你不想user1有密码,可以再打一个命令将密码去掉。

grant select,insert,update,delete on mydb.* to user1@localhost identified by "";

*/

grant all privileges on wpj1105.* to sunxiao@localhost identified by '123'; #all privileges 所有权限

#----------------------------

#-----MySql数据库操作基础-----

# 创建数据库

create database namage default character set utf8 collate utf8_general_ci;

# 如果数据库存在删除

drop database if exists manage;

# 进入数据库

use manage;

# 删除数据库

drop manage;

# 查看表的结构

desc class;

# 查看表内数据

select * from class;

# 创建班级表并添加字段:

create table class(

id int(10) not null auto_increment,

name varchar(30) not null default " noname",

add_time datetime no t null,

primary key(id)

)

ENGINE = INNODB charset=utf8;

# 1、向表内添加2条数据:如果 add_time 字段为datetime

insert into class(name,add_time) values ("一年级","2018-08-31 15:33");

insert into class(name,add_time) values ("二年级","2018-08-31 15:33");

# 2、向表内添加2条数据:如果 add_time 字段为timestamp

insert into class(name) values ("一年级");

insert into class(name) values ("二年级");

# 创建学生表并添加字段:

CREATE table student(

id int(10) not null primary key auto_increment unique, # unique唯一性,不可重复

name varchar(30) not null default "noname " comment "名称",

age int(10) not null default 0 comment "年龄",

birthday datetime not null comment "生日",

class_id int(10) ,

foreign key(class_id) references class(id)

);

# 向表内添加4条数据:

insert into student(name,age,birthday,class_id) values ("卢宇蒙",23,"1996-07-11",1);

insert into student(name,age,birthday,class_id) values ("王志敏",23,"1996-08-12",1);

insert into student(name,age,birthday,class_id) values ("赵广正",23,"1996-09-13",2);

insert into student(name,age,birthday,class_id) values ("古川",23,"1996-10-14",2);

# 创建分数表并添加字段: decimal(5,2) 5是有效长度,2是小数点后2位

create table course(

id int (10) not null primary key auto_increment,

name varchar(30) not null ,

score DECIMAL(5,2) not null,

class_id int(10) not null,

stu_id int (10) not null,

foreign key(class_id) references class(id),

foreign key (stu_id) references student(id)

);

# 向表内添加5条数据:

insert into course(name,score,class_id,stu_id) values ("数学",90.6,1,1);

insert into course(name,score,class_id,stu_id) values ("语文","135.44",1,5);

insert into course(name,score,class_id,stu_id) values ("英语","100",2,3);

insert into course(name,score,class_id,stu_id) values ("政治","98",1,2);

insert into course(name,score,class_id,stu_id) values ("历史","89.92",2,4);

完成后如图所示:

class表:

a65f48eb2b8967ea3b90399a43ce4da9.png

student表:

f8218e35a837b84d03d7a474fd2d62ed.png

course表:

9fc2a7aee063e007bc924c76f80f960e.png

# 查找三张表里所有的数据:

SELECT * FROM student;

SELECT * FROM class;

SELECT * FROM course;

# 查询student表中id=1的name名

select name from student where id=1;

# 查询student表中name=“王志敏”的数据

select * from student where name = "王志敏";

# 查询student表中年龄大于15的数据

select id,name from student where age>"15";

and且;

# 查询student表中年龄大于15并且小于30的数据

select * from student where age>"15" and age

or 或;

# 查询student表中年龄大于15或小于30的数据

select * from student where age>"15" and age

between 之间;

# 查询student表中年龄在15和30之间的数据

select * from student where age > "15" between age > "30";

in 包含

# 查询指定集合内的数据

select * from student where id in (1,3,5);

排序

id升序 :  select * from student order by id asc;

id降序 :  select * from student order by id desc;

id 最大值: select max(id) from student;

生日最小值:select min(birth) from student;

id平均值: select avg(id) as '求平均' from student;

统计数据: select count(*) from student

名字统计: select count(name) from student;(如果为空不统计)

id的和: select sum(id) from student

查询第 i 条以后的 j 条数据(不包括第i条)

select * from student limit 2,5; #从第3条开始的5条数据(3-8)

# 修改id=2的age为66

update student set age=66 where id=2;

# 修改id=2的name和age

update student set name = "haha", birth = "1999-01-01" where id=2;

# 修改表名

alter table student rename to stu;

# 修改某个字段的名字

alter table stu change name names varchar(30);(修改字段name名为names);

# 修改表的默认值

alter table stu alter text set default 'system';

# 删除默认值

alter table stu alter text drop default;

# 增加列

alter table stu add (text char(10));

# 删除主键

alter table stu drop primary key;

# 删除表内所有数据但是不删除表

delete * from student;

# 删除一列

alter table student drop column birth;

# 添加一列

alter table student add column haha varchar(30);

# 删除一行

delete from student where id=6;  如果被关联,需要把关联的数据也删除

# 添加一行

insert into student(name,age,birth,class_id) values ("姜奕迪",18,"2000-11-11")

#---------------------------

#----数据库备份---------

# 导出数据

# 进入数据库存放位置  默认位置是C:\Program Files\MySQL\MySQL Server 5.5\bin  位置更改自行查找

cd C:\Users\memgmeng\Documents\Navicat\MySQL\servers\mysql1602A

08d1248f62d69b6009031670dffbeb30.png

#  输入mysqldump -u root -p manager > D:\manager.sql    manager是数据库名,> 后是备份的位置,然后输入密码,成功后如图:

eadbdb0075bf610accf136a7f4274582.png

# 导入数据

# 输入mysql -u root -p 和密码进入mysql

# 新建一个空的数据库,如mana

# 进入数据库:use mana;

52379074155b45e29a023c79b8564ca0.png

# 导入文件:source D:/manager.sql;  D:/与导入的文件之间不能有空格

29933bd83a006ca5450ca2808a3e7dbf.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值