MySQL 基本操作

2021-07-15 14:30:49 字數 3282 閱讀 7320

mysql 為關係型資料庫rdbms(relational database management system)這種關係可以理解為**的**,乙個關係型資料庫由一張或多張表組成。

1~連線mysql

然後輸入密碼

假如剛好裝好mysql,在os x系統下 root的密碼會在通知裡出現

1、連線本機

mysql -u root -p

#root前的空格可以省略

輸入密碼

2、連線遠端主機,假設遠端主機ip為110.110

.110

.110

mysql -h110

.110

.110

.110

-u 使用者名稱 -p 密碼

3、退出mysql

exit()

2~修改密碼

命令:mysql -u 使用者名稱 -p 舊密碼 password 新密碼
3.1~建立新的資料庫

命令:create database 資料庫名[其他選項];

資料庫選項:

character set charser_name

collate collation_name

假設建立乙個testdb的資料庫(mysql對大小寫不敏感,這點藥注意)

mysql>create database testdb character set utf8;

3.2~顯示資料庫

命令:show databases;注意追後加s

mysql>show databases;

3.3~刪除資料庫

命令:drop databsase 資料庫名
3.4~連線資料庫

命令: use 資料庫名
4.1~建立資料表

命令:create

table 表名[列表宣告]

以建立students表為例,表裡存放 id,name,***,age,address

mysql> create

table students(

>id char(10) not

null

primary

key,

>name char(10) not

null,

>*** char(6) not

null,

>age int

notnull,

>address char(36) not

null);

#not null 表示該列的值不能為空,必須填寫

#primary key 表示該列是表的主鍵,該列的值必須唯一

#char(6)表示值的最大長度為6

4.2~刪除資料表

命令 :drop

table 表名

4.3~插入資料

命令: insert

into 表名 values();

mysql> insert

into students values('1','summer','male','24','hangzhou')

4.4~查詢表中的資料

select 列名 from 表名 [查詢條件]

1.查詢表中全部資料

mysql> select * from students;

輸出的結果如下:

+----+--------+------+-----+----------+

| id | name | *** | age | address |

+----+--------+------+-----+----------+

| 1 | summer | male | 24 | hangzhou |

+----+--------+------+-----+----------+

1 row in set (0.00 sec)

2.查詢表中的特定資料

列如查詢id和name這兩列

mysql> select id,name from students;

列如查詢name為summer的這一列

mysql> select * from students where name = 'summer';

4.5~ 刪除表中的資料

命令:delete

from 表名 where 表示式

mysql> delte from students where id='1';

4.6~修改表中資料

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

列如把 id 為1 的 name 改為spring

mysql> update students set name = 'spring'

where id = '1'

把所有的age 都加1

mysql> update students set age += 1

4.7~修改表中的列

1,改

命令:alter

table 表名 change 列名 新資料型別[其他]

例如把address改為adr

mysql> alter

table students change address addre char(36) not

null

2,加命令:alter

table

add 列名 新資料型別[其他]

例如新增新的列,列名為 test

mysql> alter

table

add test int(4) default

'0'#de****t'0' 表示預設值為0

3,刪命令:alter

table 表名 drop 列名

例如刪除 test 這一列

mysql> alter

table students drop test;

4.8修改表名

mysql> alter table students rename new_students;

或者mysql> rename students to new_students;

mysql基本操作 MySQL基本操作

mysql中新增使用者,新建資料庫,使用者授權,刪除使用者,修改密碼 注意每行後邊都跟個 表示乙個命令語句結束 1.新建使用者 1.1 登入mysql mysql u root p 密碼 1.2 建立使用者 mysql insert into mysql.user host,user,passwor...

mysql 基本操作 mysql基本操作

mysql 建立表,並設定主鍵自增 create table log logid int 4 primary key not null auto increment,logtitle varchar 32 not null logcontent varchar 160 not null logtim...

mysql基本操作

1,檢視資料庫狀態 及啟動停止 etc init.d mysqld status etc init.d mysqld start etc init.d mysqld stop 2,給使用者配置初始密碼123456 mysqladmin u root password 123456 3,修改root使...