發(fā)展壯大離不開廣大客戶長期以來的信賴與支持,我們將始終秉承“誠信為本、服務(wù)至上”的服務(wù)理念,堅(jiān)持“二合一”的優(yōu)良服務(wù)模式,真誠服務(wù)每家企業(yè),認(rèn)真做好每個(gè)細(xì)節(jié),不斷完善自我,成就企業(yè),實(shí)現(xiàn)共贏。行業(yè)涉及成都發(fā)電機(jī)維修等,在重慶網(wǎng)站建設(shè)公司、全網(wǎng)營銷推廣、WAP手機(jī)網(wǎng)站、VI設(shè)計(jì)、軟件開發(fā)等項(xiàng)目上具有豐富的設(shè)計(jì)經(jīng)驗(yàn)。
mysql數(shù)據(jù)庫屬于關(guān)系型數(shù)據(jù)庫,關(guān)系型數(shù)據(jù)庫是指采用關(guān)系模型來組織數(shù)據(jù)的數(shù)據(jù)庫,似于Excel函數(shù)關(guān)系表。保持?jǐn)?shù)據(jù)的一致性是關(guān)系型數(shù)據(jù)庫的最大優(yōu)勢(shì)
關(guān)系型數(shù)據(jù)庫瓶頸:
1.高并發(fā)讀寫硬盤I/O瓶頸;
2.對(duì)于關(guān)系型數(shù)據(jù)來說在一張海量數(shù)據(jù)表中查詢效率是非常低的;
3.橫向擴(kuò)展困難,當(dāng)一個(gè)應(yīng)用系統(tǒng)用戶量訪問與日俱增的時(shí)候數(shù)據(jù)庫沒有辦法像web server那樣簡單的通過添加更多的硬件或者節(jié)點(diǎn)來擴(kuò)展和提供負(fù)載能力。
對(duì)于并發(fā)量不高及數(shù)據(jù)量較小的應(yīng)用系統(tǒng)mysql還是占有很大優(yōu)勢(shì),且目前mysql屬于開源軟件,且目前mysql的衍生數(shù)據(jù)庫mariadb也正在快速發(fā)育中。
SQL
sql:用戶與數(shù)據(jù)庫交付語言,被稱為sql,sql是 Structured Query Language 結(jié)構(gòu)話查詢語言簡稱,是關(guān)系型數(shù)的數(shù)據(jù)庫應(yīng)用語言;
sql分類
DDL:數(shù)據(jù)定義語句
這些語句定義了不同數(shù)據(jù)段,數(shù)據(jù)庫,列,表,索引等數(shù)據(jù)庫對(duì)象。常用關(guān)鍵字包括create ,drop ,alter
DML:數(shù)據(jù)操縱語句
用于添加、刪除、更新、、和查詢數(shù)據(jù)庫記錄、常用語句關(guān)鍵字包括:insert、delete、update、select
DCL:數(shù)據(jù)控制語言
用于控制不同數(shù)據(jù)段之前的許可和訪問級(jí)別語句,這些語句定義了數(shù)據(jù)庫、表、字段、用戶訪問、權(quán)限和安全級(jí)別,主要的語句關(guān)鍵字包括grant、revoke。
DDL語句
DDL 是數(shù)據(jù)定義的縮寫,簡單來說就是對(duì)數(shù)據(jù)庫內(nèi)部對(duì)象進(jìn)行創(chuàng)建、刪除、修改等操作的語言,與DML的區(qū)別是DML只是對(duì)表內(nèi)部數(shù)據(jù)進(jìn)行操作,而不會(huì)涉及到定義結(jié)構(gòu)的修改更不會(huì)涉及其他對(duì)象,DDL有數(shù)據(jù)庫管理員使用。
庫操作
1.數(shù)據(jù)庫創(chuàng)建
啟動(dòng)數(shù)據(jù)庫連接到mysql服務(wù)器使用
[root@localhost ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
當(dāng)輸入一條命令要其執(zhí)行后面加上;或者\(yùn)g結(jié)束,如果不添加默認(rèn)為換行繼續(xù)輸入命令。
使用help;或者\(yùn)h命令來獲取幫助,通過\c命令來清除命令行buffer。
數(shù)據(jù)庫創(chuàng)建語法如下:
CREATE DATABASE DBNAME;
例如:
mysql> CREATE DATABASE liaodb; Query OK, 1 row affected (0.00 sec)
Query OK 表示創(chuàng)建成功,(DML和DDL所有的操作不包括select操作成功后都會(huì)顯示 Query OK)
1 row affected 表示操作只影響了一行記錄;
(0.00 sec)表示操作執(zhí)行的時(shí)間
2.查看數(shù)據(jù)庫
語法:
SHOW DATABASES;
mysql> show DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | liaodb | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec)
默認(rèn)存在的數(shù)據(jù):
information_schema 數(shù)據(jù)對(duì)象信息存儲(chǔ),例如用戶表信息。
mysql 存儲(chǔ)了系統(tǒng)的用戶權(quán)限等信息
test系統(tǒng)自動(dòng)創(chuàng)建的測試數(shù)據(jù)庫,所有用戶都可以使用;
3.選擇數(shù)據(jù)庫
語法:
USE DBNAME;
mysql> USE mysql; Database changed
4.刪除數(shù)據(jù)庫
刪除數(shù)據(jù)語法
drop database DBNAME;
刪除我們剛剛創(chuàng)建的數(shù)據(jù)庫liaodb
mysql> drop database liaodb; Query OK, 0 rows affected (0.00 sec)
注:
刪除數(shù)據(jù)庫謹(jǐn)慎操作,默認(rèn)刪除數(shù)據(jù)庫后下面的表會(huì)一起被刪除。
表操作
1.創(chuàng)建表
語法
CREATE TABLE TABLENAME(column_name_1 colemn_type_1 constraints,olumn_name_2 colemn_type_2 constraintsolumn_name_3 colemn_type_3 constraints)
column_name_1 是列的名字;column_type是列的數(shù)據(jù)類型;constraints是列的約束條件。
下面創(chuàng)建一個(gè)最常見的普通班級(jí)統(tǒng)計(jì)表
表包括name,age,sex,字段類型使用varchar(10) ,date ,varchar(1)
mysql> create table class_one(name varchar(10),age INT(3),sex varchar(1)); Query OK, 0 rows affected (0.02 sec) mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | class_one | +----------------+ 1 row in set (0.00 sec)
2.查看表的定義
語法:
DESC TABLENAME;
mysql> DESC class_one; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | name | varchar(10) | YES | | NULL | | | age | int(3) | YES | | NULL | | | sex | varchar(1) | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
查看更為詳細(xì)的表定義信息
show create table tablename \G; mysql> show create table class_one \G; *************************** 1. row *************************** Table: class_one Create Table: CREATE TABLE `class_one` ( `name` varchar(10) DEFAULT NULL, `age` int(3) DEFAULT NULL, `sex` varchar(1) DEFAULT NULL, ENGINE=MyISAM DEFAULT CHARSET=latin1 1 row in set (0.00 sec) ERROR: No query specified
\G選項(xiàng)的含義是使得記錄能夠按照字段豎向排列。
3.刪除表
DROP TABLE TABLENAME; mysql> DROP TABLE class_one; Query OK, 0 rows affected (0.00 sec)
4.修改表
1)修改表類型語法如下:
ALTER TABLE tablename MODIFY[COLUMN] column_definition [FIRST|AFTER col_name]
例如:將之前制作的class_one表中的name字段類型修改為改為verchar(20)
mysql> alter table class_one modify name varchar(20); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table class_one \G; *************************** 1. row *************************** Table: class_one Create Table: CREATE TABLE `class_one` ( `name` varchar(20) DEFAULT NULL, `age` int(3) DEFAULT NULL, `sex` varchar(1) DEFAULT NULL, `deptno` int(2) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 1 row in set (0.00 sec) ERROR: No query specified
2)增加表字段語法如下
ALTER TABLE tablename ADD [COLUMN] column_definition [FIRST|AFTER col_name]
FIRST|AFTER
定義新增的一條字段放置在那條字段前FIRST或者后AFTER
例如在class_one表中添加一條 fraction
mysql> alter table class_one add column fraction int(3); Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc class_one; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | name | varchar(15) | YES | | NULL | | | age | int(3) | YES | | NULL | | | sex | varchar(1) | YES | | NULL | | | fraction | int(3) | YES | | NULL | | +----------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
3)刪除表中字段語句如下
ALTER TABLE tablename drop [COLUMN] column_definition [FIRST|col_name]
例如將class中fraction和deptno字段刪除
mysql> desc class_one; +----------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+------------+------+-----+---------+-------+ | name | varchar(6) | YES | | NULL | | | age | int(3) | YES | | NULL | | | sex | varchar(1) | YES | | NULL | | | fraction | int(3) | YES | | NULL | | +----------+------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> alter table class_one drop column fraction; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table class_one drop column deptno; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc class_one; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | name | varchar(6) | YES | | NULL | | | age | int(3) | YES | | NULL | | | sex | varchar(1) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
4)字段改名語法如下
ALTER TABLE tablename CHANGE [COLUMN] old_col_name column_definition [FIRST\AFTER col_name]
例如將age字段更改為age1,字段類型更改為int(2);
mysql> alter table class_one change column age age1 int(2); Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc class_one; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | name | varchar(6) | YES | | NULL | | | age1 | int(2) | YES | | NULL | | | sex | varchar(1) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
5)更改字段排序
語法關(guān)鍵字
FIRST|AFTER
FIRST 表示放在某字段前
AFTER 表示放在某字段后
示例:新增一個(gè)字段為age 并且將其放在name字段后
mysql> alter table class_one add column age int(4) after name; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc class_one -> ; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | name | varchar(6) | YES | | NULL | | | age | int(4) | YES | | NULL | | | sex | varchar(1) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
修改字段name將其放到sex字段后
mysql> desc class_one; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | name | varchar(6) | YES | | NULL | | | age | int(4) | YES | | NULL | | | sex | varchar(1) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> alter table class_one modify name varchar(6) after sex; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc class_one; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | age | int(4) | YES | | NULL | | | sex | varchar(1) | YES | | NULL | | | name | varchar(6) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 3 rows in set (0.01 sec)
6)更改表名語法
ALTER TABLE tablename RENAME [TO] new_tablename
例如將class_one 改成成class_1
mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | class_one | +----------------+ 1 row in set (0.00 sec) mysql> alter table class_one rename class_1; Query OK, 0 rows affected (0.00 sec) mysql> show tables ; +----------------+ | Tables_in_test | +----------------+ | class_1 | +----------------+ 1 row in set
文章名稱:Mysql基礎(chǔ)入門-SQL_DDL語句
轉(zhuǎn)載來于:http://www.rwnh.cn/article0/igicio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、搜索引擎優(yōu)化、網(wǎng)站維護(hù)、、網(wǎng)站策劃、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)