日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

1天熟记----数据库

發布時間:2024/3/12 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1天熟记----数据库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

數據庫

查看創建的表:

show create table emp ;

一、基本用法(增刪改查)

1.增

創建數據庫

create database name;

創建表

create table person(id int primary key, # 主鍵name varchar(16) not null, # 不為空age int,phone varchar(11),address varchar(256) );

插入列

alter table person add 列名 varchar(8)null;

插入數據

inner into person(id,name,phone) values (2015,'wy','12345678')

2.刪

刪除數據庫

drop database <name>;

刪除表

drop table person;

刪除表列

alter table person drop column age;

刪除表行

delete from person where id = 2015;

3.改

更新表名

alter table person rename user;

修改表字段
①modify微調 修改數據類型,修改字段位置
②change大改 修改名字

alter table person modify age char(10);

把name改為MYname

alter table person change name MYname varchar(20);

修改表內容

update person set address='浙江杭州'; # (全部改為浙江杭州) update person set address='浙江杭州' where id = 1; #(id為1的改為杭州)

4.查

見高級用法
例如:列出name姓趙或姓王的男性,且age大于20的人,以年齡逆序排序。

select *from A where name regexp '^ [王趙]' and sex = 'Mr' and age > 20 order by age desc;

二、高級用法

1.select distinct 返回不同的值

選出A表中b列唯一不同的值,即去掉重復的值
select distinct b from A;

2.or和and

從A表中選出age大于10且country為’CN’或’UK’的人
select *from A where (age>10) and (country = 'CN' or country = 'UK');

3.order by 用于對結果按照一個列或多個列進行排序,默認升序(升序ASC,降序DESC)

從A表中按照 age和country兩列降序排列顯示
select *from A order by age,country DESC;

4.group by 用于將查詢返回的結果集進行一個分組,并展示各個分組中排在第一個的記錄,將分組中其余成員隱藏。

表A
±—±------±-----±-----------±---------+
| id | name | age | phone | address |
±—±------±-----±-----------±---------+
| 1 | yang | 22 | 231232132 | 中國上海 |
| 2 | cao | 30 | 456789 | 浙江杭州 |
| 3 | li | 23 | 34567894 | 江蘇南京 |
| 4 | huang | 33 | 34567894 | 湖北武漢 |
| 5 | zhang | 30 | 4567890 | 中國北京 |
| 6 | yang | 24 | 2343435353 | 山東青島 |
| 7 | cao | 44 | 12312312 | 河南鄭州 |
| 8 | huang | 45 | 5677675 | 安徽合肥 |
| 9 | yang | 80 | 3343738 | 江蘇南通 |
±—±------±-----±-----------±---------+

顯示名字,去掉重復值
select *from A group by name;

±—±------±-----±----------±---------+
| id | name | age | phone | address |
±—±------±-----±----------±---------+
| 2 | cao | 30 | 456789 | 浙江杭州 |
| 4 | huang | 33 | 34567894 | 湖北武漢 |
| 3 | li | 23 | 34567894 | 江蘇南京 |
| 1 | yang | 22 | 231232132 | 中國上海 |
| 5 | zhang | 30 | 4567890 | 中國北京 |
±—±------±-----±----------±---------+

5.limit 限制顯示的行數

只顯示兩行

select *from A limit 2;

6.like 操作符用于在 where子句中搜索列中的指定模式

通配符:

① % 替代 0 個或多個字符

② _ 替代一個字符

選出A表中name列以W開頭的人
select *from A where name like 'W%';
選出A表中name包含wy的客戶
select * from A where name like '%wy%';
選出A表中name以一個任意字符開始,然后是‘xxoo’的所有客戶
select *from A where name like '_xxoo';

7.in 操作符允許您在 where 子句中規定多個值

between 操作符用于選取介于兩個值之間的數據范圍內的值。

選出表A中age介于1和20之間但name不為wy和ww的人
select *from A where (age between 1 and 20and name not in'wy','ww'select *from A where (age >= '1' and age <= '20') and (name!='wy' or name != 'ww')
選出A表中name為 WY和WW的網站
select *from where name in ('WY','WW');

select *from where name='WY' or name='WW;

8.別名

A表
±—±------±-----±----------±---------+
| id | name | age | phone | address |
±—±------±-----±----------±---------+
| 2 | cao | 30 | 456789 | 浙江杭州 |
| 4 | huang | 33 | 34567894 | 湖北武漢 |
| 3 | li | 23 | 34567894 | 江蘇南京 |
| 1 | yang | 22 | 231232132 | 中國上海 |
| 5 | zhang | 30 | 4567890 | 中國北京 |
±—±------±-----±----------±---------+

把表A中phone設置為tel別名
select phone as tel from A;

±---------+
| tel |
±---------+
| 456789 |
| 34567894 |
| 34567894 |
| 23123213 |
| 4567890 |
±---------+

把A表中age,phone,address合并,并設置info的別名
select id,name, concat(age,',',phone,',',address) as info from A

±—±------±---------------------------------+
| id | name | info |
±—±------±-----------------------------------+
| 2 | cao | 30 , 456789 , 浙江杭州 |
| 4 | huang | 33 ,34567894 , 湖北武漢 |
| 3 | li | 23 ,34567894 , 江蘇南京 |
| 1 | yang | 22 ,231232132 ,中國上海 |
| 5 | zhang | 30 , 4567890 ,中國北京 |
±-±-----±--------------------------------------+

9.連接 join

inner join 可簡寫 join,inner join 關鍵字在表中存在至少一個匹配時返回行

left join,返回的結果右邊沒有為空

right join,返回的結果左邊沒有為空

full join:只要其中一個表中存在匹配,則返回行

A表
±—±-------------±--------------------------±------±--------+
| id | name | url | alexa | country |
±—±-------------±--------------------------±------±--------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘寶 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鳥教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
±—±--------------±--------------------------±------±--------+
B表
±----±--------±------±-----------+
| aid | site_id | count | date |
±----±--------±------±-----------+
| 1 | 1 | 45 | 2016-05-10 |
| 2 | 3 | 100 | 2016-05-13 |
| 3 | 1 | 230 | 2016-05-14 |
| 4 | 2 | 10 | 2016-05-14 |
| 5 | 5 | 205 | 2016-05-14 |
| 6 | 4 | 13 | 2016-05-15 |
| 7 | 3 | 220 | 2016-05-15 |
| 8 | 5 | 545 | 2016-05-16 |
| 9 | 3 | 201 | 2016-05-17 |
±----±--------±------±-----------+

以連接A、B兩個表,顯示訪問記錄
select A.name,A.url,B.count,B.date from A inner join B on A.id=B.site_id order by B.date select A.name,a.url,b.count,b.date from A left join B on A.id=B.site_id order by B.count DESC

10.正則(舉幾個簡單的例子)

選出A表中name以“F”“G”“p”開始的網站 (正則)

select *from A where name regexp '^ [FGp]';

### 11.區分大小寫
讀取 A 表中 b字段值為 c 的記錄 (不區分大小寫)
```sql
select *from A
where b = ‘c’;

讀取 A 表中 b字段值為 c 的記錄 (區分大小寫)

select *from A
where binary b = ‘c’

11.UNION 操作符合并兩個或多個 SELECT 語句的結果

A
±—±-------------±--------------------------±------±--------+
| id | name | url | alexa | country |
±—±-------------±--------------------------±------±--------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘寶 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鳥教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
±—±--------------±--------------------------±------±--------+
B
mysql> SELECT * FROM B; (顯示B表)
±—±-----------±------------------------±--------+
| id | app_name | url | country |
±—±-----------±------------------------±--------+
| 1 | QQ APP | http://im.qq.com/ | CN |
| 2 | 微博 APP | http://weibo.com/ | CN |
| 3 | 淘寶 APP | https://www.taobao.com/ | CN |
±—±-----------±------------------------±--------+
從A和 B 表中選取所有不同的country(只有不同的值)

select name,country from A union select app_name,country from B order by country

使用 UNION ALL 從A和B表中選取所有的country(也有重復的值)

select name,country from A union all select app_name,country from B order by country

12.select into從一個表復制數據,然后把數據插入到另一個新表中

創建A表的備份復制文件

select * into Abackup from A;

復制name,id到新表中

select name,id into Abackup from A;

復制study大于60的到新表

select * into Abackup from A where study > 60;

復制多個表中的數據插入到新表中

select A.name,B.study into Abackup from A join B on A.id=B.id;

13.約束

1.NOT NULL - 指示某列不能存儲 NULL 值。
2.UNIQUE - 保證某列的每行必須有唯一的值。
3.PRIMARY KEY - NOT NULL 和 UNIQUE 的結合。確保某列(或兩個列多個列的結合)有唯一標識,有助于更容易更快速地找到表中的一個特定的記錄。
4.FOREIGN KEY - 保證一個表中的數據匹配另一個表中的值的參照完整性。
5.CHECK - 保證列中的值符合指定的條件。
6.DEFAULT - 規定沒有給列賦值時的默認值。

1.創建ID,name不為空的表

create table (ID int not null,name varchar(13) not null);

在一個已創建的表的 “age” 字段中添加 NOT NULL 約束

alter table person modify age int not null;

2.在A表創建時在ID 列上創建 UNIQUE 約束

create table A( ID int not null unique)

當表已被創建時,如需在 “ID” 列創建 UNIQUE 約束

alter table A add unique (ID)

撤銷 UNIQUE 約束

alter table A drop index ID

3.在 A表創建時在ID列上創建 PRIMARY KEY 約束

create table A(ID int not null,primary key (ID));

當表已被創建時,如需在 “ID” 列創建 PRIMARY KEY約束

alter table A add primary key (ID);

撤銷 PRIMARY KEY約束

alter table A drop primary key;

4.在 A表創建時在ID列上創建foreign KEY 約束

create table A(ID int not null,foreign key (ID));

當表已被創建時,如需在 “ID” 列創建 foreign key約束

alter table A add foreign key (ID);

撤銷 foreign key約束

alter table A drop foreign key ID;

4.在 A 表創建時在 ID 列上創建 CHECK 約束

create table A(ID int not null check (ID>0));

當表已被創建時,如需在ID列創建 CHECK 約束

alter table A add check (ID>0)

撤銷 CHECK 約束

alter table A drop check (ID)

5.在A表創建時在 “city” 列上創建 DEFAULT 約束

create table A(city varchar(255) default 'CN');

在 “city” 列創建 DEFAULT 約束

alter table A alter city set default 'CN';

撤銷 DEFAULT 約束

alter table A alter city drop default;

三、函數

AVG() - 返回平均值

COUNT() - 返回行數

FIRST() - 返回第一個記錄的值

LAST() - 返回最后一個記錄的值

MAX() - 返回最大值

MIN() - 返回最小值

SUM() - 返回總和

select avg(column_name)from A

四、進階

查某一列(或多列)的重復值。(只可以查出重復記錄的值,不能查出整個記錄的信息

select a,b from A group by a,b having(count(*))>1

某一列有重復值的記錄。(此方法查出的是所有重復的記錄,如果有兩條記錄重復的,就查出兩條)

例如:查找a重復的記錄:

select * from A where a in ( select a from A group by a having(count(*))>1)

總結

以上是生活随笔為你收集整理的1天熟记----数据库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。