MySQL常用語句

2021-10-03 13:31:34 字數 1923 閱讀 9087

建立,刪除和最基本查詢:

顯示資料庫 mysql->show databases;

建立資料庫 mysql->create database db;

刪除資料庫 mysql->drop database db;

選擇資料庫 mysql->use db

建立表 mysql->create table mytable(name archar(20),***(char(1),birth date); 刪除表 mysql->drop table mytable;

顯示表的內容 mysql->show tables;

顯示表的結構 mysql->describe mytable

顯示所有的資料庫

mysql> show databases;(注意:最後有個 s)

建立資料庫

mysql> create database test;

連線資料庫

mysql> use test;

當前資料庫包含的表資訊

mysql> show tables; (注意:最後有個 s)

刪除資料庫

mysql> drop database test;

表操作備註:操作之前使用「use 《資料庫名》」應連線某個資料庫。

建表命令:create table 《表名》 (《欄位名 1> 《型別 1> [,…《欄位名 n> 《型別 n>]);

例子:mysql> create table myclass

命令: desc 表名,或者show columns from 表名

例子:mysql> describe myclass

mysql> desc myclass;

mysql> show columns from myclass;

刪除表命令:drop table 《表名》

例如:刪除表名為 myclass 的表

mysql> drop table myclass;

插入資料

命令:insert into 《表名》 [( 《欄位名 1>[,…《欄位名 n > ])] values ( 值 1 )[, ( 值 n )]

例子:mysql> insert into myclass values(1,『tom』,96.45),(2,『joan』,82.99), (2,『wang』, 96.59);

查詢表中的資料

查詢所有行

mysql> select * from myclass

詢前幾行資料

例如:檢視表 myclass 中前 2 行資料

mysql> select * from myclass order by id limit 0,2;

或者mysql> select * from myclass limit 0,2;

刪除表中資料

命令:delete from 表名 where 表示式

例如:刪除表 myclass 中編號為 1 的記錄

mysql> delete from myclass where id=1;

修改表中資料

命令:update 表名 set 字段=新值,… where 條件

mysql> update myclass set name=『mary』 where id=1;

在表中增加字段

命令:alter table 表名 add 字段 型別 其他;

例如:在表 myclass 中新增了乙個字段 passtest,型別為 int(4),預設值為 0

mysql> alter table myclass add passtest int(4) default 『0』

更改表名

命令:rename table 原表名 to 新錶名;

例如:在表 myclass 名字更改為 youclass

mysql> rename table myclass to youclass

mysql常用語句 MySQL常用語句

create table student id int primary key auto increment comment 學號 name varchar 200 comment 姓名 age int comment 年齡 comment 學生資訊 修改表注釋 alter table studen...

php mysql 常用語句 mysql常用語句

一 修改mysql使用者密碼 mysql h localhost u root p 命令列登入 update user set password password 123456 where user root 二 資料庫操作 show databases 顯示資料庫 create database ...

MySQL常用語句

and和or可以混用,and比or具有更高的優先順序,但盡量使用圓括號區分 自動過濾重複的資料owner,關鍵字distinct select distinct owner from pet 按照生日公升序排列,關鍵字order by select name,birth from pet order...