檢視Django資料庫中所有的表和刪除表操作

2021-10-06 19:19:50 字數 1375 閱讀 1452

django建立web程式後,中途你為某個模型新增了字段或者修改了字段,你希望把該錶刪除了,重新執行makemigrations、migrate。你可以直接進入dbshell介面,直接刪除表。

具體操作如下:

進入命令列介面,切換到manage.py所在的目錄:

python manage.py dbshell
注意,是dbshell,不是shell。

此時,命令列應是顯示如下字樣:

sqlite>

輸入如下命令:

select * from sqlite_master where type=

'table'

;

這條命令不需要更改任何字串,直接原樣輸入即可。

在sqlite>介面輸入任何命令語句,後面必須加上;分號,才會執行該語句。但是退出命令不用加分號,退出dbshell,輸入.quit。quit前有個點。後面也不加分號。

刪除**輸入如下命令:

drop table weekdays_plan_vocation;
從dbshell命令列介面執行drop table命令刪除**後,資料庫中的表確實刪除了。但是如果你此時就執行:

python manage.py makemigrations weekdays_plan
python manage.py migrate
是會報錯的。原因在於db.sqlite3資料庫中的django-migrations表中仍然儲存著刪除的表已經建立的資訊。所以,它不會重新建立表。

此時,執行:

顯示在撤銷相關資訊。然後再執行:

你可以檢視將要執行的遷移操作

python manage.py showmigrations weekdays_plan
接下來,你可以執行

python manage.py makemigrations train_plan

python manage.py migrate

就ok了。

查詢資料庫中所有的表

select from sysobjects where xtype u 查詢當前資料庫下所有使用者建立的表 xtype char 2 物件型別。可以是下列物件型別中的一種 c check 約束 d 預設值或 default 約束 f foreign key 約束 l 日誌 fn 標量函式 if 內...

檢視oracle資料庫中所有的表及特定使用者的表

select table name from user tables 當前使用者的表 select table name from all tables 所有使用者的表 select table name from dba tables 包括系統表 檢視特定使用者的表 此使用者登陸後,select ...

獲得資料庫中所有的表

在資料庫中一般都會有乙個系統表來記錄下所有的使用者表或檢視,儲存過程等等的.名字就叫sysobjects.所有要查詢出所有的表可以用以下語句 sql2000 select from sysobjects where xtype u或 v 或 p access select from msysobje...