二 . 常用mysql命令行命令1 .mysql的啟動(dòng)與停止 (windows下)
啟動(dòng)MYSQL服務(wù) net start mysql
停止MYSQL服務(wù) net stop mysql
在linux下:開(kāi)啟:/etc/init.d/mysqld start ?停止:/etc/init.d/mysqld stop ?重啟:/etc/init.d/mysqld restart
2 . netstat –na | findstr 3306 查看被監(jiān)聽(tīng)的端口 , findstr用于查找后面的端口是否存在
3 . 在命令行中登陸MYSQL控制臺(tái) , 即使用 MYSQL COMMEND LINE TOOL
語(yǔ)法格式 mysql –user=root –password=123456 db_name
或 mysql –u root –p123456 db_name
4 . 進(jìn)入MYSQL命令行工具后 , 使用status; 或/s 查看運(yùn)行環(huán)境信息
5 . 切換連接數(shù)據(jù)庫(kù)的語(yǔ)法 : use new_dbname;
6 . 顯示所有數(shù)據(jù)庫(kù) : show databases;
7 . 顯示數(shù)據(jù)庫(kù)中的所有表 : show tables;
8 . 顯示某個(gè)表創(chuàng)建時(shí)的全部信息 : show create table table_name;
9 . 查看表的具體屬性信息及表中各字段的描述
Describe table_name; 縮寫(xiě)形式 : desc table_name;
三 MySql中的SQL語(yǔ)句?1 . 數(shù)據(jù)庫(kù)創(chuàng)建 : Create database db_name;
數(shù)據(jù)庫(kù)刪除 : Drop database db_name; 刪除時(shí)可先判斷是否存在,寫(xiě)成 : drop database if exits db_name
2 . 建表 : 創(chuàng)建數(shù)據(jù)表的語(yǔ)法 : create table table_name (字段1 數(shù)據(jù)類(lèi)型 , 字段2 數(shù)據(jù)類(lèi)型);
例 : create table mytable (id int , username char(20));
刪表 : drop table table_name; 例 : drop table mytable;
8 . 添加數(shù)據(jù) : Insert into 表名 [(字段1 , 字段2 , ….)] values (值1 , 值2 , …..);
如果向表中的每個(gè)字段都插入一個(gè)值,那么前面 [ ] 括號(hào)內(nèi)字段名可寫(xiě)也可不寫(xiě)
例 : insert into mytable (id,username) values (1,’zhangsan’);
9 . 查詢(xún) : 查詢(xún)所有數(shù)據(jù) : select * from table_name;
查詢(xún)指定字段的數(shù)據(jù) : select 字段1 , 字段2 from table_name;
例 : select id,username from mytable where id=1 order by desc;多表查詢(xún)語(yǔ)句-
10 . 更新指定數(shù)據(jù) , 更新某一個(gè)字段的數(shù)據(jù)(注意嗽仪,不是更新字段的名字)
Update table_name set 字段名=’新值’ [, 字段2 =’新值’ , …..][where id=id_num] [order by 字段 順序]
例 : update mytable set username=’lisi’ where id=1;
Order語(yǔ)句是查詢(xún)的順序 , 如 : order by id desc(或asc) , 順序有兩種 : desc倒序(100—1,即從最新數(shù)據(jù)往后查詢(xún)),asc(從1-100),Where和order語(yǔ)句也可用于查詢(xún)select 與刪除delete
11 . 刪除表中的信息 :
刪除整個(gè)表中的信息 : delete from table_name;
刪除表中指定條件的語(yǔ)句 : delete from table_name where 條件語(yǔ)句 ; 條件語(yǔ)句如 : id=3;
12 . 創(chuàng)建數(shù)據(jù)庫(kù)用戶(hù)
一次可以創(chuàng)建多個(gè)數(shù)據(jù)庫(kù)用戶(hù)如:
CREATE USER username1 identified BY ‘password’ , username2 IDENTIFIED BY ‘password’….
13 . 用戶(hù)的權(quán)限控制:grant
庫(kù)要糊,表級(jí)的權(quán)限控制 : 將某個(gè)庫(kù)中的某個(gè)表的控制權(quán)賦予某個(gè)用戶(hù)
Grant all ON db_name.table_name TO user_name [ indentified by ‘password’ ];
14 . 表結(jié)構(gòu)的修改
(1)增加一個(gè)字段格式:
alter table table_name add column (字段名 字段類(lèi)型); ----此方法帶括號(hào)
(2)指定字段插入的位置:
alter table table_name add column 字段名 字段類(lèi)型 after 某字段;
刪除一個(gè)字段:
alter table table_name drop字段名;
(3)修改字段名稱(chēng)/類(lèi)型
alter table table_name change 舊字段名 新字段名 新字段的類(lèi)型;
(4)改表的名字
alter table table_name rename to new_table_name;
(5)一次性清空表中的所有數(shù)據(jù)
truncate table table_name; 此方法也會(huì)使表中的取號(hào)器(ID)從1開(kāi)始
15 . 增加主鍵妆丘,外鍵锄俄,約束局劲,索引。珊膜。容握。宣脉。(使用方法見(jiàn)17實(shí)例)
① 約束(主鍵Primary key车柠、唯一性Unique、非空Not Null)
② 自動(dòng)增張 auto_increment
③外鍵Foreign key-----與reference table_name(col_name列名)配合使用塑猖,建表時(shí)單獨(dú)使用
④ 刪除多個(gè)表中有關(guān)聯(lián)的數(shù)據(jù)----設(shè)置foreign key 為set null ---具體設(shè)置參考幫助文檔
16 . 查看數(shù)據(jù)庫(kù)當(dāng)前引擎
SHOW CREATE TABLE table_name;
修改數(shù)據(jù)庫(kù)引擎
ALTER TABLE table_name ENGINE=MyISAM | InnoDB;
17 . SQL語(yǔ)句運(yùn)用實(shí)例:
--1 建users表
create table users (id int primary key auto_increment,nikename varchar(20) not null unique,password varchar(100) not null,address varchar(200), reg_date timestamp not null default CURRENT_TIMESTAMP);
--2 建articles表,在建表時(shí)設(shè)置外鍵
create table articles (id int primary key auto_increment,content longtext not null,userid int,constraint foreign key (userid) references users(id) on delete set null);
-----------------------------------------------------------------------
--2.1 建articles表,建表時(shí)不設(shè)置外鍵
create table articles (id int primary key auto_increment,content longtext not null,userid int);
--2.2 給articles表設(shè)置外鍵
alter table articles add constraint foreign key (userid) references users(id) on delete set null;
------------------------------------------------------------------------
--3. 向users表中插入數(shù)據(jù),同時(shí)插入多條
insert into users (id,nikename,password,address) values (1,'lyh1','1234',null),(10,'lyh22','4321','湖北武漢'),(null,'lyh333','5678', '北京海淀');
--4. 向article中插入三條數(shù)據(jù)
insert into articles (id,content,userid) values (2,'hahahahahaha',11),(null,'xixixixixix',10),(13,'aiaiaiaiaiaiaiaiaiaiaiaia',1),(14,'hohoahaoaoooooooooo',10);
--5. 進(jìn)行多表查詢(xún)竹祷,選擇users表中ID=10的用戶(hù)發(fā)布的所有留言及該用戶(hù)的所有信息
select articles.id,articles.content,users.* from users,articles where users.id=10 and articles.userid=users.id order by articles.id desc;
--6. 查看數(shù)據(jù)庫(kù)引擎類(lèi)型
show create table users;
--7. 修改數(shù)據(jù)庫(kù)引擎類(lèi)型
alter table users engine=MyISAM;?---因?yàn)閡sers表中ID被設(shè)置成外鍵,執(zhí)行此句會(huì)出錯(cuò)
--8. 同表查詢(xún),已知一個(gè)條件的情況下.查詢(xún)ID號(hào)大于用戶(hù)lyh1的ID號(hào)的所有用戶(hù)
select a.id,a.nikename,a.address from users a,users b where b.nikename='lyh1' and a.id>b.id;
------也可寫(xiě)成
select id,nikename,address from users where id>(select id from users where nikename='lyh1');
9. 顯示年齡比領(lǐng)導(dǎo)還大的員工:
select a.name from users a,users b where a.managerid=b.id and a.age>b.age;
查詢(xún)編號(hào)為2的發(fā)帖人: 先查articles表,得到發(fā)帖人的編號(hào),再根據(jù)編號(hào)查users得到的用戶(hù)名羊苟。
接著用關(guān)聯(lián)查詢(xún).
select * from articles,users得到笛卡兒積,再加order by articles.id以便觀(guān)察
使用select * from articles,users where articles.id=2 篩選出2號(hào)帖子與每個(gè)用戶(hù)的組合記錄
再使用select * from articles,users where articles.id=2 and articles.userid=users.id選出users.id等于2號(hào)帖的發(fā)帖人id的記錄.
只取用戶(hù)名:select user where user.id=(select userid from articles where article.id =2)
找出年齡比小王還大的人:假設(shè)小王是28歲,先想找出年齡大于28的人
select * from users where age>(select age from users where name='xiaowang');
*****要查詢(xún)的記錄需要參照表里面的其他記錄:
select a.name from users a,users b where b.name='xiaowang' and a.age>b.age
表里的每個(gè)用戶(hù)都想pk一下.select a.nickname,b.nickname from users a,users b where a.id>b.id ;
更保險(xiǎn)的語(yǔ)句:select a.nickname,b.nickname from (select * from users order by id) a,(se
lect * from users order by id) b where a.id>b.id ;
再查詢(xún)某個(gè)人發(fā)的所有帖子.
select b.* from articles a , articles b where a.id=2 and a.userid=b.userid