# # cmd命令行連接MySql cd C:\Program Files\MySQL\MySQL Server 5.5\bin # 啟動mysql伺服器net start mysql # 關閉mysql伺服器net stop mysql # 進入mysql命令行 mysql -h localhos ...
#---------------------------
#----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用戶管理---------
#修改密碼:首先在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<"30";
or 或;
# 查詢student表中年齡大於15或小於30的數據
select * from student where age>"15" and age<"30";
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表:
student表:
course表:
# 查找三張表裡所有的數據:
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<"30";
or 或;
# 查詢student表中年齡大於15或小於30的數據
select * from student where age>"15" and age<"30";
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
# 輸入mysqldump -u root -p manager > D:\manager.sql manager是資料庫名,> 後是備份的位置,然後輸入密碼,成功後如圖:
# 導入數據
# 輸入mysql -u root -p 和密碼進入mysql
# 新建一個空的資料庫,如mana
# 進入資料庫:use mana;
# 導入文件:source D:/manager.sql; D:/與導入的文件之間不能有空格