標籤:identified password 修改密碼 使用者名稱 伺服器
select * from emp; #注釋
#---------------------------
#----命令列串連MySql---------
#啟動mysql伺服器
net start mysql
#關閉
net stop mysql
#進入
mysql -h 主機地址 -u 使用者名稱 -p 使用者密碼
#退出
exit
#---------------------------
#----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 [email protected] Identified by "password1";
如果希望該使用者能夠在任何機器上登陸mysql,則將localhost改為"%"。
如果你不想user1有密碼,可以再打一個命令將密碼去掉。
grant select,insert,update,delete on mydb.* to [email protected] identified by "";
*/
grant all privileges on wpj1105.* to [email protected] identified by ‘123‘; #all privileges 所有許可權
#----------------------------
#-----MySql資料庫操作基礎-----
#顯示資料庫
show databases;
#判斷是否存在資料庫wpj1105,有的話先刪除
drop database if exists wpj1105;
#建立資料庫
create database wpj1105;
#刪除資料庫
drop database wpj1105;
#使用該資料庫
use wpj1105;
#顯示資料庫中的表
show tables;
#先判斷表是否存在,存在先刪除
drop table if exists student;
#建立表
create table student(
id int auto_increment primary key,
name varchar(50),
sex varchar(20),
date varchar(50),
content varchar(100)
)default charset=utf8;
#刪除表
drop table student;
#查看錶的結構
describe student; #可以簡寫為desc student;
#插入資料
insert into student values(null,‘aa‘,‘男‘,‘1988-10-2‘,‘......‘);
insert into student values(null,‘bb‘,‘女‘,‘1889-03-6‘,‘......‘);
insert into student values(null,‘cc‘,‘男‘,‘1889-08-8‘,‘......‘);
insert into student values(null,‘dd‘,‘女‘,‘1889-12-8‘,‘......‘);
insert into student values(null,‘ee‘,‘女‘,‘1889-09-6‘,‘......‘);
insert into student values(null,‘ff‘,‘null‘,‘1889-09-6‘,‘......‘);
#查詢表中的資料
select * from student;
select id,name from student;
#修改某一條資料
update student set sex=‘男‘ where id=4;
#刪除資料
delete from student where id=5;
# and 且
select * from student where date>‘1988-1-2‘ and date<‘1988-12-1‘;
# or 或
select * from student where date<‘1988-11-2‘ or date>‘1988-12-1‘;
#between
select * from student where date between ‘1988-1-2‘ and ‘1988-12-1‘;
#in 查詢制定集合內的資料
select * from student where id in (1,3,5);
#排序 asc 升序 desc 降序
select * from student order by id asc;
#分組查詢 #彙總函式
select max(id),name,sex from student group by sex;
select min(date) from student;
select avg(id) as ‘求平均‘ from student;
select count(*) from student; #統計表中總數
select count(sex) from student; #統計表中性別總數 若有一條資料中sex為空白的話,就不予以統計~
select sum(id) from student;
#查詢第i條以後到第j條的資料(不包括第i條)
select * from student limit 2,5; #顯示3-5條資料
#鞏固練習
create table c(
id int primary key auto_increment,
name varchar(10) not null,
sex varchar(50) , #DEFAULT ‘男‘ ,
age int unsigned, #不能為負值(如為負值 則預設為0)
sno int unique #不可重複
);
drop table c;
desc c;
insert into c (id,name,sex,age,sno) values (null,‘濤哥‘,‘男‘,68,1);
insert into c (id,name,sex,age,sno) values (null,‘aa‘,‘男‘,68,2);
insert into c (id,name,sex,age,sno) values (null,‘平平‘,‘男‘,35,3);
...
select * from c;
#修改資料
update c set age=66 where id=2;
update c set name=‘花花‘,age=21,sex=‘女‘ where id=2
delete from c where age=21;
#常用查詢語句
select name,age ,id from c
select * from c where age>40 and age<60; #and
select * from c where age<40 or age<60; #or
select * from c where age between 40 and 60 #between
select * from c where age in (30,48,68,99); #in 查詢指定集合內的資料
select * from c order by age desc; #order by (asc升序 des降序)
#分組查詢
select name,max(age) from c group by sex; #按性別分組查年齡最大值
#彙總函式
select min(age) from c;
select avg(age) as ‘平均年齡 ‘ from c;
select count(*) from c; #統計表中資料總數
select sum(age) from c;
#修改表的名字
#格式:alter table tbl_name rename to new_name
alter table c rename to a;
#表結構修改
create table test
(
id int not null auto_increment primary key, #設定主鍵
name varchar(20) not null default ‘NoName‘, #設定預設值
department_id int not null,
position_id int not null,
unique (department_id,position_id) #設定唯一值
);
#修改表的名字
#格式:alter table tbl_name rename to new_name
alter table test rename to test_rename;
#向表中增加一個欄位(列)
#格式:alter table tablename add columnname type;/alter table tablename add(columnname type);
alter table test add columnname varchar(20);
#修改表中某個欄位的名字
alter table tablename change columnname newcolumnname type; #修改一個表的欄位名
alter table test change name uname varchar(50);
select * from test;
#表position 增加列test
alter table position add(test char(10));
#表position 修改列test
alter table position modify test char(20) not null;
#表position 修改列test 預設值
alter table position alter test set default ‘system‘;
#表position 去掉test 預設值
alter table position alter test drop default;
#表position 去掉列test
alter table position drop column test;
#表depart_pos 刪除主鍵
alter table depart_pos drop primary key;
#表depart_pos 增加主鍵
alter table depart_pos add primary key PK_depart_pos
(department_id,position_id);
#用文本方式將資料裝入資料庫表中(例如D:/mysql.txt)
load data local infile "D:/mysql.txt" into table MYTABLE;
#匯入.sql檔案命令(例如D:/mysql.sql)
source d:/mysql.sql; #或者 /. d:/mysql.sql;
本文出自 “信不信由你” 部落格,請務必保留此出處http://312461613.blog.51cto.com/965442/1533554