select * from emp; #註釋 #--------------------------- #----命令行鏈接MySql--------- #啓動mysql服務器 net start mysql #關閉 net stop mysql #進入 mysql -h 主機地址 -u 用戶名 -p 用戶密碼 #退出 exit status; 顯示當前mysql的version的各類信息。 #--------------------------- #----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數據庫操做基礎----- #顯示數據庫 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;