create(创建) table(表格) student<表名>

create(创建) table(表格) student<表名> ( <列名><数据类型及长度>[not null][auto_increment][primary key], <列名><数据类型及长度>,[references<主表名>(<主表主键列>)], <列名><数据类型及长度>, ) drop table<表名> DDL、DML、DCL 文件导入导出Excel 2007 转存SQL。无法把主外键关系导出来! 备份:创建备份,恢复备份!可以将主外键关系都备份出来! 数据库:文件(记事本) 数据库(网状数据库,层次数据库,关系数据库) 网状是指网络连接数据库(html) 层次是指文件,电脑文件存储 关系是指现在学习数据库! 导入导出数据 把表变成sql代码 备份与还原

数据库操作:增,删,改,查 CRUD     nisert插入into nation(code,name)info(code,name,sex,nation,birthday) values('n005,''蒙古族') 添加: nisert into <表名>(列1,列2…)values(<'值1'>,['值2'])<值>必须写[值]可写可不写 注意:1.列与值更匹配(数量,类型,次序) nisert nito <info,nation>values(''.'','','','') 2.列可以省略,但是必须与表中的总列数和列的次序完全对应。 3.自增长列,不能省掉自增列,给自增列赋个’’ delete from删除(表格)<car>where code='code'

delete from删除(表格)<car>where brand='b006'

delete from删除(表格)<car>where brand='b003'&&brand='b006' &&于

delete from删除(表格)<car>where brand='b003'||brand='b006' ||或

delete from删除(表格)<car>where brand='b003'or brand='b006' delete from删除(表格)<car>where brand='b007'&&price>50 <>大于,小于 delete from删除(表格)<car>where code !='c010'&&code<>'c018'         != delete from删除(表格)<car>where brand='b007'and price>50<> !=

转载于:https://www.cnblogs.com/naqiang/p/5527151.html

from graphviz import Digraph import pymysql # 1. 连接数据库(需替换实际配置) conn = pymysql.connect( host='localhost', user='root', password='your_password', database='your_database', charset='utf8mb4' # 确保字符集支持中文注释 ) cursor = conn.cursor() # 2. 初始化Graphviz dot = Digraph(comment='课堂行为系统 ER 图', format='png') dot.attr(rankdir='LR', nodesep=2, ranksep=1.5) # 优化布局参数 # 3. 获取所有及其列信息(含主键和外键标记) def get_table_info(): cursor.execute("SHOW FULL TABLES") tables = [table[0] for table in cursor.fetchall()] table_info = {} for table in tables: # 获取主键 cursor.execute(f"SHOW KEYS FROM `{table}` WHERE Key_name = 'PRIMARY'") primary_keys = [row[2] for row in cursor.fetchall()] # 获取外键信息 cursor.execute(f""" SELECT COLUMN_NAME as fk_column, REFERENCED_TABLE_NAME as ref_table, REFERENCED_COLUMN_NAME as ref_column FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = '{table}' AND REFERENCED_TABLE_NAME IS NOT NULL """) foreign_keys = cursor.fetchall() # 获取普通列 cursor.execute(f"SHOW COLUMNS FROM `{table}`") columns = [] for col in cursor.fetchall(): col_name = col[0] col_type = col[1] comment = col[4] # 构建列信息 column_label = f"{col_name} : {col_type}" if comment: column_label += f" ({comment})" # 标记主键 if col_name in primary_keys: column_label = f"<b>{col_name}</b> (PK) : {col_type}" if comment: column_label += f" ({comment})" # 添加到列信息 columns.append((col_name, column_label)) table_info[table] = { 'primary': primary_keys, 'foreign_keys': foreign_keys, 'columns': columns } return table_info # 4. 生成实体节点 table_info = get_table_info() for table, info in table_info.items(): # 按主键优先排序 sorted_columns = sorted( info['columns'], key=lambda x: x[0] not in info['primary'] # 主键排前面 ) # 构建HTML表格 column_labels = [col[1] for col in sorted_columns] label = f"""<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"> <TR><TD BGCOLOR="#EEEEFF"><B>{table}</B></TD></TR> {''.join([f'<TR><TD>{label}</TD></TR>' for label in column_labels])} </TABLE>>""" dot.node(table, label=label, shape='none') # 5. 生成关系边 existing_relations = set() # 避免重复关系 for table, info in table_info.items(): for fk in info['foreign_keys']: fk_column, ref_table, ref_column = fk # 关系唯一标识(A-B) relation_key = (ref_table, table) if relation_key in existing_relations: continue # 已处理过反向关系 existing_relations.add(relation_key) # 确定关系类型 if len(info['primary']) == 1 and any(ref_column == rpt[2] for rpt in table_info[ref_table]['foreign_keys']): cardinality = '1:N' # 双向1:N关系 else: cardinality = 'N:M' # 默认多对多 # 创建关系节点 rel_label = f"{table}→{ref_table} ({cardinality})" dot.node(rel_label, label=rel_label, shape='diamond', color='gray') # 建立连接 dot.edge(table, rel_label) dot.edge(rel_label, ref_table) # 6. 保存ER图 dot.render('classroom_behavior_er', view=True) # 7. 关闭连接 cursor.close() conn.close() 运行代码
最新发布
07-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值