mysql基礎筆記

來源:互聯網
上載者:User

標籤:

mysql如何串連?
(cmd)>>>mysql -h localhost -u username -p password
-h 如果不寫,預設則是localhost
以;或者/g命令結束
\c退出
顯示多個資料庫:show databases;
建立一個資料庫表:create database 表名;如:create database p1;
當出現這個資訊是“ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that
corresponds to your MySQL server version for the right syntax to use near ‘datab
ases p1‘ at line 1”
必須重新串連mysql,要寫完整mysql -h localhost -u username -p password這個,不然無法建立資料

選擇資料庫表名:use 表名;如use p1;
查看庫:show tables;
刪除某個庫:drop databases 表名;
修改某個庫:無法修改
修改表名:rename table oldName to newName;
查看某個庫下面的所有的表:show tables;
如何刪除一張表:drop table 表名;
清空表資料: truncate 表名
在於 truncate相當於刪表再重建一張同樣結構的表,操作後得到一張全新表.
而delete是從刪除所有的層面來操作的.
Trucate相當於把舊的學籍表扔了重畫一張,
Delete相當於用橡皮把學籍表的資料庫擦掉.
如果決定全清空的下,truncate速度更快一些.

建立一個表語句
create table 表名(name1 資料類型1,name2 資料類型2,
....namen 資料類型n)ngine myisam charset utf8;///engine是指表引擎,
查看錶結構:desc tableName;

如何破解資料庫的密碼:
1:通過工作管理員或者服務管理,關掉mysqld(服務進程)
2:通過命令列+特殊參數開啟mysqld
Mysqld --skip-grant-tables
3:此時,mysqld服務進程已經開啟,並且,不需要許可權檢查.
4:mysql -uroot 無密碼登陸伺服器.
5: 修改許可權表
A: use mysql;
B:update user set Password = password(‘11111‘) where User = ‘root‘;
C:flush privileges;

--------------
修改表的文法
Alter table 表名 add 列名稱 列類型 列參數; [加的列在表的最後
Alter table 表名 add 列名稱 列類型 列參數 after 某列 [把新列加在某列後]
Alter table 表名 add 列名稱 列類型 列參數 first [把新列加在最前面]
刪除列:
Alter table 表名 drop 列名
修改列類型:
Alter table 表名 modify 列名 新類型 新參數
修改列名及列類型
Alter table 表名 change 舊列名 新列名 新類型 新參數

如何讓表中不出現null值:加not null default ‘‘ / default 0

為什麼不想要的null的值
答:
不好比較,null是一種類型,比較時,只能用專門的is null 和 is not null來比較.
碰到運算子,一律返回null
效率不高,影響提高索引效果.

因此,我們往往,在建表時 not null default ‘‘/0
--------------

聲明字元集
set names 類型(gbk/utf-8)
增加資料

insert into 表名(列1,列2,...,列n) values(值1,值2,....,值n);
插入多行,如下
insert into 表名(列1,列2,...,列n) values(值1,值2,....,值n),(值a,值b,值c,...,值an);
更新
udate 表名
set 列1=新的資訊1,列2=資訊2 where 名字=新值;

查詢
select*from 表名;
select 列1,列2,列n from 表名;
select*from 表名 where 運算式;
select 列1,列2,列n from 表名 where運算式;
刪除
def from 表名 where 運算式;

列類型
整形
類型 位元組 位 無符號 有符號
tinyint 1 8 0-2的8次-1 -2的7次到2的次7-1
smallint 2 16 0-2的16-1 -2的15到2的15-1
mediumint3 24 0到2的24-1 -2的24到2的24-1
int 4 32 0的2的32次-1 -2的32到2的32-1


tinyint預設是有符號的:-128到127
整形參數可選屬性
tinyint(M)unsigned zerofill
M:表示寬度(zerofill,0填充時才有意義)
unsigned:無符號類型(非負)
zerofill:0填充(預設無符號)
not null default 0設定預設值為0
列類型primary key auto_increment 設定為主鍵,自增長


比較null的方法
null is null
or
null is not null

浮點
float(M,D)
decimal(M,D)更精確
M精度
D標度
例(6,2):6666.66

mysql基礎筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.