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

歡迎訪問 生活随笔!

生活随笔

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

数据库

mysql数据库一些常用操作

發布時間:2023/12/8 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql数据库一些常用操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

mysql命令行語句一定要加;結束,不然就算你enter以后還是需要輸入命令的。

ALTER TABLE 原表名 RENAME TO 新表名 ? //修改表名


mysql -u root -p //登陸
create database xhkdb;//創建數據庫
drop database xhkdb;//刪除數據庫
use database(庫名) ; //用某個庫//連接數據庫
show tables;//查看這個庫里的表
create table t_table2(
? ? uid int(10) not null auto_increment,
? ? name varchar(100) not null,
? ? time varchar(100) not null,
? ? primary key(uid)
? ? ?);//創建一個表,表里面可以沒有任何數據;但是你只要寫了數據,就必須定義key值(primary key(uid));
CREATE TABLE tbl_emp(
emp_id int(11) not NULL AUTO_INCREMENT,
emp_name VARCHAR(255) not null,
gender char(1) not null,
email VARCHAR(255) not NULL,
PRIMARY KEY(emp_id)
);

select:
select DISTINCT name ?from t_customer; //查詢到的字段中不會有重復的信息
select DISTINCT name ?from t_customer limit 5; //查詢這個字段的前五個數據
select ?name from t_customer limit 5, 5;//從第五行開始查詢后五個字段;
select t_customer.id from test.t_customer;//完全限定的方式
select telephone from t_customer order by telephone;//通過telephone字段來按字母順序排序
order by:
select telephone,name from t_customer order by telephone,name;//按其中的兩個列分別按字母順序排序
select telephone from t_customer order by telephone desc;//通過telephone字段來按字母逆序順序排序
select telephone,id ?from t_customer order by id desc,telephone;//先通過id字段來按字母逆序順序排序,然后通過telephone順序排序
select telephone from t_customer order by telephone desc limit 1;//查詢最大值
select telephone from t_customer order by telephone limit 1;//查詢最小值
/****order by 子句一定要位于From字段以后 limit子句一定要位于order by子句以后****/
/****子句順序不對的話會報錯****/
where:
//過濾數據(where)
select name from t_customer where id = 1;//查詢id值為1的name字段
select id, name from t_customer where name='陳曉強';//查詢id值,name值根據name字段的值
= 等于 < 小于 <= 小于等于 > 大于 >= 大于等于 ?!=或者<> 不等于 between 在指定的兩個值之間
select id, name from t_customer where ?id between 5 and 20;//查詢id值為5到20之間的字段
select id from t_customer where gender is null;//判斷空值
//數據過濾(in / not in /between / not between /)
select * from t_customer where id<5 and gender='男';//查詢id小于5和gender為男的數據;
select * from t_customer where id<5 or gender='女';// 查詢id小于5和gender為女的數據;
select * from t_customer where id in (1,5,20) order by id;// in子句
select * from t_customer where id not in (1,15,29,5) order by id;//not in 子句
select * from t_customer where id not between 5 and 20 order by id;//not between子句
//用通配符進行過濾(like)(%匹配零個、一個或者多個字符)( _ 匹配一個字符)
select * from t_customer where telephone like '%123%';
select * from t_customer where telephone like '%123';
select * from t_customer where telephone like '123%';
select * from t_customer where telephone like '17764226233';
select * from t_customer where telephone like '%2%4%';
select * from t_customer where telephone like '1776422623_';
Notes:通配符不要過度使用。
//用正則表達式進行搜索
select * from t_customer where address regexp '湖北省';//查找
select * from t_customer where telephone regexp '[123]33';//匹配133、233或者333字符
select * from t_customer where telephone regexp '[1-3]3';//匹配13、23或者33字符 [1-3]相當于匹配單個字符1到3
?. 可以匹配任何字符 但是不能匹配NULL
\\-可以匹配特殊字符- ? \\.匹配特殊字符. ?\\/匹配特殊字符/

//創建計算字段
select concat(name,'(',id,')') from t_customer;//輸出為t_customer表中的name(id) //拼接字段
select concat(ltrim(address)) from t_customer where id =32;//刪除字段左邊的所有空格
select concat(rtrim(address)) from t_customer where id =32;//刪除字段右邊的所有空格
select concat(name,'(',rtrim(ltrim(address)),')') as customer from t_customer where id =32;//跟拼接的字段起一個別名
select id,name, concat(name,'(',rtrim(ltrim(address)),')') as customer from t_customer where id =32;//可以同時跟其他字段同時查詢
//帶時間的字段
select 字段名 from where Data(帶時間的字段名) between '2015-09-01' and '2015-10-01';
//聚集函數(確定表中行數 獲取表中行組的和 找出表列、行、特定列、特定行)的最大值,最小值和平均值。
count() 返回某列的行數
select avg(distinct price) as price from t_customer where id = 1003;
//順序
select
from
where
group by 分組過濾
having 組級過濾?
order by /oder by desc
limit

select * from t_customer where id in (select id from t_customer where name='陳小強' );//子句查詢

//聯結查詢
外鍵:外鍵為某一個表的一列,它包含另一個表的主鍵值,定義了兩個表之間的關系。

select cust_name,cust_contact from customers where cust_id in (
select cust_id from orders where order_num in (
select order_num from orderitems where prod_id = 'TNT2'));
等價于
select cust_name,cust_contact from customers ,orders,orderitems where?
customers.cust_id = orders.cust_id?
and orderitems.order_num = orders.order_num?
and prod_id='TNT2';
//
select cust_name,cust_contact from customers as c ,orders as o,orderitems as oi where?
c.cust_id = o.cust_id?
and oi.order_num = o.order_num?
and prod_id='TNT2';
/*********************************************select************************************************/
insert into t_customer values(33,'59','男','24332334654856436','河北省');//按照表的順序插入
insert into t_customer(id,gender,name,address,telephone) values(34,'女','小平','湖北省武漢市','7578363567345738');//按照需要插入的順序插入,要在前面寫對應的字段
insert into t_customer values(35,'59','男','24332334654856436','河北省'),(36,'59','男','523523525325','河北省');//一次性插入多行數據
//插入從表中查出的數據
insert into customers(cust_id,cust_contact,cust_email,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country) select cust_id,cust_contact,cust_email,cust_name,cust_address,cust_city,cust_state,cust_zip,cust_country from custnew;
/***************************************delect/update********************************************/
update t_customer set telephone='4536363464364262' where id = 22;//更改一個字段
update t_customer set telephone='435353464362326847',gender='女' where id = 23;//更改多個字段

delete from t_customer where id = 23;//刪除id為23的行
如果想從表中刪除所有行;不要使用delete。可以使用truncate table語句,它可以完成相同的工作 ,但速度更快
/******************************創建表***********************************/
create table student(
stuNo int not null auto_increment primary key, //不允許為空、主鍵、自增長
stuName varchar(50) not null, //不允許為空
stuAge int null ? ?//允許為空
);

drop table t_table;//刪除一個表
rename table t_student to MyTable;//修改表的名字
rename table MyTable to table1,student to table2;//修改多個表的名字

ALTER TABLE t_customer CHANGE COLUMN name ?cust_name VARCHAR(50);;//修改表中的字段的名稱


alter table t_student add stuAddress varchar(30);//在表中插入一個新的字段
alter table t_student drop stuAddress;//在表中刪除一個字段
//alter table 經常使用來給其他表寫外鍵


/*****************************添加外鍵************************************/
alter table userinfo add CONSTRAINT fk_user_depart FOREIGN KEY(department_id) REFERENCES department(id)


create table userinfo(
uid INT auto_increment PRIMARY KEY,
name varchar(32),
department_id INT,
CONSTRAINT fk_user_depart FOREIGN KEY ("department_id") REFERENCES department("id")
)ENGINE=INNODB DEFAULT CHARSET=utf8;

create table department(
id int auto_increment PRIMARY KEY,
title varchar(32)
)ENGINE=INNODB DEFAULT CHARSET=utf8;

/***************************************分組查詢***************************************/
select count(uid),department_id from userinfo group by department_id;
select count(uid) as 部門總人數,department_id from userinfo where uid>0 group by department_id having count(uid)>1;

/***************************************連表查詢**************************************/
select * from userinfo,department where userinfo.department_id = department.id;?

select * from userinfo LEFT JOIN department on userinfo.department_id = department.id; // 左邊的表全顯示

select * from userinfo?
LEFT JOIN department on userinfo.department_id = department.id;
LEFT JOIN part on xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;//可以連接多張表
select * from userinfo RIGHT JOIN department on userinfo.department_id = department.id; // 右邊的表全顯示


索引(加速查找)
創建索引的過程中會創建額外的文件(某種存儲格式)會占用硬盤的位置

hash索引:
?? ?單值速度快
?? ?范圍查找較慢
btree索引:
?? ?二叉樹

建立索引:
?? ?-a 額外的文件保存特殊的數據結構
?? ?-b 查詢快:插入更新刪除慢
?? ?-c 查詢語句需要命中索引

普通索引創建方法:
/*******************表中創建索引*************************/
create index 索引名稱 on 表名(字段名);?
create index ind_name on account(name); //對account表中的name字段創建索引
/*******************表中刪除索引*************************/
drop index 索引名稱 on 表名;
drop index ind_name on account;//刪除account表中的ind_name索引

create table t_table2(
? ? uid int(10) not null auto_increment primary key,
? ? name varchar(100) not null,
? ? time varchar(100) not null,
? ? index ix_name(name) ?//創建普通索引
? ? ?);

唯一索引創建方法:
create table t_table2(
? ? uid int(10) not null auto_increment primary key,
? ? name varchar(100) not null,
? ? time varchar(100) not null,
? ? unique ix_name(name) ?//創建普通索引
? ? ?);
create unique index 索引名 on 表明(字段名);
drop unique index 索引名 on 表名;

總結

以上是生活随笔為你收集整理的mysql数据库一些常用操作的全部內容,希望文章能夠幫你解決所遇到的問題。

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