select * from emp;
注釋#---------------------------#----命令行連接MySql---------
啟動mysql服務(wù)器net start mysql
關(guān)閉 net stop mysql #進入mysql -h 主機地址 -u 用戶名 -p 用戶密碼
退出exit
---------------------------#----MySql用戶管理---------
修改密碼:首先在DOS 下進入mysql安裝路徑的bin目錄下,然后鍵入以下命令:mysqladmin -uroot -p123 password 456;
增加用戶#格式:grant 權(quán)限 on 數(shù)據(jù)庫.* to 用戶名@登錄主機 identified by '密碼'/如,增加一個用戶user1密碼為password1,讓其可以在本機上登錄, 并對所有數(shù)據(jù)庫有查詢、插入、修改、刪除的權(quán)限。首先用以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 所有權(quán)限
----------------------------#-----MySql數(shù)據(jù)庫操作基礎(chǔ)-----
顯示數(shù)據(jù)庫show databases;
判斷是否存在數(shù)據(jù)庫wpj1105,有的話先刪除drop database if exists wpj1105;
創(chuàng)建數(shù)據(jù)庫create database wpj1105;
刪除數(shù)據(jù)庫drop database wpj1105;
使用該數(shù)據(jù)庫use wpj1105;
顯示數(shù)據(jù)庫中的表show tables;
先判斷表是否存在,存在先刪除drop table if exists student;
創(chuàng)建表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;
查看表的結(jié)構(gòu)describe student; #可以簡寫為desc student;
插入數(shù)據(jù)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','......');#查詢表中的數(shù)據(jù)select * from student;select id,name from student;
修改某一條數(shù)據(jù)update student set sex='男' where id=4;
刪除數(shù)據(jù)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'; #betweenselect * from student where date between '1988-1-2' and '1988-12-1';
in 查詢制定集合內(nèi)的數(shù)據(jù)select * from student where id in (1,3,5);
排序 asc 升序 desc 降序select * from student order by id asc;
分組查詢 #聚合函數(shù) 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; #統(tǒng)計表中總數(shù)
select count(sex) from student; #統(tǒng)計表中性別總數(shù) 若有一條數(shù)據(jù)中sex為空的話,就不予以統(tǒng)計~
select sum(id) from student;
查詢第i條以后到第j條的數(shù)據(jù)(不包括第i條)select * from student limit 2,5; #顯示3-5條數(shù)據(jù)
鞏固練習(xí)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 #不可重復(fù));
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;
修改數(shù)據(jù) update c set age=66 where id=2;update c set name='花花',age=21,sex='女' where id=2delete from c where age=21;
常用查詢語句select name,age ,id from cselect * from c where age>40 and age<60; #andselect * from c where age<40 or age<60; #orselect * from c where age between 40 and 60 #betweenselect * from c where age in (30,48,68,99); #in 查詢指定集合內(nèi)的數(shù)據(jù)select * from c order by age desc; order by (asc升序 des降序)
分組查詢select name,max(age) from c group by sex; #按性別分組查年齡最大值#聚合函數(shù)select min(age) from c;select avg(age) as '平均年齡 ' from c;select count() from c; #統(tǒng)計表中數(shù)據(jù)總數(shù)select sum(age) from c;
修改表的名字#格式:alter table tbl_name rename to new_namealter table c rename to a; #表結(jié)構(gòu)修改create table test(id int not null auto_increment primary key, #設(shè)定主鍵name varchar(20) not null default 'NoName', #設(shè)定默認值department_id int not null,position_id int not null,unique (department_id,position_id) #設(shè)定唯一值);
修改表的名字#格式:alter table tbl_name rename to new_namealter 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 增加列testalter table position add(test char(10));#表position 修改列testalter 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 去掉列testalter 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);
用文本方式將數(shù)據(jù)裝入數(shù)據(jù)庫表中(例如D:/mysql.txt)load data local infile "D:/mysql.txt" into table MYTABLE;
導(dǎo)入.sql文件命令(例如D:/mysql.sql)source d:/mysql.sql; #或者 /. d:/mysql.sql;
mysql 語法
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 前日與老怪大戰(zhàn)八十回合握手言和,今日約戰(zhàn)在此,定要分出勝負,歡迎圍觀。 了解前日大戰(zhàn),請移步此文,看評論。 老怪接...