數據庫MySQL-筆記
打開數據庫
CMD mysql -h主機名或IP -u用戶名 -p密碼 mysql -hlocalhost -uroot -proot-h:代表MySQL的主機名或IP地址,如:-h127.0.01 -hlocalhost
-u:代表MySQL中的用戶名,默認是root
-p:代表MySQL中用記的密碼,默認是root
註意:語法中各個段之間用空格分開;
如果你不想讓別人看到你輸入的密碼,在登錄MySQL客戶端可以先不輸密碼,直接回車,會提示輸入密碼,這時候的密是以“*”號顯示;
提示:安裝完phpStudy之後,只有一個root用戶,它是超級管理員。
查詢顯示所有數據庫 show databases;
Mysql>set password=password(‘新密碼’);
註意:password( )是MySQL的一個加密函數
md5( )是PHP中的一個加密函數
創建數據庫
Create Darabase [IF NOT EXISTS]
創建的命令-是否存在-數據庫名字-字符集
Create Database IF NOT EXISTS kongkong CHARSET utf8; Create Database kongkong ; 默認字符集拉丁文 刪除數據庫 DROP DATABASE [IF EXISTS] db_name; 刪除的命令-是否存在-數據庫名字 選擇數據庫 USE db_name; use kongkong; →更改數據庫默認字更改MySQL的配置文件:C:\Program Files (x86)\phpStudy\MySQL\my.ini
客戶端(Client Section):default-character-set=gbk
服務器端(Server Section):default-character-set=latin1
在MySQL客戶端命令修改
Alter database db_name Default character Set gbk;
數據表操作
顯示當前數據庫中的所有表(先use 進入某表)
show tables ;顯示所有表
show tables from kongkong; 顯示其中一個表
顯示剛創建的數據庫格式結構
show create dababase name_db
查看的結構 show create database kongkong;
顯示剛創建表的格式結構
show create table kongkong
刪除數據表,從哪個庫中刪除
drop table kongkongbiao from kongkong
表名 在哪個 數據庫 (只有一個表可以不寫從哪個庫
修改表.
顯示表結構,顯示某個表的結構
describe table_name
創建數據表 create table table_name( 列名1 列類型 列屬性, 同上, )列名1,指定每個字段的名稱,命名跟變量一樣;
列的數據類型:指定每個字段存儲什麽樣的數據;
列的屬性:對列更詳細的設置
create table table_name( 名字 整形 不空 自增 主鍵 id int not null auto_increment primary key, title varchar(50) not null, content text null, addate int(12) no null sex tinyint not null DEFAULT 1默認1 ); create table table_name( uid int not null auto_increment primary key, keyword varchar(50) not null, msgType varchar(50) not null, contentStr text not null ); create table s_user( u_id int not null auto_increment primary key, u_name varchar(20) not null, u_phone int(11) not null, u_email varchar(200) not null, u_qq int null, u_wx int(20) not null, u_age int(3) not null, u_sex tinyint not null DEFAULT 1, u_auth int(3) not null, u_card int(3) not null ); 設置DOS環境的字符集 set names gbk; 顯示特定字段 select id,title,addate from table_name select * from news; 插入表內容 insert into table_name(title,id)value(‘新聞‘,‘1‘); insert into table_name(`keyword`,`msgType`,`contentStr`)values(‘列表‘,‘text‘,‘1.遊戲\n 2.小說\n 3.新聞\n‘) insert into table_name(`keyword`,`msgType`,`contentStr`)value(‘列表‘,‘text‘,‘1.遊戲\n 2.小說\n 3.新聞\n‘)
刪除記錄-不能忘記寫條件 delete from table_name[where條件] delete from kongkong where id<=3
delete from kongkong where id<=3 and id>20
delete from kongkong where id<=3 or authon=‘值admin‘
清空所有數據ID歸零,比上面刪的快 truncate table_name 修改記錄,更新數據一定要條件 update table_name set 字段1=新值1,字段2=新值2[where條件] UPDATE new SET auto=‘zhangxin‘,his=1000 WHERE id=120 update kongkong set parent=5 where id=15,把15改成5 update 表名 set 作者 =“張三” where id=15//修改id=15的作者
查詢數據 select* |字段from table_name [where條件][order by 字段(排序)asc|desc][limit 限定輸出結果] 哪個字段 從哪個表中查 查詢條件 排序(ASC默認升序)(DESC降序) SELECT title,id FROM 表名 ; SELECT title,id FROM 表名 WHERE title LIKE "%廣州%";//模糊查詢,標題有廣州的 SELECT title,id FROM 表名 WHERE title LIKE "廣州%";//模糊查詢,標題有廣州開頭的 SELECT title,id FROM 表名 WHERE id between 50 and 100;//條件 查詢 50到100之間 SELECT title,id FROM 表名 ORDER BY id ASC;//對id降序排列 SELECT title,id FROM 表名 ORDER BY id DESC,time ASC;//對id降序排列 時間為升序排列; SELECT title,id FROM 表名 ORDER BY id DESC,time ASC LIMIT 0,10;//從第0行起輸出10條記錄,不包括第0行,用語網頁數據分頁 LIKE模糊查詢=% select 表列 from 表名 untion all select
數據庫MySQL-筆記