mysql workbench,mysql sql使用_MySql簡單sql使用

 2023-11-18 阅读 20 评论 0

摘要:一、表的創建關鍵部分:表的名稱、列的名稱、列的數據類型。mysql workbench?基本語法:create table table_name (column_name column_type);實例1:-- 創建表mysql是啥?use testone;create table grocery_inventory(id int not null primary key auto_i

一、表的創建

關鍵部分:表的名稱、列的名稱、列的數據類型。

mysql workbench?基本語法:

create table table_name (column_name column_type);實例1:

-- 創建表

mysql是啥?use testone;

create table grocery_inventory(

id int not null primary key auto_increment,

mysql和sql、item_name varchar(5) not null,

item_desc text,

item_price float not null,

.sql文件、curr_qty int not null

);注:auto_increment 作為字段屬性,標示自增長

二、使用Insert命令

Insert的基本語法:

insert into table_name (column list) values(column values);

在括號中的值列表中,我們必須使用引號括起來字符串。Sql 標準是單引號,但MySql允許使用單引號或者雙引號。如果引號在字符串本身之中,別忘了所用的引號的類型進行轉義。

提示:整數不需要使用引號括起來。

下面是一個需要轉義的字符串的例子。

0‘Connor said "Boo"

如果我們把字符串放入到雙引號中,Insert 語句將會如下所示。

insert into table_name (column_name) values("0'Connor said \"Boo\"");如果我們把字符串放入到單引號中,Insert語句將會如下所示。

insert into table_name (column_name) values('0\'Connor said "Boo"');實例1:

-- 使用insert 命令

insert into testone.grocery_inventory

(

id,item_name,item_desc,item_price,curr_qty

)

values ('1','蘋果蘋果蘋','Beautiful,ripe apples.','0.25',1000);

insert into testone.grocery_inventory

values ('3','bunch','Seedless grapes.','2.99',500);

-- 插入所有列數據,為自增長列指定 null

insert into testone.grocery_inventory values (null,'bunc3','Seedless grapes.','4',400);

-- 不指定自增長列名

insert into testone.grocery_inventory

(

item_name,item_desc,item_price,curr_qty

)

values ('gray','lay lay la','3',700);

-- MySql auto_increment 列,必須要指定內容,才可以添加數據,如果是定對應的數字,添加的記錄中顯示對應的數字,如果需要自動增長則指定null

insert into testone.grocery_inventory values ('bunc4','Seedless grapes.','4',400);

三、使用 Select 命令

最基本的select 語法如下所示。

SELECT expressions_and_columns FROM table_name

[WHERE some_condition_is_true]

[ORDER BY some_column [ASC | DESC]]

[LIMIT offset,rows]

實例1:

-- 查詢數據

select * from testone.grocery_inventory;

-- 指定別名查詢

select *,100 as 張三 from testone.grocery_inventory;

1. 排序Select結果

Order By 默認的排序是升序(Asc),字符串排序是從A到Z,整數順序是從0開始,日期順序是從最早的日期到最近的日期。也可以指定一個降序,使用Desc,實例:

-- 排序select 結果

select * from testone.grocery_inventory order by item_name;

select * from testone.grocery_inventory order by item_price desc;

2. 限制結果

可以使用 Limit 子句來從Select查詢結果中返回一定數目的記錄。使用Limit的使用可以有兩個參數:偏移量和行數。偏移量是起始位置,而行數應該是自索命的。偏移量不指定默認為0.

-- 限制結果

select * from testone.grocery_inventory order by curr_qty desc limit 2;

select * from testone.grocery_inventory order by curr_qty desc limit 1,2;

四、在查詢中使用Where

select * from testone.grocery_inventory where curr_qty=500;

1.在Where子句中使用操作符

操作符:=、、!= 、<=、 >=

還有一個叫做Between的方便的操作符,它在比較整數或數據的時候很有用,因為它搜索位于一個最小值和最大值之間的結果(包括臨界值),示例如下:

select * from testone.grocery_inventory where curr_qty between 500 and 1000;

還有其他的操作符,邏輯操作符 And和Or

2. 使用Like 比較字符串

這個操作符在模式匹配中可以使用如下兩個字符作為通配符。

%----------匹配多個字符

_-----------匹配一個字符

select * from testone.grocery_inventory where item_name like 'b%';提示:除非在一個二進制字符串上執行一個like比較,否則這個比較總是不區分大小寫的。我們可以使用Binary 關鍵詞來強制執行一個區分大小寫的比較。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/177399.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息