天天看點

MySql第二天

2022-09-04

MySQL常用的指令:

1、進入MySQL的指令:

mysql -uroot -p;      

說明:-uroot是指以root方式進行登陸MySQL。之後輸入設定的SQL密碼。

2、查詢目前的時間

select now();      

3、退出的指令三種操作

(1)方式一,輸入指令

exit;      

 (2)按“Ctrl+D”鍵,退出

(3)也可以輸出下面指令

quit();      

---------------------------------------------------------------------------------------------------------------

資料庫操作的指令

4、顯示目前所有的資料庫

show databases;      

5、建立資料庫

create database xxx(資料庫名稱) charset=utf8;      

注:如果在Linux作業系統中,建立的資料庫預設是存放在“/var/lib/mysql”中。

6、使用某個資料庫

use xxx(某個資料庫名稱);      

7、檢視目前使用的資料庫

select database();      

8、删除某個資料庫

drop database xxx(某個資料庫名稱);      

---------------------------------------------------------------------------------------------------------------

表操作的指令

9、顯示表

show tables;      

10、建立表

在建立表之前,選中要在哪個資料庫插入資料,使用指令“use xxx(某個資料庫名稱)”,再執行建立表的操作。

例如:建立一個學生表

create table students(id int unsigned primary key auto_increment not null,name varchar(10) not null,age tinyint default 0,gender enum("男","女") default "男");      

11、檢視表的結構

desc xxx(某個表的名稱);      

12、在表中添加某個字段

例如:在表“students”中添加字段birthday

alter table students add birthday datetime not null;      

說明: alter table 表名 add 字段名(屬性) 類型 限制;

13、修改字段類型

例如:将字段中的“birthday”的類型“datetime”改為“date”

alter table students modify birthday date;      

說明:alter table 表名 modify 字段名  新改的類型名

14、修改表中的字段名和字段類型

例如:将字段中的名稱“birthday”改為“birth”;而且将“birthday”字段的類型“datetime”改為“date”。

alter table students change birthday birth datetime not null;      

說明:alter table 表名 change 字段名 新的字段名 字段的類型 限制

15、修改表——删除某個字段名

例如:将上述“students”表中的字段名“birth”删除

alter table students drop birth;      

說明:alter table 表名 drop 字段名;