資料庫基本操作

2021-07-28 07:56:11 字數 2434 閱讀 5058

登入資料庫系統:mysql -h localhost -u root -p

檢視已存在的資料庫: show  databases

檢視預設儲存引擎:show variables like 'storage_engine'

建立資料庫:create database  想建立的資料庫名字

刪除資料庫: drop databases 資料庫名字

檢視資料庫系統支援的儲存引擎的型別:show engines

建立修改表:create table 表名(

屬性名 資料型別[完整約束條件],

......

屬性名  資料型別

);

在使用create table語句建立表時,首先用use 語句選擇資料庫,否則會出現1046錯誤;

選擇資料庫語句的基本格式為「use 資料庫名」

設定表的主鍵

1.單字段主鍵    屬性名 資料型別  primary key

2.多欄位主鍵    primary key(屬性名1,屬性名2,... ,屬性名)

設定表的外來鍵

constraint 外來鍵別名  foreign key(屬性名1.1 ,屬性名1.2  ,......  ,屬性名1.n)

references 表名 (屬性名1.1 ,屬性名1.2  ,......  ,屬性名1.n

1.設定表的非空約束

屬性名 資料型別  not null

2.設定表的唯一約束性

屬性名  資料型別  unique

3.設定表的屬性值自動增加

屬性名  資料型別  auto_incerment

4.設定表的屬性的預設值

屬性名  資料型別  default  預設值

檢視表結構

1.檢視表的基本結構:describe 表名;或者縮寫成 desc 表名

2.檢視表的詳細結構:show create table 表名;

修改表名

語法格式:alter table 舊表名 rename [to]  新錶名;

修改欄位的資料型別

基本語法:alter table 表名 modify 屬性名 資料型別;

修改欄位名

基本語法:alter table 表名 change 舊屬性名 新屬性名 新資料型別;

增加字段

基本語法:alter table 表名add 屬性名1 資料型別 [完整約束條件] [first | after 屬性名2]

刪除字段

基本語法: alter table 表名 drop 屬性名;

修改欄位的排列位置

基本語法: alter table 表名 modify 屬性名1 資料型別 first|after 屬性名2;

更改表的儲存引擎

基本語法: alter table 表名 engine=儲存引擎名;

刪除表的外來鍵約束

基本語法:alter table 表名 dropforeign key 外來鍵別名;

刪除表

基本語法:drop table 表名;

資料庫 資料庫基本操作

操作練習 修改表結構 表資料的操作 實現 1 建立表 create table student stu no char 12 not null primary key,stu name varchar 20 not null gender tinyint 1 default1,age tinyint...

資料庫基本操作

1.查詢一周之內的資料 select from 表名 where date sub curdate interval 7 day date 欄位名 2.插入 年 月 日 時 分 秒的時間 pstmt.settimestamp 7,new timestamp system.currenttimemil...

資料庫基本操作

mysql常用的操作命令 mysql h ip u root data base show databases 檢視資料庫中的所有庫 use data base 選擇data base這個資料庫 show tables 檢視data base這個庫中的表 desc data table 開啟表中的d...