mysql資料庫建表的完整步驟「常用的mysql命令大全」

SQL資料庫語言

  • 1.數據定義語言(CREATE,ALTER,DROP,DECLARE)
  • 2.數據操縱語言(SELECT,DELETE,UPDATE,INSERT)
  • 3.數據控制語言(GRANT,REVOKE,COMMIT,ROLLBACK)

創建資料庫

create database my-database
1

刪除資料庫

drop database my-database
1

創建新表

create table my_table(col1 type1 [not null] [primary key],col2,type2 [not null] [primary key],...)
1

根據已有的表創建新表

create table new_table like old_table
1

刪除表

drop table my_table
1

增加一個列

列增加後不能刪除。DB2中列加上後數據類型也不改變,唯一能改變的是增 加 varchar 類型的長度。

Alter table my_table add column col type
1

添加主鍵

Alter table my_table add primary key(col)
1

刪除主鍵

Alter table my_table drop primary key(col)
1

創建索引

create [unique] index inx on my_table(col...)
1

刪除索引

drop index inx
1

創建視圖

create view my_view as select statement
1

刪除視圖

drop view my_view
1

常用的 sql 基本語句

選擇:select * from my_table where 條件
插入:insert into my_table (field1,field2) values (value1,value2)
刪除:delete from my_table where 條件
更改:update my_table set field1 = value1 where 條件
查找:select * from my_table where field1 like 『%value1』
排序:select * from my_table order by field1,field2 [desc]
總數:select count * as totalcount form my_table
求和:select sum(field1) as sumvalue from my_table
平均:select avg(field1) as avgvalue from my_table
最大:select max(field1) as maxvalue from my_table
最小:select min(field1) as minvalue from my_table

PHP 操作 mysql 資料庫

1.鏈接資料庫

$conn=@mysql_connect("localhost","root","root");
if(!$conn){
	die("資料庫連接失敗",mysql_error())
}else{
	echo"數據路連接成功"
}
123456

2.選擇資料庫

mysql_select_db("web",$conn)
1

3.執行 sql 語句

$query=mysql_query("select * from 表名 where 1")
1

4.操作 sql 結果

while($row=mysql_fetch_assoc($result)){
	print_r($row);
	echo "<br>";
}
1234

5.關閉 mysql 鏈接

mysql_close($conn)

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/273802.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-17 14:08
下一篇 2024-12-17 14:08

相關推薦

發表回復

登錄後才能評論